From 49dc5e85e19e477811f4e4ee0f47cd080b480858 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Thu, 21 Oct 2021 11:43:23 +0200 Subject: [PATCH] common/hexutil: improve performance of EncodeBig (#23780) - use Text instead of fmt.Sprintf - reduced allocs from 6 to 2 - improved speed --- common/hexutil/hexutil.go | 11 ++++++----- common/hexutil/hexutil_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index 02c488a3f178..81e68046500b 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -18,7 +18,7 @@ Package hexutil implements hex encoding with 0x prefix. This encoding is used by the Ethereum RPC API to transport binary data in JSON payloads. -Encoding Rules +# Encoding Rules All hex data must have prefix "0x". @@ -175,13 +175,14 @@ func MustDecodeBig(input string) *big.Int { } // EncodeBig encodes bigint as a hex string with 0x prefix. -// The sign of the integer is ignored. func EncodeBig(bigint *big.Int) string { - nbits := bigint.BitLen() - if nbits == 0 { + if sign := bigint.Sign(); sign == 0 { return "0x0" + } else if sign > 0 { + return "0x" + bigint.Text(16) + } else { + return "-0x" + bigint.Text(16)[1:] } - return fmt.Sprintf("%#x", bigint) } func has0xPrefix(input string) bool { diff --git a/common/hexutil/hexutil_test.go b/common/hexutil/hexutil_test.go index ed6fccc3ca6f..f2b800d82c9d 100644 --- a/common/hexutil/hexutil_test.go +++ b/common/hexutil/hexutil_test.go @@ -201,3 +201,15 @@ func TestDecodeUint64(t *testing.T) { } } } + +func BenchmarkEncodeBig(b *testing.B) { + for _, bench := range encodeBigTests { + b.Run(bench.want, func(b *testing.B) { + b.ReportAllocs() + bigint := bench.input.(*big.Int) + for i := 0; i < b.N; i++ { + EncodeBig(bigint) + } + }) + } +}