common/hexutil: improve performance of EncodeBig (#23780)

- use Text instead of fmt.Sprintf
- reduced allocs from 6 to 2
- improved speed
This commit is contained in:
lmittmann
2021-10-21 11:43:23 +02:00
committed by GitHub
parent b6fb18479c
commit 2954f40eac
2 changed files with 17 additions and 4 deletions

View File

@ -176,13 +176,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 {