Skip to content

perf: elide record-update copies in generated C - #1338

Merged
godofecht merged 3 commits into
mainfrom
cursor/copy-elision-732-0ef9
Oct 4, 2026
Merged

godofecht merged 3 commits into
mainfrom
cursor/copy-elision-732-0ef9

Conversation

@godofecht

@godofecht godofecht commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Linked issue or roadmap item: Part of #732 (related #696, parent #727).

What changed, and why is this the smallest useful change?

Record updates Name { ..base, f: v } lowered to a GNU statement expression that copied base into __flowc_sl and then copied that temporary into the destination. That is one avoidable full-struct copy on every let-bound or assigned update.

This slice constructs the update in place when the destination is distinct or the base is dead:

  • let q = Name { ..base, f: v } → T q = base; q.f = v;
  • q = Name { ..base, f: v } when no update value reads q → q = base; q.f = v; (or just the field writes when q is the base)
  • return Name { ..local, f: v } when local is a function-scoped binding, is not captured, and no update value reads it → mutate local and return it

Expression-position updates (call arguments, values that read the destination) still use the temporary, so value semantics and overwrite-dependence stay intact. No new syntax.

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: No open PR implements #732. The Python reference is archive/fix/732-copy-elision (let-bound in-place) and archive/perf/696-move-inference (returned last-use). This ports both conservative cases into flowc.

Evidence

Baseline before this change: record_update goldens required __flowc_sl temps for let-bound updates. let q = Point { ..p, y: 40 } emitted Point q = ({ Point __flowc_slN = (p); __flowc_slN.y = 40; __flowc_slN; }); (two copies).

Targeted proof that exercises the changed behaviour:

  • ./flow tool tests/cgen/run.flow copy_elision record_update — PASS (structural C goldens plus exit 0)
  • ./flow test-lang tests/lang/test_copy_elision.flow — PASS (value semantics: base unchanged; dest==base swap still sees old fields)

Why that proof would detect the old failure or regression: the new goldens require T q = (p); q.f = v; / p.f = v; return p; and forbid the extra temporary on those paths. The language test fails if in-place reuse clobbers a field that an update still needs (the left-rotation class of bug from the issue).

Repository gate(s) run:

  • env -u FLOWC_IN -u FLOWC_OUT "$(./flow tool compiler/scripts/flowc_host.flow)" — flowc: PASS
  • ./compiler/scripts/bootstrap_from_c.sh --regen — fixed point at gen2
  • ./compiler/scripts/bootstrap_from_c.sh --verify — PASS
  • ./flow tool tests/cgen/run.flow — 114 passed, 0 failed
  • ./flow test-lang tests/lang/test_copy_elision.flow tests/lang/test_structs.flow tests/lang/test_struct_literal_return.flow tests/lang/test_struct_lit_array_field.flow — all passed

Generated/bootstrap state: compiler/bootstrap/flowc_stage_a.c regenerated in a separate commit after the source edit.

Risk

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

In-place writes are withheld unless a conservative name-walk shows the destination is not read by an update value. Last-use return also refuses captured names, module statics, pending defer, and lambda bodies. Remaining #732 work (sret/destination-passing ABI, overwrite-dependence certification for cyclic same-storage construction, copy-count metrics for #728, MLIR parity) is intentionally out of scope.

  • 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.

Agent handoff

Remaining #732 work after this slice:

  1. sret / hidden destination-pointer ABI for large struct returns and arguments (caller slot, no extra materialization).
  2. Overwrite-dependence certification so same-storage construction can proceed when the slot graph is acyclic (issue comment on [Performance #727] Elide aggregate/value copies after move inference in generated C #732).
  3. Copy-count instrumentation and a [Performance] Build a cross-dimensional Python comparison harness and required regression gates #728 metric.
  4. Same elisions in the MLIR emitter (mlirgen.flow still insertvalue-copies the base).
  5. Broader last-use / uniqueness beyond record updates (arrays, closure environments, matrices).
Open in Web Open in Cursor 

cursoragent and others added 2 commits October 4, 2026 18:40
A `Name { ..base, f: v }` used to lower to a statement-expression temporary
that copied the base and then copied that temporary into the destination.
Construct into the destination when it is safe: a new let binding, an
assignment whose update values do not read the target, or a last-use return
of a local/parameter base. Expression position still uses the temporary.

Part of #732.

Co-authored-by: Abhishek Shivakumar <[email protected]>
Self-emit from compiler/src until the C reaches a fixed point at gen2
so bootstrap_from_c.sh --verify matches the #732 in-place lowering.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht marked this pull request as ready for review October 4, 2026 18:48
@godofecht
godofecht merged commit ad8e50c into main Oct 4, 2026
35 checks passed
cursor Bot pushed a commit that referenced this pull request Oct 4, 2026
cgen.flow auto-merged: n-way string concat and record-update copy
elision both remain. Bootstrap C regenerated to the combined fixed
point after #1338.

Co-authored-by: Abhishek Shivakumar <[email protected]>
@godofecht
godofecht deleted the cursor/copy-elision-732-0ef9 branch October 12, 2026 23:02
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