Skip to content

Commit

Permalink
Mark []byte passed to Sum64 as noescape
Browse files Browse the repository at this point in the history
The compiler cannot tell if arguments passed to an asm function escape
so it assumes that they always do. Mark arguments passed to Sum64 as
noescape so any temporary created from a []byte(string) conversion does
not escape.

% benchstat {old,new}.txt
name          old time/op    new time/op    delta
StringHash-4    80.8ns ± 4%    20.8ns ± 0%   -74.25%   (p=0.000 n=10+9)

name          old alloc/op   new alloc/op   delta
StringHash-4     16.0B ± 0%     0.0B ±NaN%  -100.00%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
StringHash-4      1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.000 n=10+10)
  • Loading branch information
davecheney committed Sep 14, 2016
1 parent 883fc5c commit 1ce1061
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions xxhash_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package xxhash

//go:noescape
func sum64(b []byte) uint64

func writeBlocks(x *xxh, b []byte) []byte
12 changes: 12 additions & 0 deletions xxhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ import (
"github.com/spaolacci/murmur3"
)

var result uint64

func BenchmarkStringHash(b *testing.B) {
const s = "abcdefghijklmnop"
var r uint64
b.ReportAllocs()
for n := 0; n < b.N; n++ {
r = Sum64([]byte(s))
}
result = r
}

func TestSum(t *testing.T) {
for i, tt := range []struct {
input string
Expand Down

0 comments on commit 1ce1061

Please sign in to comment.