高性能的序列化和反序列化算法库xxxPack

对比目前业界高性能库:

  • encoding/gob
  • encoding/json
  • github.com/vmihailenco/msgpack/v5
  • labix.org/v2/mgo/bson
  • github.com/valyala/fastjson
  • github.com/json-iterator/go
  • 自研序列化算法

验证数据集合:

type Data struct {
    Name     string
    BirthDay time.Time
    Phone    string
    Siblings int
    Spouse   bool
    Money    float64
    ExInfo   interface{}
}

Benchmark Results

2023-02-09 Results with

  • Go 1.19.4
  • MacPro Apple M2 8G macOS Ventura
  • Darwin 192.168.1.7 22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:06:26 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8112 arm64

环境下,数据:

goos: darwin
goarch: amd64
pkg: github.com/kubeservice-stack/serialization-benchmarks
cpu: VirtualApple @ 2.50GHz
Benchmark_Bson_Marshal-8         	 2272702	       499.2 ns/op	       110.0 B/serial	     376 B/op	      10 allocs/op
Benchmark_Bson_Unmarshal-8       	 1617357	       716.2 ns/op	       110.0 B/serial	     224 B/op	      19 allocs/op
Benchmark_FastJson_Marshal-8     	 4016576	       296.9 ns/op	       133.8 B/serial	     504 B/op	       6 allocs/op
Benchmark_FastJson_Unmarshal-8   	 1527366	       778.7 ns/op	       133.8 B/serial	    1704 B/op	       9 allocs/op
Benchmark_Gob_Marshal-8          	  467408	      2475 ns/op	       163.6 B/serial	    1616 B/op	      35 allocs/op
Benchmark_Gob_Unmarshal-8        	   95322	     12386 ns/op	       163.6 B/serial	    7688 B/op	     207 allocs/op
Benchmark_JsonIter_Marshal-8     	 1941692	       591.5 ns/op	       148.7 B/serial	     216 B/op	       3 allocs/op
Benchmark_JsonIter_Unmarshal-8   	 1307601	       871.8 ns/op	       148.7 B/serial	     359 B/op	      14 allocs/op
Benchmark_Json_Marshal-8         	 1406618	       850.8 ns/op	       148.6 B/serial	     208 B/op	       2 allocs/op
Benchmark_Json_Unmarshal-8       	  494430	      2153 ns/op	       148.6 B/serial	     399 B/op	       9 allocs/op
Benchmark_xxxPack_Marshal-8      	 3624224	       312.8 ns/op	       119.0 B/serial	     344 B/op	       6 allocs/op
Benchmark_xxxPack_Unmarshal-8    	 2070897	       574.7 ns/op	       119.0 B/serial	     112 B/op	       3 allocs/op
Benchmark_Msgpack_Marshal-8      	 2961548	       401.9 ns/op	        92.00 B/serial	     264 B/op	       4 allocs/op
Benchmark_Msgpack_Unmarshal-8    	 2209035	       536.9 ns/op	        92.00 B/serial	     160 B/op	       4 allocs/op
PASS
ok  	github.com/kubeservice-stack/serialization-benchmarks	23.928s

总结

  1. xxxPack 一次序列化+一次反序列化性能最优 综合排第1 :574.7 ns/op + 312.8 ns/op = 887.5 ns/op
  2. xxxPack 整体性能是 golang原生json库的 3.38倍 :(2153+850.8)/ 887.5 = 3.3845倍
  3. xxxPack 每次申请操作平均内存申请大小(119B/serial)排 第3; xxxPack Unmarshal 内存申请最小(112 B/op)排第1
  4. xxxPack Unmarshal 每次操作申请了内存次数 排第2; Marshal 排第4;