celestiaorg / go-header

Go library with all the services needed to request, sync and store blockchain headers.
Apache License 2.0
19 stars 18 forks source link

perf: faster hash marshaling methods #183

Closed cristaloleg closed 3 months ago

cristaloleg commented 4 months ago

Overview

Reduce a bit of allocations and cpu time in Hash arshaling methods. Same behaviour as before but slightly in another way: instead of operations on string we can do this on []byte. As a result we now have tests for that.

go-header % go-perftuner bstat a.txt b.txt
args: [a.txt b.txt]name                         old time/op    new time/op    delta
HashMarshaling/String-10        798ns ± 0%     613ns ± 0%  -23.08%  (p=0.002 n=6+6)
HashMarshaling/Marshal-10      1.11µs ± 0%    0.84µs ± 0%  -24.55%  (p=0.004 n=5+6)
HashMarshaling/Unmarshal-10     333ns ±11%     277ns ± 1%  -16.92%  (p=0.004 n=5+6)

name                         old alloc/op   new alloc/op   delta
HashMarshaling/String-10         192B ± 0%      128B ± 0%  -33.33%  (p=0.002 n=6+6)
HashMarshaling/Marshal-10        296B ± 0%      104B ± 0%  -64.86%  (p=0.002 n=6+6)
HashMarshaling/Unmarshal-10      128B ± 0%       32B ± 0%  -75.00%  (p=0.002 n=6+6)

name                         old allocs/op  new allocs/op  delta
HashMarshaling/String-10         3.00 ± 0%      2.00 ± 0%        ~  (p=0.002 n=6+6)
HashMarshaling/Marshal-10        5.00 ± 0%      2.00 ± 0%  -60.00%  (p=0.002 n=6+6)
HashMarshaling/Unmarshal-10      2.00 ± 0%      1.00 ± 0%        ~  (p=0.002 n=6+6)
cristaloleg commented 4 months ago

@renaynay added a fuzzing test for hexToUpper (to run: go test -v -run=^$ -fuzz=Fuzz_hexToUpper .)