From ed1570c2d1f773d8c6e665103c842f3cc27582ac Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Wed, 3 Sep 2025 15:37:14 +0800 Subject: [PATCH] rlp: optimize intsize #32421 (#1403) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) Co-authored-by: cui --- rlp/encode.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rlp/encode.go b/rlp/encode.go index 9435cfc22c..25b1a96c1a 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -21,6 +21,7 @@ import ( "fmt" "io" "math/big" + "math/bits" "reflect" "github.com/XinFinOrg/XDPoSChain/rlp/internal/rlpstruct" @@ -487,9 +488,8 @@ func putint(b []byte, i uint64) (size int) { // intsize computes the minimum number of bytes required to store i. func intsize(i uint64) (size int) { - for size = 1; ; size++ { - if i >>= 8; i == 0 { - return size - } + if i == 0 { + return 1 } + return (bits.Len64(i) + 7) / 8 }