Categorygithub.com/superjsf2010/protoc-gen-fastjsonpb
repositorypackage
0.0.0-20220815074036-9739c837220f
Repository: https://github.com/superjsf2010/protoc-gen-fastjsonpb.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# README

protoc-gen-fastjsonpb

安装

go install github.com/superjsf2010/protoc-gen-fastjsonpb

使用

依赖

  • protoc
  • protoc-gen-go

编译

$PROTOC --go_out=$OUTPUT_DIR \
      --fastjsonpb_out=$OUTPUT_DIR \
      --proto_path=$PROTO_DIR \
      $PROTO_DIR/$var
  • go_out fastjsonpb__out 输出目录要保持一致

调用

...
import(
  fastjsonpb "github.com/superjsf2010/protoc-gen-fastjsonpb/encoding/json"
  // proto编译产出
  "github.com/superjsf2010/protoc-gen-fastjsonpb/test/example"
)
...
// 序列化
e1 := &example.Example{}
ret,err := fastjsonpb.Marshal(e1)

// 反序列化
e2 := example.ExampleNew()
// 也可以使用e2 := &example.Example{} 创建对象,上述方式会使用Pool提高性能
fastjsonpb.Unmarshal(ret, e2)
// !!!! 为提供性能,需要手动释放对象,释放的对象会进入Pool,当然你也可以不这么做
e2.Destructor()
...

性能对比

平台

goos: linux goarch: amd64 cpu: Intel(R) Xeon(R) CPU E5-26xx v4

源代码

https://github.com/superjsf2010/protoc-gen-fastjsonpb/blob/main/test/marshal_test.go https://github.com/superjsf2010/protoc-gen-fastjsonpb/blob/main/test/unmarshal_test.go

数据

ns/opallocation bytesallocation times
FastJsonpb Marshal2636 ns/op160 B/op12 allocs/op
StdJsonpb Marshal13927 ns/op2024 B/op43 allocs/op
StdJson Marshal3279 ns/op384 B/op1 allocs/op
Jsoniter Marshal5541 ns/op2528 B/op27 allocs/op
FastJsonpb Unmarshal3523 ns/op464 B/op7 allocs/op
StdJsonpb Unmarshal19182 ns/op2864 B/op101 allocs/op
StdJson Unmarshal10249 ns/op1416 B/op18 allocs/op
Jsoniter Unmarshal3354 ns/op1672 B/op30 allocs/op

备注

  • std-json, jsoniter不支持pb
  • 官方标准库已经迭代过多个版本,性能也得到大幅提升