前段時間剛試用了一個序列化工具cereal,請看cereal:C++實現的開源序列化庫,打算再總結下我對google proto buf序列化庫的使用呢,html
結果還沒動手,大Google又出了一個新的、開源、跨平臺的序列化工具:FlatBuffers。那就索性先了解了解這個工具把。git
一. 什麼是Google FlatBuffersgithub
FlatBuffers是一個開源的、跨平臺的、高效的、提供了C++/Java接口的序列化工具庫。它是Google專門爲遊戲開發或其餘性能敏感的應用程序需求而建立。尤爲更適用於移動平臺,這些平臺上內存大小及帶寬相比桌面系統都是受限的,而應用程序好比遊戲又有更高的性能要求。它將序列化數據存儲在緩存中,這些數據既能夠存儲在文件中,又能夠經過網絡原樣傳輸,而不須要任何解析開銷。緩存
代碼託管主頁:https://github.com/google/flatbuffers;網絡
項目介紹主頁:http://google.github.io/flatbuffers/index.html;函數
二. 爲何要使用Google FlatBuffers工具
三. 爲何不使用Protocol Buffers的,或者JSON性能
Protocol Buffers的確和FlatBuffers比較相似,但其主要區別在於FlatBuffers在訪問數據前不須要解析/拆包這一步。 並且Protocol Buffers既沒有可選的文本導入/導出功能,也沒有Schemas語法特性(好比union)。測試
JSON是很是可讀的,並且當和動態類型語言(如JavaScript)一塊兒使用時很是方便。然而在靜態類型語言中序列化數據時,JSON不但具備運行效率低的明顯缺點,並且會讓你寫更多的代碼來訪問數據(這個與直覺相反)。ui
想了解更多關於FlatBuffers的「爲何」請訪問flatbuffers白皮書。
四. 內建的數據類型
byte ubyte bool
short ushort
int uint float
long ulong double
[type]
). Nesting vectors is not supported, instead you can wrap the inner vector in a table.[byte]
or [ubyte]
) instead.詳細介紹請參考:schema語法格式。
五. 如何使用
六. 一個簡單的Schemas(IDL)文件
namespace zl.persons; enum GENDER_TYPE : byte { MALE = 0, FEMALE = 1, OTHER = 2 } table personal_info { id : uint; name : string; age : byte; gender : GENDER_TYPE; phone_num : ulong; } table personal_info_list { info : [personal_info]; } root_type personal_info_list;
注意:這裏有table、struct的區別:
table是Flatbuffers中用來定義對象的主要方式,和struct最大的區別在於:它的每一個字段都是可選的(相似protobuf中的optional字段),而struct的全部成員都是required。
table除了成員名稱和類型以外,還能夠給成員一個默認值,若是不顯式指定,則默認爲0(或空)。struct不能定義scalar成員,好比說string類型的成員。在生成C++代碼時,struct的成員順序會保持和IDL的定義順序一致,若是有必要對齊,生成器會自動生成用於對齊的額外成員。如如下Schemas代碼:
struct STest { a : int; b : int; c : byte; }
在生成爲C++代碼以後,會補充兩個用於padding的成員__padding0與__padding1:
MANUALLY_ALIGNED_STRUCT(4) STest { private: int32_t a_; int32_t b_; int8_t c_; int8_t __padding0; int16_t __padding1; public: STest(int32_t a, int32_t b, int8_t c) : a_(flatbuffers::EndianScalar(a)), b_(flatbuffers::EndianScalar(b)), c_(flatbuffers::EndianScalar(c)), __padding0(0) {} int32_t a() const { return flatbuffers::EndianScalar(a_); } int32_t b() const { return flatbuffers::EndianScalar(b_); } int8_t c() const { return flatbuffers::EndianScalar(c_); } };
STRUCT_END(STest, 12);
table的成員順序是動態調整的,這和struct有區別。在生成C++代碼時,生成器會自動調整爲最佳順序以保證它佔用最小的內存空間。
七. 一個完整Demo
這裏只給一個函數演示如何對對象進行序列化,完整工程請直接點擊下載,或者前往github查看google_flatbuffers_test。
std::string CreateOnePerson() { flatbuffers::FlatBufferBuilder builder; fb_offset<fb_string> name = builder.CreateString("hello word"); zl::persons::personal_infoBuilder pib(builder); pib.add_id(1); pib.add_age(25); pib.add_gender(zl::persons::GENDER_TYPE_MALE); pib.add_name(name); pib.add_phone_num(1234567890); flatbuffers::Offset<zl::persons::personal_info> personinfo = pib.Finish(); fb_offset<zl::persons::personal_info> info[1]; info[0] = personinfo; fb_offset<fb_vector<fb_offset<zl::persons::personal_info>>> info_array = fb_create_vector(builder, info, sizeof(info) / sizeof(info[0])); fb_offset<zl::persons::personal_info_list> info_list = create_personal_info_list(builder, info_array); fb_finish(builder, info_list); // return the buffer for the caller to use. return std::string(reinterpret_cast<const char *>(builder.GetBufferPointer()), builder.GetSize()); }
八. 其餘
關於性能,除了Google公佈的基準測試外,有人本身測試驗證過,上面的IDL文件即來源於該做者的這篇文章。
九. 參考
http://google.github.io/flatbuffers/index.html
http://powman.org/archives/md__schemas.html
http://blog.csdn.net/menggucaoyuan/article/details/34409433
http://liubin.org/2014/06/19/google-flatbuffers-cross-platform-serialization-library/