Skip to content

perf(libs/json): Lower heap overhead of JSON encoding (backport #2846)#2876

Merged
andynog merged 2 commits intov0.37.xfrom
mergify/bp/v0.37.x/pr-2846
Apr 23, 2024
Merged

perf(libs/json): Lower heap overhead of JSON encoding (backport #2846)#2876
andynog merged 2 commits intov0.37.xfrom
mergify/bp/v0.37.x/pr-2846

Conversation

@mergify
Copy link
Contributor

@mergify mergify bot commented Apr 23, 2024


Many RPC methods require JSON marshalled responses. We saw this taking a notable amount of heap allocation in query serving full nodes. This PR removes some extra heap allocations that were being done. We avoided using the more efficient encoder.Encode before, because it added a newline. This PR changes the function signature for these private methods to be using *bytes.Buffer, and then uses the in-buffer methods (rather than a second copy). We then just truncate the final byte after each such call, which does not waste any allocations.

I added a benchmark for the most complex test case.

OLD:

BenchmarkJsonMarshalStruct-12              78992             15542 ns/op            4487 B/op        191 allocs/op

New:

BenchmarkJsonMarshalStruct-12              93346             11132 ns/op            3245 B/op         58 allocs/op

Roughly a 3-4x reduction in the number of allocations, and 20% speedup.

PR checklist

  • Tests written/updated - Existing tests cover this
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

This is an automatic backport of pull request #2846 done by [Mergify](https://mergify.com).

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants