purego: reduce allocations in RegisterFunc through caching and pooling#365
Draft
tmc wants to merge 4 commits intoebitengine:mainfrom
Draft
purego: reduce allocations in RegisterFunc through caching and pooling#365tmc wants to merge 4 commits intoebitengine:mainfrom
tmc wants to merge 4 commits intoebitengine:mainfrom
Conversation
Add benchmark tests comparing RegisterFunc, SyscallN, and callback performance across different argument counts (1, 2, 3, 5, 10, 14, 15 args). The benchmarks measure: - RegisterFunc with Go callbacks vs C functions - SyscallN with Go callbacks vs C functions - Round-trip calls (Go → C → Go callback) Includes corresponding C library with sum functions and callback wrappers to enable realistic performance comparisons between different calling approaches in purego.
Replace direct purego.Dlopen/Dlclose calls with load.OpenLibrary/CloseLibrary from the internal load package for consistency with other test code. Includes proper error handling in the defer cleanup function.
) Fix a critical bug in ARM64 struct argument packing where the register value (val) and class were not being reset after flushing due to alignment requirements. When packing struct fields into registers, if a field's alignment causes shift >= 64, the current register is flushed. However, the code was not resetting 'val' and 'class' after the flush, causing subsequent fields to be ORed with stale data from previous fields. Example bug with FourInt32s{1, 2, 3, 4}: - Fields 0,1 packed: val = 0x0000000200000001 - Flush at field 2 due to shift >= 64 - BUG: val still contains 0x0000000200000001 - Field 2 packs: val |= 3 becomes 0x0000000200000003 (should be 0x03) - Field 3 packs: val |= (4<<32) becomes 0x0000000400000003 - Result: field 3 = 6 instead of 4 (bit 1 from field 1 leaked) This fix ensures val and class are properly reset after each flush, preventing data corruption between register boundaries. Closes ebitengine#359
Optimize RegisterFunc performance by eliminating repeated allocations when handling stack arguments on darwin/arm64. Key improvements: - Cache struct types using type hashing to avoid recreating identical types - Pool struct instances for reuse across calls - Pre-compute field names array to eliminate strconv.Itoa allocations - Use sync.Map for concurrent-safe caching of types and pools These optimizations significantly reduce memory pressure in RegisterFunc when dealing with functions that have many arguments passed on the stack, particularly benefiting high-frequency call scenarios.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR optimizes
RegisterFuncperformance and adds comprehensive benchmarking infrastructure, plus fixes a critical ARM64 bug:Benchmark Results
Sample results (RegisterFunc/CFunc):