perf: zero-allocation FFI + ABI-safe struct layout (v0.5.4)#56
Merged
Conversation
Update benchmark table to show 0 allocs on Unix (//go:noescape). Document structs.HostLayout and noescape in architecture guide.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Jun 15, 2026
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
//go:noescapeonruntime_cgocalleliminates 1 heap allocation (208–248 bytes) per call on all Unix platforms.syscallArgsstays on goroutine stack. Expected 10–25% perf improvement (Add //go:noescape to runtime_cgocall to eliminate per-call allocation #54)structs.HostLayout(Go 1.23+) added to all 8 assembly-interface structs, guaranteeing C ABI memory layout against future Go compiler optimizations (Use Go 1.23's structs.HostLayout in ABI-boundary structures #55)Details
Issue #54:
//go:noescape3 files changed — added
//go:noescapebetween//go:linknameandfunc runtime_cgocall:internal/syscall/syscall_unix_amd64.gointernal/syscall/syscall_arm64.gointernal/dl/dl_unix.goSafety verified:
runtime.asmcgocallitself has//go:noescapeinruntime/stubs.go:310. purego lacks this — compensates withsync.Pool.Issue #55:
structs.HostLayout8 structs across 5 files —
_ structs.HostLayoutas first field (zero-sized, no offset change):syscallArgs(AMD64)internal/syscall/syscall_unix_amd64.gosyscallArgs(ARM64)internal/syscall/syscall_arm64.godlopenArgs/dlsymArgs/dlerrorArgsinternal/dl/dl_unix.gocallbackArgs(AMD64)ffi/callback.gocallbackArgs(ARM64)ffi/callback_arm64.goG,ThreadStartinternal/fakecgo/libcgo.goIssue #55 listed 4 structs — we found 8 (the highest-risk ones were missing).
Test plan
go fmt ./...— cleango build ./...— cleango test ./...— all passgo vet ./...— only knowndl_windows.gofalse positivesgolangci-lint run— 0 issues (exit 7 = Go 1.25 noise)go test -bench=. -benchmem ./ffi) — expecting 0 allocs/opCloses #54, closes #55