chronoxor / FastBinaryEncoding

Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift
https://chronoxor.github.io/FastBinaryEncoding
MIT License
827 stars 86 forks source link

Clarification: saving offset in binary #97

Closed jozefhopko closed 7 months ago

jozefhopko commented 7 months ago

First of all, great project and implementation!

I was just wondering what is the motivation of using offset in binary for struct, string, vector etc. As described in the specification, these types are written by using [offset] which points to --> [size] [data..]

What is the advantage of using the offset for these data types? Why not just to write it directly after each other like bool or array attributes?

Thanks in advance!

chronoxor commented 7 months ago

Consider this example

struct
{
  bytes field1;
  string field2 = "";
  byte[] field3;
}

in the struct definition you'll have 3 offsets of unit32 pointing to the corresponding data later in buffer

[uint32(field1-offset)],[uint32(field2-offset)],[uint32(field3-offset)],[field1-size,field1-data],[field2-size,field2-data],[field3-size,field3-item1,...,field3-itemN]
jozefhopko commented 7 months ago

Great, thanks for the fast response!

Using your example, I was wondering, why not just remove the [uint32(field1-offset)],[uint32(field2-offset)],[uint32(field3-offset)]

and write directly [field1-size,field1-data],[field2-size,field2-data],[field3-size,field3-item1,...,field3-itemN]

This would reduce the size and parsing complexity. Or am I missing a use-case where the offset has an advantage?

chronoxor commented 7 months ago

You need to know offsets to access struct fileds, otherwise you need to travel from filed1 to fieldN. Without offsets your data will be variadic size.

jozefhopko commented 7 months ago

Perfect, makes sense, thanks for the clarification!