No more massive serialization and deserialization code, leads to a more tidy project. No more meaningless diff of generated code in code review.
Serialized and Deserialize struct generated by Thriftgo
If you have a Thrift file, and all you need is using Frugal to do serialization and deserialization. You can use thriftgo to generate Go struct, then you can use Frugal.
Serialization and deserialization on a customized Go struct
If you don’t want any Thrift files, and you want serialize or deserialize a customized Go struct. You can add some struct field tag to the Go struct, then you can use Frugal.
Usage
Using with Kitex
1. Update Kitex and Frugal
go get github.com/cloudwego/kitex@latest
go get github.com/cloudwego/frugal@latest
2. Generate code with -thrift frugal_tag option
Example:
kitex -thrift frugal_tag -service a.b.c my.thrift
If you don’t need codec code, you can use -thrift template=slim option to reduce generated code significantly.
Note: Kitex automatically falls back to FastCodec or Apache Thrift for types that don’t have frugal struct tags, so it’s safe to enable Frugal globally.
Codec type options
Option
Description
thrift.FrugalRead
Use Frugal for deserialization
thrift.FrugalWrite
Use Frugal for serialization
thrift.FrugalReadWrite
Shorthand for FrugalRead | FrugalWrite
thrift.FastRead
Use FastCodec for deserialization
thrift.FastWrite
Use FastCodec for serialization
thrift.EnableSkipDecoder
Required when using Buffered transport (no Framed/TTHeader)
For Framed or TTHeader transport, FrugalRead | FrugalWrite is sufficient. For Buffered (PurePayload) transport, add EnableSkipDecoder:
Now we have thrift file, we can use Thriftgo to generate Go code.
Recent thriftgo versions generate thrift struct tags that Frugal can read directly, including extra type information for list, set, enum, and maps containing them when needed. Add frugal_tag if you want explicit Frugal tags, or when the generated code will be used with Kitex’s Frugal codec:
Frugal tag is like frugal:"1,default,string", 1 is field ID, default is field requiredness, string is field type. Field ID is required. Requiredness is optional and defaults to default. Field type is usually optional, but required for list, set, enum, and maps containing them.
Frugal
A very fast dynamic Thrift serializer & deserializer without generating code.
Features
Code Generation Free
Traditional Thrift serializer and deserializer are based on generated code which is no longer needed since we can make use of struct field tags.
High Performance
Based on the test cases in
frugal/tests, Frugal is about 2.5x to 3.7x faster than Apache Thrift (TBinaryProtocol) in the benchmark below.There may be variations between different test cases. Feel free to share your test cases with us.
What can you do with Frugal ?
Use Frugal as Kitex serializer and deserializer
No more massive serialization and deserialization code, leads to a more tidy project. No more meaningless diff of generated code in code review.
Serialized and Deserialize struct generated by Thriftgo
If you have a Thrift file, and all you need is using Frugal to do serialization and deserialization. You can use thriftgo to generate Go struct, then you can use Frugal.
Serialization and deserialization on a customized Go struct
If you don’t want any Thrift files, and you want serialize or deserialize a customized Go struct. You can add some struct field tag to the Go struct, then you can use Frugal.
Usage
Using with Kitex
1. Update Kitex and Frugal
2. Generate code with
-thrift frugal_tagoptionExample:
If you don’t need codec code, you can use
-thrift template=slimoption to reduce generated code significantly.Note: Kitex detects Frugal support through
frugalstruct tags, so keepfrugal_tagwhen enabling the Frugal codec.3. Init clients and servers with Frugal codec
Use
thrift.NewThriftCodecWithConfigto enable Frugal. Codec type priority: Frugal > FastCodec > Apache Thrift.Client example:
Server example:
Note: Kitex automatically falls back to FastCodec or Apache Thrift for types that don’t have
frugalstruct tags, so it’s safe to enable Frugal globally.Codec type options
thrift.FrugalReadthrift.FrugalWritethrift.FrugalReadWriteFrugalRead | FrugalWritethrift.FastReadthrift.FastWritethrift.EnableSkipDecoderFor Framed or TTHeader transport,
FrugalRead | FrugalWriteis sufficient. For Buffered (PurePayload) transport, addEnableSkipDecoder:Using with Thrift IDL
Prepare Thrift file
We can define a struct in Thrift file like below:
my.thrift:
Use Thriftgo to generate code
Now we have thrift file, we can use Thriftgo to generate Go code.
Recent thriftgo versions generate
thriftstruct tags that Frugal can read directly, including extra type information forlist,set,enum, and maps containing them when needed. Addfrugal_tagif you want explicit Frugal tags, or when the generated code will be used with Kitex’s Frugal codec:If you don’t need codec code, you can use
template=slimoption to reduce generated code:Use Frugal to serialize or deserialize
Now we can use Frugal to serialize or deserialize the struct defined in thrift file.
Example:
Serialization and deserialization on a customized Go struct
Define a Go struct
We can define a struct like this:
Add Frugal tag to struct fields
Frugal tag is like
frugal:"1,default,string",1is field ID,defaultis field requiredness,stringis field type. Field ID is required. Requiredness is optional and defaults todefault. Field type is usually optional, but required forlist,set,enum, and maps containing them.You can add Frugal tag to
MyStructlike below:All types example:
Use Frugal to serialize or deserialize
Example: