Repository navigation
Parse Cargo.lock once in a hosted Cargo scan (#761) - #2138
Conversation
The hosted Cargo rewrite parsed Cargo.lock several times for every patched crate: for the other-versions probe, the dependents check, the override check and the plan, each time over the whole lock as rewritten so far. #761 measured that at 79% of the instructions of a hosted scan of a 600-crate project with 15 patches. The rewrite now holds the lock in one CargoHostedLock: the text, one parse of it made on first use, and the repoints committed so far as splices into that text, applied once when the rewrite ends. Its probes and plans answer as a fresh parse of the lock as rewritten so far would: - A repoint that changes only the package's own block writes the new source and checksum into the model. That is every repoint of a lock in the format cargo has written since 1.41, unless a dependent names the package by its full id. - Any other repoint (a v1 lock's [metadata] line, a full id in another block) is applied to the text at once, and the text is parsed again on its next use. - A plan is made on a fresh parse when the bytes it would read have moved: for a package this rewrite has already repointed, and for a rewrite with a splice that touches a pending one. A rewrite that never reads the lock does not parse it. plan() gives the repoint as a value and commit() takes it and returns its edits, so no edit is recorded for a repoint that was not made. No behaviour change intended. A seeded sweep compares every plan, edit, probe answer and final text with the crate-by-crate rewrite, and two order tests cover a later crate whose refusal depends on an earlier repoint; both pass on the old rewrite too. Closes #761. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR SummaryMedium Risk Overview
The hosted redirect path wires probes through Reviewed by Cursor Bugbot for commit 6b436a9. Configure here. |
|
[agent] PR shepherd: review brief for the approver What it does Risk: medium. This is the core hosted Cargo rewrite path, and the cached model has to match what a fresh parse of the partly rewritten lock would give. The PR handles that conservatively: a block-only ("local") repoint updates the cached Look here
Verified
Changes I made: none. Open questions: none blocking. The author left one asymmetry on purpose: Generated by Claude Code |
Third review round of the one-parse rewrite. - A planned repoint borrows the lock until it is committed or dropped, so no plan can come between the two and a commit cannot be stale. It had a silent no-op path for that case. - A repoint is kept on the held parse only when it replaces the block's source and checksum values where they stand. One that adds a line can collide with a key that is a table and leave a lock that does not parse. That one is applied and parsed again, so the crates after it are refused as they were. - A package's earlier repoint is checked before its plan is made, not after. - splice() builds the text in one pass. Tests: the sweep compares the whole model at every step, and a count of CargoLock::parse pins one parse through the Cargo rewrite itself and none for a rewrite refused before it reads the lock. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Closes #761.
Problem
scan --mode hostedon a Cargo project parsedCargo.lockseveral times for every patched crate: for the other-versions probe, for the dependents check, for the override check and the plan, and again on the refusal paths. Each parse reads the whole lock, by then partly rewritten. #761 measured it at 79% of the instructions of a hosted scan on the benchmark fixture (600 crates, 15 patched).Fix
The rewrite holds the lock in one
CargoHostedLock(formats/cargo/hosted.rs): the text, one parse of it made on first use, and the repoints committed so far as splices into that text. The splices are applied once, when the rewrite ends.The probes and the plans have to answer as a fresh parse of the lock as rewritten so far would. Three rules keep that:
sourceandchecksumvalues writes the two into the model. That is every repoint of a registry crate in a lock in the format cargo has written since 1.41, unless a dependent names the package by its full id (cargo spells it that way when a name and version are ambiguous).[metadata]line, a full id in another block) is applied to the text at once, and the text is parsed again on its next use.A rewrite that never reads the lock does not parse it: an offline-source refusal, a floor refusal, an invalid override.
planreturns the repoint as a value that holds the lock until it is committed or dropped, so no other plan can come between the two. Committing it returns the edits to record, so no edit is recorded for a repoint that was not made.Result
sp-bench compareon the benchmark fixture (600 crates, 15 patched),perfprofile, on an arm64 Mac. Head is this branch atc6f144325. Base is the rewrite as it was at59823ff46, one commit before the branch's base; the commit between them (#2100) touches no Cargo code.cargo/hostedcargo/rescanA second run gave -42.4% and -51.0%.
What is left of the lock in a hosted scan is the inventory's own parse (
formats::cargo::read_packages, 12% in #761's profile), which this does not touch. A v1 lock, a lock where dependents name the patched crates by full id, and a block that gets asourceorchecksumline it did not have are parsed once per such repoint, where they were parsed several times.No behaviour change intended
a_rewrite_on_one_parse_matches_the_crate_by_crate_rewriteruns seeded locks through runs of up to eight repoints, with repeats, re-pins to a second index, absent crates and dropped plans. At every step it compares the whole model, what the probes read, the plan and its edits with a fresh parse of the text the crate-by-crate rewrite has at that point, and at the end the text. The locks are 600 from the existing generator (v1 and v3/v4, with its hostile shapes), 1,500 from a new one whose full ids name the lock's own packages (twins, packages already at a hosted index, stray ids, stray[metadata]lines), and the hand-written ones.cargo_rewrite_order_testsholds two rewrites where a later crate's refusal depends on what an earlier one did to the lock. They pass on the rewrite as it was (I ran them against the three changed files at59823ff46) and on this branch.redirect_golden), the restore golden (upstream_restore_golden) and the per-plan goldens (span_splice_matches_golden_*) pass unchanged.a_rewrite_parses_the_lock_oncepins the count on the lock holder: one parse for 29 repoints of a 200-crate lock, one for a re-run that mixes repoints with crates already at their index, two when one package is planned twice.the_rewrite_parses_the_lock_oncepins it through the Cargo rewrite itself, by countingCargoLock::parse: one parse for four repoints, none for a rewrite refused before it reads the lock.Review
/code-review highran three times: on the first version (ten findings), on the rework that followed (nine), and on this PR as opened (eight, none on a lock cargo writes). The fixes for the third round were not reviewed again. The tests above are what checks them.Fixed from the first round:
sourcethat a first crate's pin gave one was still read as a path package by the dependents check of a second crate, and the override check of a crate named by two patches read its source as still crates.io. Tests: the two incargo_rewrite_order_tests.committook any splices. It takes the repointplanreturned, which onlyplancan make.Option<&CargoLock>meant two things in the dependents check. It takes the lock holder.HostedLock, and two doc links to a function that had become test-only.Fixed from the second round:
[metadata]line for the id the repoint gives the package ends up with a duplicate key. Every crate used to parse the lock, so the next one was refused. On one parse the crates after it were repointed on a lock cargo cannot read. A repoint that reaches outside its block now parses the lock again.planandcommitwere tied by a hidden field, and the edits went to the caller before the repoint was made.Fixed from the third round:
commitdid nothing, silently, for a plan made before the lock's latest one, while the caller had already written the manifest and the config and went on to count the patch as pinned. Nothing reached it, but nothing ruled it out. A planned repoint now borrows the lock, so the compiler rules it out.checksumorsourceis a table, which cargo never writes. A repoint is kept on the held parse only when it replaces the two values where they stand. Test: the second lock ina_lock_a_repoint_leaves_unreadable_refuses_the_crates_after_it.the_rewrite_parses_the_lock_once.commitwrites was unchecked. It compares the whole model.splicecopying the text and shifting its tail once per splice (one pass now).Not fixed:
cargo_unpinnable_dependentstakes the lock holder while the other probes takeOption<&CargoLock>. The first round asked for this: with theOption,Nonemeant both "no lock" and "a lock that does not parse".CargoHostedLockand pnpm'sPnpmLockStateare two holders of a text and its pending splices, and the restore path has a splice loop of its own. Folding them into one touches pnpm's hosted planner and the Cargo restore, which this PR leaves alone, and the Cargo holder also keeps a parsed model in step, which pnpm's does not have. That is a refactor for a PR of its own.Testing
cargo test -p socket-patch-core --lib: 7,908 passed.in_process_redirect,hosted_memory_engine,mode_migration_cargo,e2e_vex_redirect,scan,rollback: 638 passed.redirect_golden(1) andupstream_restore_golden(62) pass.e2e_redirect_cargo_shapes(15) ande2e_redirect_cargo_build(3) pass.cargo fmt --checkandcargo clippy --locked --workspace --all-features -- -D warningsare clean.cargo doc --document-private-itemsreports no unresolved link in the files this PR touches.c6f144325. After mergingmaininto the branch I re-ran only the Cargo lock, VEX discovery and vendor test groups of the core crate (317 passed).🤖 Generated with Claude Code