Rust: mutual tail-call wrappers take records by value - #1441
Merged
Merged
Conversation
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
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.
btc-listener's
orPump,orPumpAnyandpoppedFor(hearing/polling paths) are members of mutual tail-call groups; aPoolhanded to them had its Maps copied on update.Root cause
emit_mir_mutual_tco_blockgives 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 firstMap.setinside the group copies the whole Map, once per call.compute_owned_record_paramsskipped 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&Tfor the whole run and never clones them.Fix
mutual_tco_value_paramscomputes, per member, which params the wrapper takes by value (non-copy and not an invariant of its group), and stores it inrust_owned_record_params, which the rest of the backend already reads as "this callee position takes its argument by value".pub fn pump(pool: Pool, ..) { tramp(Pump(pool, ..)) });callee_borrow_maskand 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.Measurement
tests/fixtures/rust_mutual_tco_by_value: a loop hands aPoolholding a Map topump/pumpedonce per turn;pumpsets 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 clonespool.seenand 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.AVER_SELF_HOST_REGEN=1 cargo test --test rust_self_host_regen -- --ignored(build and corpus parity) passes.examples/,tests/fixtures,self_hosted/with amaincompiled to Rust andcargo checked: 152 pass, the same 6 as on main fail for unrelated reasons. btc-listener HEAD passescargo check.rust_work_spec,own_param_*,rust_codegen_regression,compile_spec,shared_update_spec,verify_handle_params_spec, fmt, clippy.