Skip to content

feat: stable zero-copy FFI buffer ABI (#737) - #1347

Merged
godofecht merged 3 commits into
mainfrom
cursor/zero-copy-ffi-buffer-a9dc
Oct 5, 2026
Merged

godofecht merged 3 commits into
mainfrom
cursor/zero-copy-ffi-buffer-a9dc

Conversation

@godofecht

@godofecht godofecht commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Linked issue: #737 (first useful slice only).

Define a stable zero-copy buffer ABI for compatible contiguous pointer/span/array data, with explicit borrowed vs owned lifetime, fail-closed layout checks, a structural no-copy proof, and a short contract note. This does not include the full Python/native-library audit or the #728 end-to-end suite.

#1277 already landed constructors and timing rows. Those did not check layout, did not fail closed, and did not prove the pointer was reused. This slice fills that gap instead of opening a second ABI.

Overlap check

  • I searched open pull requests and recent commits for overlapping work.
  • This does not duplicate another active implementation, or the relationship is explained below.

Overlap / dependency notes:

Open PRs #1332, #1331, #1330, #1329, #1328, #1326, #1325, #1268, #1267, #1265 are affine/AoSoA/async-copy/opt-level/register-tile/startup/I-O/baseline work. None of them implement the #737 buffer contract. Merged #1277 is the stub this change extends.

Evidence

Baseline before this change:

On 9a278876, FFIBuffer was a Flow struct with borrowed/owned flags and optional instrumentation. tests/lang/test_ffi_buffer.flow only checked those fields. flow_ffi.h had no buffer type. Incompatible layout had no status and no refusal to copy.

Targeted proof that exercises the changed behaviour:

./flow test-lang tests/lang/test_ffi_buffer.flow
# All 1 language tests passed

./flow run benchmarks/micro/ffi_boundary_benchmark.flow
# zero-copy handoff pointer identity: PASS (copied_bytes=0)

cc -std=c11 -Wall -Wextra -Werror -c runtime/flow_ffi_buffer.h
# header compiles standalone

./flow tool scripts/check_pkg_parity.flow
# ffi cases PASS (goldens refreshed for the package file sizes)

Why that proof would detect the old failure or regression:

The old test passed if the struct fields were set, even if the implementation copied bytes. The new test fails unless buf.data is the caller's pointer and a write through the view mutates the original storage. The C helper in tests/lang/ffi_buffer_abi_helper.h includes runtime/flow_ffi_buffer.h and must return the same address. A silent copy path would also fail the incompatible-layout cases (null view, unchanged source, zero copy-bytes).

Repository gate(s) run:

  • ./flow test-lang tests/lang/test_ffi_buffer.flow — pass
  • ./flow run benchmarks/micro/ffi_boundary_benchmark.flow — identity row pass
  • ./flow tool scripts/check_pkg_parity.flow — ffi goldens updated and passing
  • Standalone compile of runtime/flow_ffi_buffer.h — pass

Generated/bootstrap state, if any:

None. Compiler sources that feed bootstrap C were not edited.

Risk

Compatibility, performance, security, lifetime, backend, or release implications:

FFIBuffer gains item_align, dealloc, and dealloc_ctx (replacing deallocator). That is a package 0.1.0 layout change, documented in the changelog. The C header is additive. No compiler lowering change: spans/arrays still lower as today; callers form the view explicitly.

  • No unresolved language/API design decision is being implemented implicitly.
  • No unrelated generated artifacts or local state are included.
  • Docs changed when user-visible behaviour or syntax changed.
  • docs/project/Questions.md updated if this needs a language-design decision.

Compatibility

  • No user-visible compatibility change.
  • Compatibility change is documented in docs/project/CHANGELOG.md.

FFIBuffer field layout changed; existing ffi_buffer_borrowed / ffi_buffer_owned signatures are unchanged.

Agent handoff

Cursor cloud agent. Remaining #737 work (Python/native audit, #728 harness rows, break-even measurements on real crossings) stays on the issue.

Open in Web Open in Cursor 

cursoragent and others added 2 commits October 5, 2026 02:04
Define a pointer/span/array handoff contract with borrowed vs owned
lifetime, fail-closed layout checks that never silently copy, a
pointer-identity lang test, and a microbenchmark identity row.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht marked this pull request as ready for review October 5, 2026 02:06
The wiki builder requires every page under docs/ to appear in
docs/nav.json. Put the zero-copy FFI buffer contract next to the
export ABI under Language → Targets & interop.

Co-authored-by: Abhishek Shivakumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants