Skip to content

Commit 2e42a5a

Browse files
committed
Add Go compiler directive 'noescape' to assembly implementations
Without this directive Go considers that `data []byte` escapes from the function, and forces allocation on the heap even for values which can perfectly stay on the stack. This removes extra allocation when value passed in is not on the heap already. See also: golang/go#4099
1 parent 7f484ce commit 2e42a5a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

murmur128_decl.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@
22

33
package murmur3
44

5+
//go:noescape
6+
57
// Sum128 returns the murmur3 sum of data. It is equivalent to the following
68
// sequence (without the extra burden and the extra allocation):
79
// hasher := New128()
810
// hasher.Write(data)
911
// return hasher.Sum128()
1012
func Sum128(data []byte) (h1 uint64, h2 uint64)
1113

14+
//go:noescape
15+
1216
// SeedSum128 returns the murmur3 sum of data with digests initialized to seed1
1317
// and seed2.
1418
//
1519
// The canonical implementation allows only one uint32 seed; to imitate that
1620
// behavior, use the same, uint32-max seed for seed1 and seed2.
1721
func SeedSum128(seed1, seed2 uint64, data []byte) (h1 uint64, h2 uint64)
1822

23+
//go:noescape
24+
1925
// StringSum128 is the string version of Sum128.
2026
func StringSum128(data string) (h1 uint64, h2 uint64)
2127

28+
//go:noescape
29+
2230
// SeedStringSum128 is the string version of SeedSum128.
2331
func SeedStringSum128(seed1, seed2 uint64, data string) (h1 uint64, h2 uint64)

murmur32_decl.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
package murmur3
44

5+
//go:noescape
6+
57
// Sum32 returns the murmur3 sum of data. It is equivalent to the following
68
// sequence (without the extra burden and the extra allocation):
79
// hasher := New32()
810
// hasher.Write(data)
911
// return hasher.Sum32()
1012
func Sum32(data []byte) (h1 uint32)
1113

14+
//go:noescape
15+
1216
// SeedSum32 returns the murmur3 sum of data with the digest initialized to
1317
// seed.
1418
func SeedSum32(seed uint32, data []byte) (h1 uint32)
1519

20+
//go:noescape
21+
1622
// StringSum32 is the string version of Sum32.
1723
func StringSum32(data string) (h1 uint32)
1824

25+
//go:noescape
26+
1927
// SeedStringSum32 is the string version of SeedSum32.
2028
func SeedStringSum32(seed uint32, data string) (h1 uint32)

0 commit comments

Comments
 (0)