-
Notifications
You must be signed in to change notification settings - Fork 99
purego: add benchmarks for calling methods #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmc
wants to merge
2
commits into
ebitengine:main
Choose a base branch
from
tmc:add-benchmark
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2025 The Ebitengine Authors | ||
|
|
||
| package purego_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/ebitengine/purego" | ||
| "github.com/ebitengine/purego/internal/load" | ||
| ) | ||
|
|
||
| // BenchmarkCallingMethods compares RegisterFunc, SyscallN, and Callback methods | ||
| func BenchmarkCallingMethods(b *testing.B) { | ||
| testCases := []struct { | ||
| n int | ||
| fn any | ||
| fnPtr uintptr | ||
| cFnPtr uintptr | ||
| cFnName string | ||
| cCallbackPtr uintptr | ||
| cCallbackName string | ||
| args []uintptr | ||
| expectedSum int | ||
| }{ | ||
| {1, sum1, 0, 0, "sum1_c", 0, "call_callback1", []uintptr{1}, 1}, | ||
| {2, sum2, 0, 0, "sum2_c", 0, "call_callback2", []uintptr{1, 2}, 3}, | ||
| {3, sum3, 0, 0, "sum3_c", 0, "call_callback3", []uintptr{1, 2, 3}, 6}, | ||
| {5, sum5, 0, 0, "sum5_c", 0, "call_callback5", []uintptr{1, 2, 3, 4, 5}, 15}, | ||
| {10, sum10, 0, 0, "sum10_c", 0, "call_callback10", []uintptr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 55}, | ||
| {14, sum15, 0, 0, "sum14_c", 0, "call_callback14", []uintptr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 105}, | ||
| {15, sum15, 0, 0, "sum15_c", 0, "call_callback15", []uintptr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 120}, | ||
| } | ||
|
|
||
| // Build C library for benchmarking | ||
| libFileName := filepath.Join(b.TempDir(), "libbenchmark.so") | ||
| if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "benchmarktest", "benchmark.c")); err != nil { | ||
| b.Skipf("Failed to build C library: %v", err) | ||
| } | ||
| b.Cleanup(func() { | ||
| os.Remove(libFileName) | ||
| }) | ||
|
|
||
| libHandle, err := load.OpenLibrary(libFileName) | ||
| if err != nil { | ||
| b.Fatalf("Failed to load C library: %v", err) | ||
| } | ||
| defer func() { | ||
| if err := load.CloseLibrary(libHandle); err != nil { | ||
| b.Fatalf("Failed to close library: %s", err) | ||
| } | ||
| }() | ||
|
|
||
| // Create callbacks and load C functions | ||
| for i := range testCases { | ||
| testCases[i].fnPtr = purego.NewCallback(testCases[i].fn) | ||
|
|
||
| cFn, err := load.OpenSymbol(libHandle, testCases[i].cFnName) | ||
| if err != nil { | ||
| b.Fatalf("Failed to load C function %s: %v", testCases[i].cFnName, err) | ||
| } | ||
| testCases[i].cFnPtr = cFn | ||
|
|
||
| cCallbackFn, err := load.OpenSymbol(libHandle, testCases[i].cCallbackName) | ||
| if err != nil { | ||
| b.Fatalf("Failed to load C callback wrapper %s: %v", testCases[i].cCallbackName, err) | ||
| } | ||
| testCases[i].cCallbackPtr = cCallbackFn | ||
| } | ||
|
|
||
| b.Run("RegisterFunc/Callback", func(b *testing.B) { | ||
| for _, tc := range testCases { | ||
| b.Run(fmt.Sprintf("%dargs", tc.n), func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| registerFn := makeRegisterFunc(tc.n) | ||
| purego.RegisterFunc(registerFn, tc.fnPtr) | ||
|
|
||
| b.ResetTimer() | ||
| result := callRegisterFunc(registerFn, tc.n, tc.args, b.N) | ||
| b.StopTimer() | ||
|
|
||
| if int(result) != tc.expectedSum { | ||
| b.Fatalf("RegisterFunc/Callback: expected sum %d, got %d", tc.expectedSum, result) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Benchmark RegisterFunc with C functions | ||
| b.Run("RegisterFunc/CFunc", func(b *testing.B) { | ||
| for _, tc := range testCases { | ||
| b.Run(fmt.Sprintf("%dargs", tc.n), func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| registerFn := makeRegisterFunc(tc.n) | ||
| purego.RegisterFunc(registerFn, tc.cFnPtr) | ||
|
|
||
| b.ResetTimer() | ||
| result := callRegisterFunc(registerFn, tc.n, tc.args, b.N) | ||
| b.StopTimer() | ||
|
|
||
| if int(result) != tc.expectedSum { | ||
| b.Fatalf("RegisterFunc/CFunc: expected sum %d, got %d", tc.expectedSum, result) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Benchmark SyscallN with Go callbacks | ||
| b.Run("SyscallN/Callback", func(b *testing.B) { | ||
| for _, tc := range testCases { | ||
| b.Run(fmt.Sprintf("%dargs", tc.n), func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var result uintptr | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| result, _, _ = purego.SyscallN(tc.fnPtr, tc.args...) | ||
| } | ||
| b.StopTimer() | ||
| if int(result) != tc.expectedSum { | ||
| b.Fatalf("SyscallN/Callback: expected sum %d, got %d", tc.expectedSum, result) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Benchmark SyscallN with C functions | ||
| b.Run("SyscallN/CFunc", func(b *testing.B) { | ||
| for _, tc := range testCases { | ||
| b.Run(fmt.Sprintf("%dargs", tc.n), func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var result uintptr | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| result, _, _ = purego.SyscallN(tc.cFnPtr, tc.args...) | ||
| } | ||
| b.StopTimer() | ||
| if int(result) != tc.expectedSum { | ||
| b.Fatalf("SyscallN/CFunc: expected sum %d, got %d", tc.expectedSum, result) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Benchmark round-trip: Go → C → Go callback (realistic use case) | ||
| b.Run("RoundTrip", func(b *testing.B) { | ||
| for _, tc := range testCases { | ||
| b.Run(fmt.Sprintf("%dargs", tc.n), func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| // Build args: first arg is callback pointer, rest are the arguments | ||
| callbackArgs := make([]uintptr, len(tc.args)+1) | ||
| callbackArgs[0] = tc.fnPtr | ||
| copy(callbackArgs[1:], tc.args) | ||
|
|
||
| // Skip if total args (callback + args) exceeds or meets limit | ||
| // SyscallN has issues with exactly 15 or more arguments | ||
| if len(callbackArgs) >= 15 { | ||
| b.Skipf("Round-trip with %d args + callback (%d total) exceeds/meets SyscallN limit", tc.n, len(callbackArgs)) | ||
| } | ||
|
|
||
| var result uintptr | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| result, _, _ = purego.SyscallN(tc.cCallbackPtr, callbackArgs...) | ||
| } | ||
| b.StopTimer() | ||
| if int(result) != tc.expectedSum { | ||
| b.Fatalf("RoundTrip: expected sum %d, got %d", tc.expectedSum, result) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // makeRegisterFunc creates a function pointer of the appropriate signature | ||
| func makeRegisterFunc(n int) any { | ||
| switch n { | ||
| case 1: | ||
| return new(func(uintptr) uintptr) | ||
| case 2: | ||
| return new(func(uintptr, uintptr) uintptr) | ||
| case 3: | ||
| return new(func(uintptr, uintptr, uintptr) uintptr) | ||
| case 5: | ||
| return new(func(uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| case 10: | ||
| return new(func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| case 14: | ||
| return new(func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| case 15: | ||
| return new(func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // callRegisterFunc calls the registered function with the appropriate number of arguments | ||
| func callRegisterFunc(registerFn any, n int, args []uintptr, iterations int) uintptr { | ||
| var result uintptr | ||
| switch n { | ||
| case 1: | ||
| f := registerFn.(*func(uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0]) | ||
| } | ||
| case 2: | ||
| f := registerFn.(*func(uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1]) | ||
| } | ||
| case 3: | ||
| f := registerFn.(*func(uintptr, uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1], args[2]) | ||
| } | ||
| case 5: | ||
| f := registerFn.(*func(uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1], args[2], args[3], args[4]) | ||
| } | ||
| case 10: | ||
| f := registerFn.(*func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1], args[2], args[3], args[4], | ||
| args[5], args[6], args[7], args[8], args[9]) | ||
| } | ||
| case 14: | ||
| f := registerFn.(*func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1], args[2], args[3], args[4], | ||
| args[5], args[6], args[7], args[8], args[9], | ||
| args[10], args[11], args[12], args[13]) | ||
| } | ||
| case 15: | ||
| f := registerFn.(*func(uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr, uintptr) uintptr) | ||
| for i := 0; i < iterations; i++ { | ||
| result = (*f)(args[0], args[1], args[2], args[3], args[4], | ||
| args[5], args[6], args[7], args[8], args[9], | ||
| args[10], args[11], args[12], args[13], args[14]) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| //go:noinline | ||
| func sum1(a1 uintptr) uintptr { return a1 } | ||
|
|
||
| //go:noinline | ||
| func sum2(a1, a2 uintptr) uintptr { return a1 + a2 } | ||
|
|
||
| //go:noinline | ||
| func sum3(a1, a2, a3 uintptr) uintptr { return a1 + a2 + a3 } | ||
|
|
||
| //go:noinline | ||
| func sum5(a1, a2, a3, a4, a5 uintptr) uintptr { return a1 + a2 + a3 + a4 + a5 } | ||
|
|
||
| //go:noinline | ||
| func sum10(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 uintptr) uintptr { | ||
| return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 | ||
| } | ||
|
|
||
| //go:noinline | ||
| func sum15(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) uintptr { | ||
| return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15 | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2025 The Ebitengine Authors | ||
|
|
||
| // go:generate sh -c "if [ $(uname -s) = 'Darwin' ]; then cc -dynamiclib -O2 -o | ||
| // libnoop.dylib noop.c; else cc -shared -fPIC -O2 -o libnoop.so noop.c; fi" | ||
|
|
||
| long sum1_c(long a1) { return a1; } | ||
|
|
||
| long sum2_c(long a1, long a2) { return a1 + a2; } | ||
|
|
||
| long sum3_c(long a1, long a2, long a3) { return a1 + a2 + a3; } | ||
|
|
||
| long sum5_c(long a1, long a2, long a3, long a4, long a5) { | ||
| return a1 + a2 + a3 + a4 + a5; | ||
| } | ||
|
|
||
| long sum10_c(long a1, long a2, long a3, long a4, long a5, long a6, long a7, | ||
| long a8, long a9, long a10) { | ||
| return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10; | ||
| } | ||
|
|
||
| long sum14_c(long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8, long a9, long a10, long a11, long a12, long a13, long a14, long a15) { | ||
| return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + | ||
| a14 + a15; | ||
| } | ||
|
|
||
| long sum15_c(long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8, long a9, long a10, long a11, long a12, long a13, long a14, long a15) { | ||
| return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + | ||
| a14 + a15; | ||
| } | ||
| typedef long (*callback1_t)(long); | ||
| long call_callback1(callback1_t cb, long a1) { return cb(a1); } | ||
|
|
||
| typedef long (*callback2_t)(long, long); | ||
| long call_callback2(callback2_t cb, long a1, long a2) { return cb(a1, a2); } | ||
|
|
||
| typedef long (*callback3_t)(long, long, long); | ||
| long call_callback3(callback3_t cb, long a1, long a2, long a3) { | ||
| return cb(a1, a2, a3); | ||
| } | ||
|
|
||
| typedef long (*callback5_t)(long, long, long, long, long); | ||
| long call_callback5(callback5_t cb, long a1, long a2, long a3, long a4, long a5) { | ||
| return cb(a1, a2, a3, a4, a5); | ||
| } | ||
|
|
||
| typedef long (*callback10_t)(long, long, long, long, long, long, long, long, long, long); | ||
| long call_callback10(callback10_t cb, long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8, long a9, long a10) { | ||
| return cb(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); | ||
| } | ||
|
|
||
| typedef long (*callback14_t)(long, long, long, long, long, long, long, long, long, long, long, long, long, long); | ||
| long call_callback14(callback14_t cb, long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8, long a9, long a10, long a11, long a12, long a13, long a14) { | ||
| return cb(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); | ||
| } | ||
|
|
||
| typedef long (*callback15_t)(long, long, long, long, long, long, long, long, long, long, long, long, long, long, long); | ||
| long call_callback15(callback15_t cb, long a1, long a2, long a3, long a4, long a5, long a6, long a7, long a8, long a9, long a10, long a11, long a12, long a13, long a14, long a15) { | ||
| return cb(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.