Repository navigation
perf(ffi): Go's calls pass C no pointer to a Go local - #837
Conversation
Quest-Claim: 73fba301d12cd82c
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-pointers # Conflicts: # ffi/src/go.rs
…-pointers # Conflicts: # ffi/src/go.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
ℹ️ No correctness issues found. One minor suggestion inline.
Reviewed changes
I reviewed the full diff: the Go exporter's new generic cgo shim, and every call site moved onto it.
- One shim for every pointer-passing call:
checked_c/call_checkedbecomeGen::call/call_sig. Any signature with anIn,Out, orInOutparameter goes through<prefix>GoCall_<name>. That shim takes values by copy and returns what C writes from a zeroed C local, alone or in<prefix>GoReturned_<name>. - Go call sites rebind results to the old local names: plain calls, eager and lazy futures (make/start/poll), the
String/GoString/Clone/Equaltraits, and a checkeddrop_all bind the shim's results to the names they used before (c_status_,c_<n>_,f_,c_self_write-back), so the code after each call is unchanged. - The lazy make frees only on success: this is equivalent, since a failed check's
wrapreturns a null future. - The quest is finished:
quest/m1/ffi-speed/go-pointers.mdis deleted, and nothing links to it.
I checked the one shape that could emit an unused c_ret_.value statement: a non-void return with outs and no error or status. The ABI produces it only with written &mut outs, and c_out_ := {call} already handles that case. No &c_*/&f_ Go-local pointer remains in go.rs.
claude-opus-5-5 | 𝕏
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
I reviewed what changed since the prior Pullfrog review (c46f3a0).
- Doc comment restored:
out_name's/// The C name of the out value…line is back onout_name, andthenkeeps only its own doc. This addresses the prior finding.
claude-sonnet-5-5 | 𝕏
Its other children landed in #837, #838, and #849, so deleting shared-borrow.md would leave the epic with no children. Per the maintainer's decision, delete its README, rewrite the links to it, and replace bench/ffi's link to the deleted go-pointers.md with #837. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Finishes quest/m1/ffi-speed/go-pointers.md: a generated Go call no longer passes C a pointer to a Go local, so cgo moves none of them to the heap.
Summary
kixel-ai/rust2#829 gave each checked call a C shim in the package's cgo preamble that kept the status in a C local. This PR extends that shim to every C call whose parameters include an
In,Out, orInOutpointer, checked or not, and replaceschecked_c/call_checkedwith onecall. Each such call now goes through<prefix>GoCall_<name>, which:In: a value receiverc_self_,Equal'sc_other_) by value, and passes C a pointer to its own copy;Out:c_out_,c_out<k>_, a&mutparam's written value,c_err_, the status, a future's handlef_) in a zeroed C local and returns it;InOut(&mut selfof a value) holds after the call, which Go writes back to*self.A call with one result returns it by itself, and one with more returns them in a
<prefix>GoReturned_<name>struct. Go binds each result to the local the old code passed by pointer, so the rest of each call site stays as it was. A C local never moves, and cgo's wrapper writes the returned value into the goroutine's stack even after a callback moved that stack. So the shim works no matter when C writes, including after a callback, like #829's status.The C locals start zeroed so Go never reads a stale word as a pointer where C wrote nothing, as when a poll pends or a check fails. The GC scans those fields.
Every Go call site goes through it: functions and methods, eager and lazy futures (make, start, poll), the
String/GoString/Clone/Equaltraits, and a checked type'sdrop_. Calls that pass no such pointer emit the same code as before.The C ABI, the Rust glue, and every generated Go API are unchanged.
Validation
go build -a -gcflags=-m ./...), before → after:c_self_201 → 0,c_out_71 → 0,c_other_65 → 0,c_err_35 → 0,f_20 → 0,c_out0_15 → 0,c_out1_14 → 0.just ffi moqafter merging main: noc_*_orf_moves left.v(the*TanOptionresult lifts to),s/bin runtime helpers, andk*_(thecgo.Handlea lent closure or reader passes as its data; see Decisions).just ffi test(api fixtures in every language, includingapi_go's script undergo vet),just ffi shapes(the nightly round trip of every sampled type shape in every language:shapes::nightly::gook),just ffi moq(moq's export through C, C++, Kotlin, Python, Swift, Go, and Dart), andjust ci. All pass, re-run after each merge of main (Shared borrows that don't contend #834, feat(ffi)!: lossless binding types #832).libmoq.aand ran alternately on a host shared with other agents (load average ~10-13 on 32 threads).A focused probe (the bench's calls alone, 20 interleaved rounds of 200k calls, medians, heap allocations per call from
runtime.MemStats):TimestampFromMicrosThe bench's own Go program (
ffi/moq/bench/export.go), 12 interleaved rounds pinned to the same 8 CPUs, wall-time medians: record 65.2 → 62.2, object 243.8 → 229.5, stamped 331.7 → 317.4. The write row is bimodal on this host, with IQRs of 234-420 on both builds, so it cannot resolve a 20 ns change. An earlier unpinned 10-round pass was just as noisy: stamped read +25% there, while call and getter, whose code did not change, moved 3-4%. The allocation counts are the measurement that doesn't depend on load.Object and record return already passed C no Go-local pointer after perf(ffi): Go's checked calls keep their status in C #829, which moved their status into C. So they do not move here; their 3 allocations are the object and its cleanup, which object-ownership owns.
Decisions
c_self_/c_out_/c_other_(most of the 201/71/65) on the heap, though the Goal counts them.= {0}). Rejected: leaving them uninitialized, as the Go side would then copy stack garbage into fields the GC scans as pointers whenever C wrote nothing (a pending poll, a failed check).c_out_ := c_ret_.p2), and fields are named by position (p<i>, plusvalueandstatus). Rejected: naming fields after the C parameter, which could collide withvalue/statusor a Go keyword under cgo's renaming.if c_status_ == 0 { free }). Before, the code freed whatever the shim returned. Rejected: binding the status and leaving it unused, which Go refuses to compile.cgo.Handle(k*_) still moves to the heap. It is outside the Goal's list. Moving it into C means special-casingTy::Data, and thecgo.NewHandlebeside it costs far more than one word on the heap.uintptrhide (checkptr refuses it under-race) and#cgo noescape(it applies only besidenocallback, and these calls can call back).Public API
None. The generated Go API, the C ABI, and the Rust glue are unchanged. Only the cgo preamble's private shims and the generated function bodies change.
Caused by
None: this is a speedup, not a fix. The escapes are as old as the Go exporter.
Follow-ups
None.
Models
Size: M
Implementer: Claude Opus 5.5, claude-opus-5-5, medium effort
🤖 Generated with Claude Code
(Written by Claude Opus 5.5 with medium effort)
Review
Rewritten by
quest reviewafter each round.Head: cc28423
Base: origin/main at 72880f4
Risk: medium,
the base has no RISK.md to name what is always high or customer-facing, so every change is mediumClassified: medium by default
Reviewer: Codex, gpt-6.1-sol, high effort
Session:
01a1e0cd-5a60-77f3-b393-02ca219105e3, round 2Score: 8/10,
The incremental change correctly relocates a documentation comment and introduces no actionable defects; builds and tests remain unrun, as instructed.Rounds: 2; first round 0; findings 0: fixed 0, refuted 0, open 0
Findings: none
Summary by CodeRabbit
Summary by CodeRabbit