Repository navigation
perf: elide record-update copies in generated C - #1338
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 4, 2026 18:48
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]>
7 of 8 tasks
This was referenced Oct 4, 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.
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 copiedbaseinto__flowc_sland 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 readsq→q = base; q.f = v;(or just the field writes whenqis the base)return Name { ..local, f: v }whenlocalis a function-scoped binding, is not captured, and no update value reads it → mutatelocaland return itExpression-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
Overlap / dependency notes: No open PR implements #732. The Python reference is
archive/fix/732-copy-elision(let-bound in-place) andarchive/perf/696-move-inference(returned last-use). This ports both conservative cases into flowc.Evidence
Baseline before this change:
record_updategoldens required__flowc_sltemps for let-bound updates.let q = Point { ..p, y: 40 }emittedPoint 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 plusexit 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 passedGenerated/bootstrap state:
compiler/bootstrap/flowc_stage_a.cregenerated 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.docs/project/Questions.mdupdated if this needs a language-design decision.Compatibility
docs/project/CHANGELOG.md.Agent handoff
Remaining #732 work after this slice:
mlirgen.flowstill insertvalue-copies the base).