How can you write binary data, quickly, into a string?
Especially when this data is made from shorts/longs/floats/doubles, and
not bytes?
The problem is that an RB string has 1 byte of padding :o( This means
that the string is unaligned in RAM.
For example, let's say for a hypothetical example, that I wanted to
serialise a sound into a series of floats, (CoreAudio style), and then
return that data as a string, so that the user can save it to a file.
Well, because the RB string is unaligned, writing the data will be 4x
slower than it should be. That's no fun.
What would be a way to write binary to the string quickly? I have some
ideas, but none are perfect:
1) Hack the RB string's pointer (assuming that mPrivatedata is
unchanged, it *might* be possible to get away with this. But that's no
guarantee that this will still work in future RB versions.)
2) Pad 3 extra bytes at the start, and just write those 3 extra bytes
to the file. Perhaps I could write 1 byte to the file first before
writing the padded string, making the entire file aligned also.
3) Write to an aligned buffer, and BlockMoveData/memmove across.
4) Write to a memoryblock instead. (Unfortunately RB has no abilities
to write a memoryBlock directly to a binary stream! Or any stream for
that matter!)
As I said, none of these seem perfect.
Personally, I think RB would have been better if they did not have a
pstring at the start of the string. The pstring is basically optimising
for an outdated kind of declare.
So we have traded speed, simplicity, and RAM efficiency, for being
optimised for one particular use which happens to be a very out dated
kind of use... (Pstrings have been phased out of most places.)
bah. Bad design choice, in my opinion!
--
elfdata.com/plugin/ Industrial strength string processing, made easy.
http://groups.yahoo.com/group/elfdata/
"All things are logical. Putting free-will in the slot for premises in
a logical system, makes all of life both understandable, and free."
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|