Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(libs/json): Lower heap overhead of JSON encoding (backport #2846) (
#2875) --- 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 - [x] Tests written/updated - Existing tests cover this - [x] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [x] Updated relevant documentation (`docs/` or `spec/`) and code comments - [x] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec <hr>This is an automatic backport of pull request #2846 done by [Mergify](https://mergify.com). --------- Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Andy Nogueira <me@andynogueira.dev>
- Loading branch information