Created
June 5, 2025 16:02
-
-
Save anderseknert/14e60451a9cfadaa08f635a74b4879fc to your computer and use it in GitHub Desktop.
Benchmark the cost of copying BuiltinContext passed to function vs not doing it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func BenchmarkBuiltinContextCopyCost(b *testing.B) { | |
b.Run("withBctx", func(b *testing.B) { | |
b.ResetTimer() | |
b.ReportAllocs() | |
for range b.N { | |
bctx := BuiltinContext{} | |
withBctx(bctx, "bctx") | |
} | |
}) | |
b.Run("withBctxPointer", func(b *testing.B) { | |
b.ResetTimer() | |
b.ReportAllocs() | |
for range b.N { | |
withBctxPointer(nil, "bctx") | |
} | |
}) | |
b.Run("withoutBctx", func(b *testing.B) { | |
b.ResetTimer() | |
b.ReportAllocs() | |
for range b.N { | |
withoutBctx("bctx") | |
} | |
}) | |
} | |
//go:noinline | |
func withBctx(_ BuiltinContext, s string) { | |
if s != "bctx" { | |
panic("bctx should not be used in this test") | |
} | |
} | |
//go:noinline | |
func withBctxPointer(_ *BuiltinContext, s string) { | |
if s != "bctx" { | |
panic("bctx should not be used in this test") | |
} | |
} | |
//go:noinline | |
func withoutBctx(s string) { | |
if s != "bctx" { | |
panic("bctx should not be used in this test") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goos: darwin | |
goarch: arm64 | |
pkg: github.com/open-policy-agent/opa/v1/topdown | |
cpu: Apple M4 Pro | |
BenchmarkBuiltinContextCopyCost/withBctx-12 307583601 3.834 ns/op 0 B/op 0 allocs/op | |
BenchmarkBuiltinContextCopyCost/withBctxPointer-12 1000000000 0.9629 ns/op 0 B/op 0 allocs/op | |
BenchmarkBuiltinContextCopyCost/withoutBctx-12 1000000000 0.9621 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok github.com/open-policy-agent/opa/v1/topdown 4.103s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment