Skip to content

Commit 305ed95

Browse files
authored
Merge pull request #20038 from holiman/minor_encodingfix
core/state: optimize some internals during encoding
2 parents 46b437f + 72045df commit 305ed95

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

common/bytes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,14 @@ func LeftPadBytes(slice []byte, l int) []byte {
134134

135135
return padded
136136
}
137+
138+
// TrimLeftZeroes returns a subslice of s without leading zeroes
139+
func TrimLeftZeroes(s []byte) []byte {
140+
idx := 0
141+
for ; idx < len(s); idx++ {
142+
if s[idx] != 0 {
143+
break
144+
}
145+
}
146+
return s[idx:]
147+
}

core/state/state_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (s *stateObject) updateTrie(db Database) Trie {
274274
continue
275275
}
276276
// Encoding []byte cannot fail, ok to ignore the error.
277-
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
277+
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
278278
s.setError(tr.TryUpdate(key[:], v))
279279
}
280280
return tr

core/state/state_object_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package state
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"math/rand"
23+
"testing"
24+
"time"
25+
26+
"github.com/ethereum/go-ethereum/common"
27+
)
28+
29+
func BenchmarkCutOriginal(b *testing.B) {
30+
value := common.HexToHash("0x01")
31+
for i := 0; i < b.N; i++ {
32+
bytes.TrimLeft(value[:], "\x00")
33+
}
34+
}
35+
36+
func BenchmarkCutsetterFn(b *testing.B) {
37+
value := common.HexToHash("0x01")
38+
cutSetFn := func(r rune) bool {
39+
return int32(r) == int32(0)
40+
}
41+
for i := 0; i < b.N; i++ {
42+
bytes.TrimLeftFunc(value[:], cutSetFn)
43+
}
44+
}
45+
46+
func BenchmarkCutCustomTrim(b *testing.B) {
47+
value := common.HexToHash("0x01")
48+
for i := 0; i < b.N; i++ {
49+
common.TrimLeftZeroes(value[:])
50+
}
51+
}
52+
53+
func xTestFuzzCutter(t *testing.T) {
54+
rand.Seed(time.Now().Unix())
55+
for {
56+
v := make([]byte, 20)
57+
zeroes := rand.Intn(21)
58+
rand.Read(v[zeroes:])
59+
exp := bytes.TrimLeft(v[:], "\x00")
60+
got := common.TrimLeftZeroes(v)
61+
if !bytes.Equal(exp, got) {
62+
63+
fmt.Printf("Input %x\n", v)
64+
fmt.Printf("Exp %x\n", exp)
65+
fmt.Printf("Got %x\n", got)
66+
t.Fatalf("Error")
67+
}
68+
//break
69+
}
70+
}

0 commit comments

Comments
 (0)