aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/memmove_test.go
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2015-01-07 17:44:49 -0800
committerJosh Bleecher Snyder <josharian@gmail.com>2015-01-09 22:35:25 +0000
commitf03c9202c43e0abb130669852082117ca50aa9b1 (patch)
treebb8f7c8026fbbd79231e5e6ea108d2d1a380cecc /src/runtime/memmove_test.go
parent1d0c7792f172ab0485edf2818398deffe3d452ba (diff)
downloadgo-f03c9202c43e0abb130669852082117ca50aa9b1.tar.gz
go-f03c9202c43e0abb130669852082117ca50aa9b1.zip
cmd/gc: optimize memclr of slices and arrays
Recognize loops of the form for i := range a { a[i] = zero } in which the evaluation of a is free from side effects. Replace these loops with calls to memclr. This occurs in the stdlib in 18 places. The motivating example is clearing a byte slice: benchmark old ns/op new ns/op delta BenchmarkGoMemclr5 3.31 3.26 -1.51% BenchmarkGoMemclr16 13.7 3.28 -76.06% BenchmarkGoMemclr64 50.8 4.14 -91.85% BenchmarkGoMemclr256 157 6.02 -96.17% Update #5373. Change-Id: I99d3e6f5f268e8c6499b7e661df46403e5eb83e4 Reviewed-on: https://go-review.googlesource.com/2520 Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/memmove_test.go')
-rw-r--r--src/runtime/memmove_test.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/runtime/memmove_test.go b/src/runtime/memmove_test.go
index ffda4fe6c5..29c62cc37d 100644
--- a/src/runtime/memmove_test.go
+++ b/src/runtime/memmove_test.go
@@ -162,6 +162,20 @@ func BenchmarkMemclr256(b *testing.B) { bmMemclr(b, 256) }
func BenchmarkMemclr4096(b *testing.B) { bmMemclr(b, 4096) }
func BenchmarkMemclr65536(b *testing.B) { bmMemclr(b, 65536) }
+func bmGoMemclr(b *testing.B, n int) {
+ x := make([]byte, n)
+ b.SetBytes(int64(n))
+ for i := 0; i < b.N; i++ {
+ for j := range x {
+ x[j] = 0
+ }
+ }
+}
+func BenchmarkGoMemclr5(b *testing.B) { bmGoMemclr(b, 5) }
+func BenchmarkGoMemclr16(b *testing.B) { bmGoMemclr(b, 16) }
+func BenchmarkGoMemclr64(b *testing.B) { bmGoMemclr(b, 64) }
+func BenchmarkGoMemclr256(b *testing.B) { bmGoMemclr(b, 256) }
+
func BenchmarkClearFat8(b *testing.B) {
for i := 0; i < b.N; i++ {
var x [8 / 4]uint32