From a6c9323611421a1a9bf3ab80182a7e0df2adc6b8 Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 15 Mar 2026 18:51:38 +0100 Subject: [PATCH 1/2] Use `or_insert_with` for `FilePins::insert` Within a single fork, the same `(name, version)` always resolves to the same distribution, so we can skip constructing the `FilePin` (which involves `to_owned()` and two `distribution_id()` calls) when the entry already exists. --- crates/uv-resolver/src/pins.rs | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/crates/uv-resolver/src/pins.rs b/crates/uv-resolver/src/pins.rs index 417364d38a3c8..246a04fc5aa4e 100644 --- a/crates/uv-resolver/src/pins.rs +++ b/crates/uv-resolver/src/pins.rs @@ -1,5 +1,3 @@ -use std::collections::hash_map::Entry; - use rustc_hash::FxHashMap; use uv_distribution_types::{CompatibleDist, DistributionId, Identifier, ResolvedDist}; @@ -27,26 +25,16 @@ pub(crate) struct FilePins(FxHashMap<(PackageName, uv_pep440::Version), FilePin> // final resolution). impl FilePins { /// Pin a candidate package. + /// + /// Within a single fork, the same `(name, version)` always resolves to the same distribution, + /// so we skip construction when an entry already exists. pub(crate) fn insert(&mut self, candidate: &Candidate, dist: &CompatibleDist) { - let pin = FilePin { - dist: dist.for_installation().to_owned(), - metadata_id: dist.for_resolution().distribution_id(), - }; - match self - .0 + self.0 .entry((candidate.name().clone(), candidate.version().clone())) - { - Entry::Occupied(mut entry) => { - if entry.get().dist.distribution_id() != pin.dist.distribution_id() - || entry.get().metadata_id != pin.metadata_id - { - entry.insert(pin); - } - } - Entry::Vacant(entry) => { - entry.insert(pin); - } - } + .or_insert_with(|| FilePin { + dist: dist.for_installation().to_owned(), + metadata_id: dist.for_resolution().distribution_id(), + }); } /// Return the pinned file for the given package name and version, if it exists. From 9c7629c10ab2659d91471db18fd144501b49848a Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 15 Mar 2026 18:53:36 +0100 Subject: [PATCH 2/2] Avoid clone and double lookup in `get_dependencies` Replace separate `metadata_id()` + `get()` calls with a single `get_with_metadata_id()` lookup. Borrow the `DistributionId` from the pin instead of cloning, and defer error string formatting to the error path. --- crates/uv-resolver/src/pins.rs | 8 ++++---- crates/uv-resolver/src/resolution/output.rs | 11 ++++------- crates/uv-resolver/src/resolver/mod.rs | 19 +++++++++---------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/crates/uv-resolver/src/pins.rs b/crates/uv-resolver/src/pins.rs index 246a04fc5aa4e..9121dccfe2c37 100644 --- a/crates/uv-resolver/src/pins.rs +++ b/crates/uv-resolver/src/pins.rs @@ -48,14 +48,14 @@ impl FilePins { .map(|pin| &pin.dist) } - /// Return the distribution id whose metadata was used during resolution. - pub(crate) fn metadata_id( + /// Return the pinned distribution and its metadata id in a single lookup. + pub(crate) fn dist_and_id( &self, name: &PackageName, version: &uv_pep440::Version, - ) -> Option<&DistributionId> { + ) -> Option<(&ResolvedDist, &DistributionId)> { self.0 .get(&(name.clone(), version.clone())) - .map(|pin| &pin.metadata_id) + .map(|pin| (&pin.dist, &pin.metadata_id)) } } diff --git a/crates/uv-resolver/src/resolution/output.rs b/crates/uv-resolver/src/resolution/output.rs index 36e0ec1e629ab..fe514855d4e58 100644 --- a/crates/uv-resolver/src/resolution/output.rs +++ b/crates/uv-resolver/src/resolution/output.rs @@ -456,14 +456,11 @@ impl ResolverOutput { Some(metadata), ) } else { - let dist = pins - .get(name, version) - .expect("Every package should be pinned") - .clone(); + let (dist, metadata_id) = pins + .dist_and_id(name, version) + .expect("Every package should be pinned"); + let dist = dist.clone(); let hashes_id = dist.distribution_id(); - let metadata_id = pins - .metadata_id(name, version) - .expect("Every package should have pinned metadata"); // Track yanks for any registry distributions. match dist.yanked() { diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index bb4e5ac4e9795..a106367e21012 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -1810,17 +1810,16 @@ impl ResolverState ResolverState &archive.metadata,