Skip to content

Commit

Permalink
common/bitutil: use result of TestBytes to prevent dead code eliminat…
Browse files Browse the repository at this point in the history
…ion (ethereum#19846)

Gollvm has very aggressive dead code elimination that completely
removes one of these two benchmarks.  To prevent this, use the
result of the benchmark (a boolean), and to be "fair", make the
transformation to both benchmarks.

To be reliably assured of not removing the code, "use" means
assigning to an exported global.  Non-exported globals and
//go:noinline functions are possibly subject to this optimization.
  • Loading branch information
dr2chase authored and gzliudan committed Dec 28, 2024
1 parent 057a7dd commit 5a8110b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions common/bitutil/bitutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,20 @@ func benchmarkBaseOR(b *testing.B, size int) {
}
}

var GloBool bool // Exported global will not be dead-code eliminated, at least not yet.

// Benchmarks the potentially optimized bit testing performance.
func BenchmarkFastTest1KB(b *testing.B) { benchmarkFastTest(b, 1024) }
func BenchmarkFastTest2KB(b *testing.B) { benchmarkFastTest(b, 2048) }
func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) }

func benchmarkFastTest(b *testing.B, size int) {
p := make([]byte, size)
a := false
for i := 0; i < b.N; i++ {
TestBytes(p)
a = a != TestBytes(p)
}
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
}

// Benchmarks the baseline bit testing performance.
Expand All @@ -209,7 +213,9 @@ func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) }

func benchmarkBaseTest(b *testing.B, size int) {
p := make([]byte, size)
a := false
for i := 0; i < b.N; i++ {
safeTestBytes(p)
a = a != safeTestBytes(p)
}
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
}

0 comments on commit 5a8110b

Please sign in to comment.