Skip to content

Rust: mutual tail-call wrappers take records by value - #1441

Merged
jasisz merged 2 commits into
mainfrom
fix/rust-mutual-tco-by-value
Sep 25, 2026
Merged

jasisz merged 2 commits into
mainfrom
fix/rust-mutual-tco-by-value

Conversation

@jasisz

@jasisz jasisz commented Sep 25, 2026

Copy link
Copy Markdown
Owner

btc-listener's orPump, orPumpAny and poppedFor (hearing/polling paths) are members of mutual tail-call groups; a Pool handed to them had its Maps copied on update.

Root cause

emit_mir_mutual_tco_block gives every member a public wrapper that borrows each non-copy param (should_borrow_param) and clones it into the trampoline's enum state. The caller's value is still alive during the call, so the Rc count of every Map in it is 2 and the first Map.set inside the group copies the whole Map, once per call. compute_owned_record_params skipped mutual members entirely, so callers never handed one their value either.

Decision

By-value params can flow through a mutual group: every trampoline arm already binds its params by value (Pump(mut pool, ...)), so the only borrow was the wrapper's, and it always ended in a clone. Taking the value in the wrapper is never more expensive: a caller at its last use moves it in, and a caller that keeps it clones at the call site instead of inside the wrapper. The group's invariants (params every member passes on unchanged, compute_resolved_rc_params) stay borrowed: the trampoline reads them through &T for the whole run and never clones them.

Fix

  • mutual_tco_value_params computes, per member, which params the wrapper takes by value (non-copy and not an invariant of its group), and stores it in rust_owned_record_params, which the rest of the backend already reads as "this callee position takes its argument by value".
  • The wrapper signature and the enum construction follow it (pub fn pump(pool: Pool, ..) { tramp(Pump(pool, ..)) }); callee_borrow_mask and the function-value adapter read the same facts, so every call site matches the signature. A wrapper whose group view of invariants differs from the emitted trampoline's still type-checks: an invariant it takes by value is passed as &x, and a param it borrows is cloned into the state, as before.
  • Callers that pass a record at its last use to such a member now count as consuming it, so they graduate to by value in turn.

Measurement

tests/fixtures/rust_mutual_tco_by_value: a loop hands a Pool holding a Map to pump/pumped once per turn; pump sets one key. Release build, one million entries, 100 turns: 0.64 s with the borrowing wrapper, 0.07 s by value; the borrowing one grows with the turns (about 6 ms each, one full Map copy), the by-value one does not. This was measured together with #1438, which lets the trampoline arms move the Map out of the record; without it the arm still clones pool.seen and the copy stays.

Tests

  • rust_work_spec::a_mutual_tail_call_member_takes_a_record_by_value: checks the by-value wrapper and the moving call, builds, compares with the VM.
  • Self-host regenerated (the eval, parser, resolver and match groups: 723 lines added, 841 removed, mostly wrapper clones gone); AVER_SELF_HOST_REGEN=1 cargo test --test rust_self_host_regen -- --ignored (build and corpus parity) passes.
  • Every entry in examples/, tests/fixtures, self_hosted/ with a main compiled to Rust and cargo checked: 152 pass, the same 6 as on main fail for unrelated reasons. btc-listener HEAD passes cargo check.
  • lib tests, rust_work_spec, own_param_*, rust_codegen_regression, compile_spec, shared_update_spec, verify_handle_params_spec, fmt, clippy.

jasisz and others added 2 commits September 25, 2026 10:11
The public function of a mutual tail-call group borrowed every record,
collection or other non-copy argument and cloned it into the trampoline
state. The caller's value lived on during the call, so the first in-place
update of a Map inside it (Map.set on pool.seen) copied the whole Map, once
per call. compute_owned_record_params skipped these members, so no caller
ever handed them its value.

The trampoline holds its params by value anyway, so the wrapper now takes
them by value too, except the group's invariants, which every member hands
on unchanged and the trampoline reads through a borrow for the whole run.
The facts go into rust_owned_record_params for the members, so call sites,
function-value adapters and the callers' own by-value graduation all read
the same decision: a caller at its last use moves the value in, one that
keeps it clones at the call instead of in the wrapper, which costs the
same.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…utual-tco-by-value

# Conflicts:
#	CHANGELOG.md
#	src/self_host/aver_generated/domain/eval/core/mod.rs
#	tests/rust_work_spec.rs
@jasisz
jasisz merged commit c4b0817 into main Sep 25, 2026
28 checks passed
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.

1 participant