Structures, Pointers, Memory Blocks, Streams, Packing... keeping it fast.

I have a class that gets instantiated from a binary stream.

A binarystream is constructed from a string that came in from a socket event or from a binarystream that was created from a file.
So data read from the socket has to be converted into a binarystream to be passed to the constructor of the class.

But what will that binary stream reading from?

Was a copy of the string made in the constructor of the stream such that when the stream’s reference count goes to zero the copy of the string is released?
I mean you can’t make a reference to a string right?

Can’t I have the socket write the binary stream and the class read the binary stream?

I have more questions, but I’ll save for now.

I would expect that to be the case. Internally, it probably copies the string into a memoryblock. So don’t keep the original string around.

I do know that if you pass a memoryblock to the binarystream, it just points to it. You can modify the contents of the memoryblock and the binarystream sees the modification. So if the string is really big, read it in small chunks into a memoryblock and create the binarystream from that. That would prevent you from having 2 copies of a big string in memory at any one time.


Unfortunately, no.

Can the Constructor be changed to take a Readable instead of a BinaryStream?

If I have a string or structure of length 20, Can I create two binarystream? (one from 0-14 and the other from 15-19)
Without two copies of the static data?

Use a memoryblock.

Memory Blocks do not provide a notion of a stream nor eof.

Sure it does. It acts the same as a string and has the added benefit of not creating a copy when you instantiate the binarystream. But perhaps I don’t fully understand what you’re trying to do.

Perhaps all I need to do is to subclass MemoryBlock to be able to read sequentially without exposing the offset method of the memory block class? Kinda like a stream.
That’s pretty clear.

If I have a structure :

typedef struct { U16Bit x; U8Bit a; U32Bit y; ] myDataType;

In C the sizeof that structure may change depending on structure packing parameters passed to the C compiler.
There is most likely only 1 byte of difference in the size here depending on that parameter.
If structure packing is off there will be a blank unused byte after the a and before the y. This is considered an optimization to make sure that the member lands on an even memory address. (It’s faster to read that value)

I’m not sure how this is done in Xojo… but it matters when transmitting structures over the a network connection.
The memoryblock is ok for stuff like that but then must be turned into a string or stream.
Maybe a ptr to a structure?

So are you trying to get away from using a binarystream?

I have some pieces of old code that relied on structures…
and some that were defined in the ide. I’m wondering about their usefulness and issues.