question about Flatbuffer vs raw struct

890 views
Skip to first unread message

SuperHong

unread,
Dec 4, 2021, 1:47:38 AM12/4/21
to FlatBuffers
I study C++ gameServer 

I use struct packet 

like 

#pragma pack (push, 1)
struct cs_packet_login {
unsigned char size;
char type;
char name[MAX_NAME_SIZE];
};
#pragma pack(pop)

What's better about Flatbuffer than Struct?

faster?
Convenience?

My server is C++
My Client is C++

My expression is awkward because I am not good at English.

mikkelfj

unread,
Dec 4, 2021, 7:40:05 PM12/4/21
to FlatBuffers
FlatBuffers are not faster than structs and are not more convient than structs, but only when structs is all that you need. FlatBuffers work across more languages than C++ and allow for versioning so you can add fields later and still use buffers encoded earlier on, and you can have complex hierarchies of data and variable length strings and arrays (called vectors). Given these extra features, FlatBuffers are typically faster than other encodings that offer similar features, and often a lot faster than necessary. But if you do not need the flexibility, simpler encodings like structs can be better.

You can represent structs inside FlatBuffers and these structs will be compatible across different languages and different C/C++ compilers. The pragma pack struct is not a universal standard so FlatBuffer tools can ensure that a compiler either fails with an assertion or represents the struct correctly.

Also, FlatBuffers can work across little and big endian platforms.

You cannot have FlatBuffers that are purely structs, they must be stored in so called tables (except the FlatCC tool for C, that does allow for pure struct buffers for speed and portability). The tables can point to other tables and other structural data outside itself, and it can contain simple data and structs inside the tables memory layout.

The most important part of FlatBuffers is the version stability aspect.

Usually FlatBuffers are about 10 times slower than structs to encode, and 2 times slower to read compared to simple structs, but I depends a lot on the particular use case.

FlatBuffers can also easily be converted to JSON, so you can use the FlatBuffer schema for JSON communication, or mixed communication where some endpoints require JSON. However, only C and C++ readily supports JSON encoded FlatBuffers. The C implementation (that I wrote) supports very fast parsing of FlatBuffers into JSON and vice versa.

If you need to verify data, FlatBuffers is a bit more sensitive to malicious input than JSON or protocol buffers because it use direct access to data instead of parsing data, but it is possible to verify a buffer before accessing it, especially in C and C++.

SuperHong

unread,
Dec 7, 2021, 3:18:22 AM12/7/21
to FlatBuffers
Thank you for your kind answer

I understand
struct is faster than Flatbuffer
but Flatbuffer overcomes platform(OS, languages, compiler ...)

I will use Flatbuffer in the environment where C++ server and  Unity(C#) client 

thank you

Good Luck

2021년 12월 5일 일요일 오전 9시 40분 5초 UTC+9에 mikkelfj님이 작성:
Reply all
Reply to author
Forward
0 new messages