]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo/internal/test: benchmark for #cgo noescape directive
authordoujiang24 <doujiang24@gmail.com>
Sat, 26 Aug 2023 04:49:53 +0000 (04:49 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 28 Aug 2023 16:36:29 +0000 (16:36 +0000)
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: fc67d5ad7a1ba56025f2c142bb88cc4174fa3a27
GitHub-Pull-Request: golang/go#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>
src/cmd/cgo/internal/test/test.go

index 7da5a856b307a066862961fcf68a446a44e4bcdb..9a6c6d82cefa1abfde6c3a2dcc03d1ed08a6f479 100644 (file)
@@ -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;
@@ -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++ {