Skip to content

Instantly share code, notes, and snippets.

@lzap
Created May 15, 2025 11:14
Show Gist options
  • Save lzap/edfd4b0316df5c86e7f0b9d7523124b6 to your computer and use it in GitHub Desktop.
Save lzap/edfd4b0316df5c86e7f0b9d7523124b6 to your computer and use it in GitHub Desktop.
Pass Go array by value or reference
package main
import "testing"
type type16 [16]byte
//go:noinline
func val16(v type16) {}
//go:noinline
func ref16(v *type16) {}
type type32 [32]byte
//go:noinline
func val32(v type32) {}
//go:noinline
func ref32(v *type32) {}
type type64 [64]byte
//go:noinline
func val64(v type64) {}
//go:noinline
func ref64(v *type64) {}
type type128 [128]byte
//go:noinline
func val128(v type128) {}
//go:noinline
func ref128(v *type128) {}
//go:noinline
func Benchmark(b *testing.B) {
var v16 type16
b.Run("16 by val", func(b *testing.B) {
for i := 0; i < b.N; i++ {
val16(v16)
}
})
b.Run("16 by ref", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ref16(&v16)
}
})
var v32 type32
b.Run("32 by val", func(b *testing.B) {
for i := 0; i < b.N; i++ {
val32(v32)
}
})
b.Run("32 by ref", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ref32(&v32)
}
})
var v64 type64
b.Run("64 by val", func(b *testing.B) {
for i := 0; i < b.N; i++ {
val64(v64)
}
})
b.Run("64 by ref", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ref64(&v64)
}
})
var v128 type128
b.Run("128 by val", func(b *testing.B) {
for i := 0; i < b.N; i++ {
val128(v128)
}
})
b.Run("128 by ref", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ref128(&v128)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment