Merge pull request #2133 from nspcc-dev/optimize-util

util: reduce allocations in `util.Uint256DecodeStringLE`
This commit is contained in:
Roman Khimov 2021-08-17 19:20:16 +03:00 committed by GitHub
commit 483934d3a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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()
}
}
}