google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Swift] Return Data from ByteBuffer.

kennycarruthers opened this issue · comments

In pull 7093, ByteBuffer.underlyingBytes was added. Is there a reason this API returns an Array[UInt8] rather than the more common Data?

The JSONEncoder returns a Data and in almost every case where I'd like to use serialized data, it would be as Data, not Array. I'm pretty sure that calling Data(underlyingBytes) causes a full allocation and copy to occur, which would be nice to avoid.

Or if you'd prefer to keep the existing API, then an additional computed property of .data or .underlyingData would be helpful.

That was added for a special case, so if you want to actually get the data behind the buffer, you should always use the FlatbufferBuilder.data instead because the byte buffer doesn't know when the data has been serialized.

Fair enough. I see that ByteBuffer is used throughout FlatBuffers which would make it annoying to convert back-and-forth between Data.

I was originally using the Object API which provides a convenient serialize function that returns a ByteBuffer but not Data.

(Feel free to close this.)

If you want there is an API, that you can use that passes a FlatbufferBuilder to the serialize function that should allow you to extract the data object each time instead of getting a ByteBuffer

If you want there is an API, that you can use that passes a FlatbufferBuilder to the serialize function that should allow you to extract the data object each time instead of getting a ByteBuffer

Great, thank you.