Skip to content

Commit

Permalink
cmd/cgo/internal/test: benchmark for #cgo noescape directive
Browse files Browse the repository at this point in the history
case: passing a single Go string object to C function.
result: 87 ns vs 61 ns.

BenchmarkCgoCall/string-pointer-escape
BenchmarkCgoCall/string-pointer-escape-12        67731663   87.02 ns/op
BenchmarkCgoCall/string-pointer-noescape
BenchmarkCgoCall/string-pointer-noescape-12    99424776   61.30 ns/op

For #56378

Change-Id: Iff5c69d8deedfa248f5d7399e1921a5cb0dc8b16
GitHub-Last-Rev: fc67d5a
GitHub-Pull-Request: #62297
Reviewed-on: https://go-review.googlesource.com/c/go/+/522939
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
  • Loading branch information
doujiang24 authored and gopherbot committed Aug 28, 2023
1 parent faf5646 commit 55db175
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/cmd/cgo/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ int add(int x, int y) {
return x+y;
};
// escape vs noescape
#cgo noescape handleGoStringPointerNoescape
void handleGoStringPointerNoescape(void *s) {}
void handleGoStringPointerEscape(void *s) {}
// Following mimics vulkan complex definitions for benchmarking cgocheck overhead.
typedef uint32_t VkFlags;
Expand Down Expand Up @@ -1106,6 +1113,18 @@ func benchCgoCall(b *testing.B) {
C.handleComplexPointer(&a0)
}
})
b.Run("string-pointer-escape", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var s string
C.handleGoStringPointerEscape(unsafe.Pointer(&s))
}
})
b.Run("string-pointer-noescape", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var s string
C.handleGoStringPointerNoescape(unsafe.Pointer(&s))
}
})
b.Run("eight-pointers", func(b *testing.B) {
var a0, a1, a2, a3, a4, a5, a6, a7 C.VkDeviceCreateInfo
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 55db175

Please sign in to comment.