Skip to content

Commit f054bef

Browse files
authored
rlp: optimize intsize (#32421)
goos: darwin goarch: arm64 pkg: github.com/ethereum/go-ethereum/rlp cpu: Apple M4 │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Intsize 2.175n ± 5% 1.050n ± 4% -51.76% (p=0.000 n=10)
1 parent a4d3fb9 commit f054bef

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

rlp/encode.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"math/big"
24+
"math/bits"
2425
"reflect"
2526

2627
"github.com/ethereum/go-ethereum/rlp/internal/rlpstruct"
@@ -487,9 +488,8 @@ func putint(b []byte, i uint64) (size int) {
487488

488489
// intsize computes the minimum number of bytes required to store i.
489490
func intsize(i uint64) (size int) {
490-
for size = 1; ; size++ {
491-
if i >>= 8; i == 0 {
492-
return size
493-
}
491+
if i == 0 {
492+
return 1
494493
}
494+
return (bits.Len64(i) + 7) / 8
495495
}

0 commit comments

Comments
 (0)