Skip to content

Commit 891d506

Browse files
authored
refactor: use b.Loop() to simplify the code and improve performance (#17358)
replace `for i := range b.N` or `for range b.N` in a benchmark with `for b.Loop()`, and remove any preceding calls to `b.StopTimer`,` b.StartTimer`, and `b.ResetTimer`. Supported by Go Team, more info: https://go.dev/blog/testing-b-loop Before: ```shell go test -run=^$ -bench=. ./execution/types -timeout=1h goos: darwin goarch: arm64 pkg: github.com/erigontech/erigon/execution/types cpu: Apple M4 BenchmarkEncodeBlock-10 173065 6771 ns/op BenchmarkBloom9-10 368839657 3.256 ns/op BenchmarkBloom9Lookup-10 4975617 240.6 ns/op BenchmarkCreateBloom/small-10 1242428 964.7 ns/op 8 B/op 1 allocs/op BenchmarkCreateBloom/large-10 12807 93468 ns/op 8 B/op 1 allocs/op BenchmarkHeaderRLP/Encode-10 9562680 124.2 ns/op 0 B/op 0 allocs/op BenchmarkHeaderRLP/Decode-10 2297347 518.4 ns/op 504 B/op 15 allocs/op BenchmarkLegacyTxRLP-10 16128363 72.30 ns/op BenchmarkAccessListTxRLP-10 7349416 185.0 ns/op BenchmarkDynamicFeeTxRLP-10 5958285 195.3 ns/op BenchmarkBlobTxRLP-10 100000000 148.9 ns/op BenchmarkSetCodeTxRLP-10 100000000 341.8 ns/op BenchmarkWithdrawalRLP-10 46775037 24.73 ns/op BenchmarkLegacySmallList-10 22819 52377 ns/op BenchmarkCurrentSmallList-10 26280 45301 ns/op BenchmarkLegacyLargeList-10 21 53830175 ns/op BenchmarkCurrentLargeList-10 25 44664355 ns/op PASS ok github.com/erigontech/erigon/execution/types 74.201s ``` After this change: ```shell go test -run=^$ -bench=. ./execution/types -timeout=1h goos: darwin goarch: arm64 pkg: github.com/erigontech/erigon/execution/types cpu: Apple M4 BenchmarkEncodeBlock-10 160569 7002 ns/op BenchmarkBloom9-10 33985428 30.14 ns/op BenchmarkBloom9Lookup-10 4995614 243.7 ns/op BenchmarkCreateBloom/small-10 1238462 965.2 ns/op 8 B/op 1 allocs/op BenchmarkCreateBloom/large-10 12829 94128 ns/op 8 B/op 1 allocs/op BenchmarkHeaderRLP/Encode-10 9330062 128.0 ns/op 0 B/op 0 allocs/op BenchmarkHeaderRLP/Decode-10 2145024 572.4 ns/op 936 B/op 15 allocs/op BenchmarkLegacyTxRLP-10 17637829 67.74 ns/op BenchmarkAccessListTxRLP-10 10397460 114.7 ns/op BenchmarkDynamicFeeTxRLP-10 5630047 211.0 ns/op BenchmarkBlobTxRLP-10 8582659 138.2 ns/op BenchmarkSetCodeTxRLP-10 3109618 384.2 ns/op BenchmarkWithdrawalRLP-10 48780487 23.98 ns/op BenchmarkLegacySmallList-10 23052 52040 ns/op BenchmarkCurrentSmallList-10 26418 45358 ns/op BenchmarkLegacyLargeList-10 21 53593482 ns/op BenchmarkCurrentLargeList-10 26 44445859 ns/op PASS ok github.com/erigontech/erigon/execution/types 24.190s ``` Signed-off-by: cargoedit <[email protected]>
1 parent 62fa494 commit 891d506

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

execution/types/accounts/account_benchmark_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ func BenchmarkIsEmptyCodeHash(b *testing.B) {
444444
}
445445

446446
var isEmpty bool
447-
b.ResetTimer()
448-
for i := 0; i < b.N; i++ {
447+
448+
for b.Loop() {
449449
isEmpty = acc.IsEmptyCodeHash()
450450
}
451451
b.StopTimer()
@@ -462,8 +462,8 @@ func BenchmarkIsEmptyRoot(b *testing.B) {
462462
}
463463

464464
var isEmpty bool
465-
b.ResetTimer()
466-
for i := 0; i < b.N; i++ {
465+
466+
for b.Loop() {
467467
isEmpty = acc.IsEmptyRoot()
468468
}
469469
b.StopTimer()

execution/types/block_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))
313313

314314
func BenchmarkEncodeBlock(b *testing.B) {
315315
block := makeBenchBlock()
316-
b.ResetTimer()
317316

318-
for i := 0; i < b.N; i++ {
317+
for b.Loop() {
319318
benchBuffer.Reset()
320319
if err := rlp.Encode(benchBuffer, block); err != nil {
321320
b.Fatal(err)

execution/types/bloom9_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func TestBloomExtensively(t *testing.T) {
8585

8686
func BenchmarkBloom9(b *testing.B) {
8787
test := []byte("testestestest")
88-
for i := 0; i < b.N; i++ {
88+
for b.Loop() {
8989
Bloom9(test)
9090
}
9191
}
9292

9393
func BenchmarkBloom9Lookup(b *testing.B) {
9494
toTest := []byte("testtest")
9595
bloom := new(Bloom)
96-
for i := 0; i < b.N; i++ {
96+
for b.Loop() {
9797
bloom.Test(toTest)
9898
}
9999
}

execution/types/encdec_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ func BenchmarkLegacyTxRLP(b *testing.B) {
679679
tr := NewTRand()
680680
txn := tr.RandTransaction(LegacyTxType)
681681
var buf bytes.Buffer
682-
b.ResetTimer()
683-
for i := 0; i < b.N; i++ {
682+
683+
for b.Loop() {
684684
buf.Reset()
685685
txn.EncodeRLP(&buf)
686686
}
@@ -690,8 +690,8 @@ func BenchmarkAccessListTxRLP(b *testing.B) {
690690
tr := NewTRand()
691691
txn := tr.RandTransaction(AccessListTxType)
692692
var buf bytes.Buffer
693-
b.ResetTimer()
694-
for i := 0; i < b.N; i++ {
693+
694+
for b.Loop() {
695695
buf.Reset()
696696
txn.EncodeRLP(&buf)
697697
}
@@ -701,8 +701,8 @@ func BenchmarkDynamicFeeTxRLP(b *testing.B) {
701701
tr := NewTRand()
702702
txn := tr.RandTransaction(DynamicFeeTxType)
703703
var buf bytes.Buffer
704-
b.ResetTimer()
705-
for i := 0; i < b.N; i++ {
704+
705+
for b.Loop() {
706706
buf.Reset()
707707
txn.EncodeRLP(&buf)
708708
}
@@ -712,8 +712,8 @@ func BenchmarkBlobTxRLP(b *testing.B) {
712712
tr := NewTRand()
713713
txn := tr.RandTransaction(BlobTxType)
714714
var buf bytes.Buffer
715-
b.ResetTimer()
716-
for i := 0; i < b.N; i++ {
715+
716+
for b.Loop() {
717717
buf.Reset()
718718
txn.EncodeRLP(&buf)
719719
}
@@ -723,8 +723,8 @@ func BenchmarkSetCodeTxRLP(b *testing.B) {
723723
tr := NewTRand()
724724
txn := tr.RandTransaction(SetCodeTxType)
725725
var buf bytes.Buffer
726-
b.ResetTimer()
727-
for i := 0; i < b.N; i++ {
726+
727+
for b.Loop() {
728728
buf.Reset()
729729
txn.EncodeRLP(&buf)
730730
}
@@ -734,8 +734,8 @@ func BenchmarkWithdrawalRLP(b *testing.B) {
734734
tr := NewTRand()
735735
w := tr.RandWithdrawal()
736736
var buf bytes.Buffer
737-
b.ResetTimer()
738-
for i := 0; i < b.N; i++ {
737+
738+
for b.Loop() {
739739
buf.Reset()
740740
w.EncodeRLP(&buf)
741741
}

execution/types/hashing_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,25 @@ var (
114114
)
115115

116116
func BenchmarkLegacySmallList(b *testing.B) {
117-
for i := 0; i < b.N; i++ {
117+
for b.Loop() {
118118
legacyDeriveSha(smallTxList)
119119
}
120120
}
121121

122122
func BenchmarkCurrentSmallList(b *testing.B) {
123-
for i := 0; i < b.N; i++ {
123+
for b.Loop() {
124124
DeriveSha(smallTxList)
125125
}
126126
}
127127

128128
func BenchmarkLegacyLargeList(b *testing.B) {
129-
for i := 0; i < b.N; i++ {
129+
for b.Loop() {
130130
legacyDeriveSha(largeTxList)
131131
}
132132
}
133133

134134
func BenchmarkCurrentLargeList(b *testing.B) {
135-
for i := 0; i < b.N; i++ {
135+
for b.Loop() {
136136
DeriveSha(largeTxList)
137137
}
138138
}

0 commit comments

Comments
 (0)