util: reduce allocations in util.Uint256DecodeStringLE

It is used a lot in clients (including our benchmark).
`Uint160` is already optimized.

```
name                     old time/op    new time/op    delta
Uint256DecodeStringLE-8     150ns ±15%     112ns ± 3%  -25.23%  (p=0.000 n=10+10)

name                     old alloc/op   new alloc/op   delta
Uint256DecodeStringLE-8     96.0B ± 0%     64.0B ± 0%  -33.33%  (p=0.000 n=10+10)

name                     old allocs/op  new allocs/op  delta
Uint256DecodeStringLE-8      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-17 16:50:26 +03:00
parent 11351b9702
commit 8c31831626
2 changed files with 15 additions and 1 deletions

View file

@ -26,7 +26,8 @@ func Uint256DecodeStringLE(s string) (u Uint256, err error) {
if err != nil {
return u, err
}
return Uint256DecodeBytesLE(b)
slice.Reverse(b)
return Uint256DecodeBytesBE(b)
}
// Uint256DecodeStringBE attempts to decode the given string (in BE representation)

View file

@ -95,3 +95,16 @@ func TestUint256_Serializable(t *testing.T) {
var b util.Uint256
testserdes.EncodeDecodeBinary(t, &a, &b)
}
func BenchmarkUint256DecodeStringLE(b *testing.B) {
a := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := util.Uint256DecodeStringLE(a)
if err != nil {
b.FailNow()
}
}
}