Skip to content

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

@bored-engineer

Description

@bored-engineer
  • 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions