slackhq/go-audit

Consider using code generation (ex: easyjson) for JSON models

bored-engineer opened this issue · 0 comments

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Description

Currently go-audit uses the stdlib encoding/json class to marshal the AuditMessageGroup and AuditMessage structs into a []byte. Internally the encoding/json package uses reflection so it can marshal arbitrary objects.

Because go-audit only uses two static structures there would likely be a significant performance improvement using a code generation library like easyjson to serialize the structures into bytes.

You can find various benchmarks on JSON serialization performance across code generation packages vs stdlib but I am partial to go_serialization_benchmarks:

benchmark iter time/iter bytes/op allocs/op tt.sec tt.kb ns/alloc
BenchmarkJsonMarshal-8 1000000 1585 ns/op 304 4 1.58 30400 396.25
BenchmarkEasyJsonMarshal-8 1000000 1125 ns/op 784 5 1.12 78400 225.00

This comes at the cost of an additional dependency (primarily when the models change only)