Skip to content

perf: eliminate stage_artifact double-copy, drop was_patched flag#3157

Merged
messense merged 2 commits intoPyO3:mainfrom
messense:refactor/stage-artifact-finalize
Apr 26, 2026
Merged

perf: eliminate stage_artifact double-copy, drop was_patched flag#3157
messense merged 2 commits intoPyO3:mainfrom
messense:refactor/stage-artifact-finalize

Conversation

@messense
Copy link
Copy Markdown
Member

Cross-device staging used to do a full copy plus a remove_file in stage_file, then another full copy plus a remove_file in finalize_staged_artifacts. The original is now left at the cargo output path on cross-device, so finalize just drops the staged duplicate and the bytes are only copied once. Same-filesystem stage_file also drops a syscall: std::fs::rename overwrites stale staging leftovers on both Unix and Windows, so the pre-clean is needed only in the cross-device branch.

The build-wide was_patched bool plumbed through add_external_libs, WheelRepairer::patch_editable, and the orchestrator is now redundant with the per-artifact cargo_output_path: Option<PathBuf>. Patching code paths clear that field on the artifacts they touch and finalize skips artifacts where it is None — single source of truth, and a multi-bin build with mixed patched/unpatched bins gets per-artifact precision for free. WheelRepairer::patch_editable's signature returns to Result<()>.

finalize_staged_artifact also recovers a robustness property the previous PR lost: if cargo / rust-analyzer rebuilt the artifact at the cargo output path while the wheel was being written, the fresh file wins and the staged duplicate is discarded.

Cross-device staging used to do a full copy plus a `remove_file` in
`stage_file`, then another full copy plus a `remove_file` in
`finalize_staged_artifacts`. The original is now left at the cargo
output path on cross-device, so finalize just drops the staged duplicate
and the bytes are only copied once. Same-filesystem `stage_file` also
drops a syscall: `std::fs::rename` overwrites stale staging leftovers
on both Unix and Windows, so the pre-clean is needed only in the
cross-device branch.

The build-wide `was_patched` bool plumbed through `add_external_libs`,
`WheelRepairer::patch_editable`, and the orchestrator is now redundant
with the per-artifact `cargo_output_path: Option<PathBuf>`. Patching
code paths clear that field on the artifacts they touch and finalize
skips artifacts where it is `None` — single source of truth, and a
multi-bin build with mixed patched/unpatched bins gets per-artifact
precision for free. `WheelRepairer::patch_editable`'s signature returns
to `Result<()>`.

`finalize_staged_artifact` also recovers a robustness property the
previous PR lost: if cargo / rust-analyzer rebuilt the artifact at the
cargo output path while the wheel was being written, the fresh file
wins and the staged duplicate is discarded.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR streamlines artifact staging/finalization to avoid redundant copies (especially on cross-device staging) and simplifies patch-tracking by removing the build-wide was_patched flag in favor of per-artifact cargo_output_path: Option<PathBuf> as the single source of truth.

Changes:

  • Avoid the stage→finalize “double copy” on cross-device by leaving the original at the cargo output path and discarding the staged duplicate during finalize.
  • Remove the was_patched plumbing by switching patch tracking to per-artifact cargo_output_path clearing, and update call sites accordingly.
  • Update editable patching and Linux repairer code to conform to the new patch_editable(&mut [AuditedArtifact]) -> Result<()> contract.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/compile.rs Clarifies cargo_output_path semantics as the per-artifact patch/restore signal.
src/build_orchestrator.rs Updates wheel-writing pipeline to pass audited artifacts mutably; removes was_patched usage.
src/build_context/repair.rs Implements new staging/finalization behavior (single-copy cross-device) and makes finalize conditional on per-artifact cargo_output_path.
src/auditwheel/repair.rs Adjusts WheelRepairer::patch_editable signature/contract to use per-artifact patch signaling.
src/auditwheel/linux.rs Updates Linux editable patching to clear cargo_output_path instead of returning a patched boolean.

Comment thread src/build_context/repair.rs
Comment thread src/build_context/repair.rs
Comment thread src/build_context/repair.rs
@messense messense force-pushed the refactor/stage-artifact-finalize branch from 1d52847 to 9b993ba Compare April 26, 2026 03:59
Previously `stage_file` treated any `fs::rename` error as cross-device
and fell back to `reflink_or_copy`, silently masking permission /
locking / I/O failures. Match on `io::ErrorKind::CrossesDevices`
specifically and propagate every other error with context.
@messense messense force-pushed the refactor/stage-artifact-finalize branch from 9b993ba to 81851d9 Compare April 26, 2026 04:03
@messense messense merged commit 5754f1c into PyO3:main Apr 26, 2026
42 of 44 checks passed
@messense messense deleted the refactor/stage-artifact-finalize branch April 26, 2026 04:35
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