-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convertVarIntToBytes: use reusable bytes array (#352)
Noticed while auditing and profiling dependencies of cosmos-sdk, that convertVarIntToBytes, while reusing already implemented code, it was expensively creating a bytes.Buffer (40B on 64-bit architectures) returning a result and discarding it, yet that code was called 3 times successively at least. By reusing a byte array (not a slice, to ensure bounds checks eliminations by the compiler), we are able to dramatically improve performance, taking it from ~4µs down to 850ns (~4.5X reduction), reduce allocations by >=~80% in every dimension: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta ConvertLeafOp-8 3.90µs ± 1% 0.85µs ± 4% -78.12% (p=0.000 n=10+10) name old alloc/op new alloc/op delta ConvertLeafOp-8 5.18kB ± 0% 0.77kB ± 0% -85.19% (p=0.000 n=10+10) name old allocs/op new allocs/op delta ConvertLeafOp-8 120 ± 0% 24 ± 0% -80.00% (p=0.000 n=10+10) ``` Fixes #344
- Loading branch information
Showing
2 changed files
with
47 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters