diff options
author | Keith Randall <khr@golang.org> | 2021-05-07 14:14:39 -0700 |
---|---|---|
committer | Ben Shi <powerman1st@163.com> | 2021-05-08 03:27:59 +0000 |
commit | b211fe005860db3ceff5fd56af9951d6d1f44325 (patch) | |
tree | 2d4db9f01381ed1cab1ac47f620d08865e6a78ef /src/cmd/compile/internal/test | |
parent | f24eac47710b0170fd45611ab1867e87701e0a95 (diff) | |
download | go-b211fe005860db3ceff5fd56af9951d6d1f44325.tar.gz go-b211fe005860db3ceff5fd56af9951d6d1f44325.zip |
cmd/compile: remove bit operations that modify memory directly
These operations (BT{S,R,C}{Q,L}modify) are quite a bit slower than
other ways of doing the same thing.
Without the BTxmodify operations, there are two fallback ways the compiler
performs these operations: AND/OR/XOR operations directly on memory, or
load-BTx-write sequences. The compiler kinda chooses one arbitrarily
depending on rewrite rule application order. Currently, it uses
load-BTx-write for the Const benchmarks and AND/OR/XOR directly to memory
for the non-Const benchmarks. TBD, someone might investigate which of
the two fallback strategies is really better. For now, they are both
better than BTx ops.
name old time/op new time/op delta
BitSet-8 1.09µs ± 2% 0.64µs ± 5% -41.60% (p=0.000 n=9+10)
BitClear-8 1.15µs ± 3% 0.68µs ± 6% -41.00% (p=0.000 n=10+10)
BitToggle-8 1.18µs ± 4% 0.73µs ± 2% -38.36% (p=0.000 n=10+8)
BitSetConst-8 37.0ns ± 7% 25.8ns ± 2% -30.24% (p=0.000 n=10+10)
BitClearConst-8 30.7ns ± 2% 25.0ns ±12% -18.46% (p=0.000 n=10+10)
BitToggleConst-8 36.9ns ± 1% 23.8ns ± 3% -35.46% (p=0.000 n=9+10)
Fixes #45790
Update #45242
Change-Id: Ie33a72dc139f261af82db15d446cd0855afb4e59
Reviewed-on: https://go-review.googlesource.com/c/go/+/318149
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ben Shi <powerman1st@163.com>
Diffstat (limited to 'src/cmd/compile/internal/test')
-rw-r--r-- | src/cmd/compile/internal/test/bench_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/test/bench_test.go b/src/cmd/compile/internal/test/bench_test.go index 3fffe57d08..4724600091 100644 --- a/src/cmd/compile/internal/test/bench_test.go +++ b/src/cmd/compile/internal/test/bench_test.go @@ -62,3 +62,63 @@ func BenchmarkConstModify(b *testing.B) { } } } + +func BenchmarkBitSet(b *testing.B) { + const N = 64 * 8 + a := make([]uint64, N/64) + for i := 0; i < b.N; i++ { + for j := uint64(0); j < N; j++ { + a[j/64] |= 1 << (j % 64) + } + } +} + +func BenchmarkBitClear(b *testing.B) { + const N = 64 * 8 + a := make([]uint64, N/64) + for i := 0; i < b.N; i++ { + for j := uint64(0); j < N; j++ { + a[j/64] &^= 1 << (j % 64) + } + } +} + +func BenchmarkBitToggle(b *testing.B) { + const N = 64 * 8 + a := make([]uint64, N/64) + for i := 0; i < b.N; i++ { + for j := uint64(0); j < N; j++ { + a[j/64] ^= 1 << (j % 64) + } + } +} + +func BenchmarkBitSetConst(b *testing.B) { + const N = 64 + a := make([]uint64, N) + for i := 0; i < b.N; i++ { + for j := range a { + a[j] |= 1 << 37 + } + } +} + +func BenchmarkBitClearConst(b *testing.B) { + const N = 64 + a := make([]uint64, N) + for i := 0; i < b.N; i++ { + for j := range a { + a[j] &^= 1 << 37 + } + } +} + +func BenchmarkBitToggleConst(b *testing.B) { + const N = 64 + a := make([]uint64, N) + for i := 0; i < b.N; i++ { + for j := range a { + a[j] ^= 1 << 37 + } + } +} |