puffin-resolver: add singleton fast path

In many cases, version ranges are actually just pins to a
specific and single version. And we can detect that statically
by examining the range. If we do have a range that is just one
version, then we can ask a `VersionMap` for just that version
instead of iterating over what's in the map until we find one
that satisfies the range.

I had tried this before making `VersionMap` construction lazy,
but it didn't seem to matter much. But helps a lot more now
with a lazy `VersionMap` because it lets us avoid creating a
lot of distributions in memory that we won't ultimately use.
This commit is contained in:
Andrew Gallant 2024-02-14 13:46:53 -05:00 committed by Andrew Gallant
parent 8102980192
commit ed000d0dd5
1 changed files with 14 additions and 1 deletions

View File

@ -96,7 +96,7 @@ impl CandidateSelector {
pub(crate) fn select<'a>(
&'a self,
package_name: &'a PackageName,
range: &Range<Version>,
range: &'a Range<Version>,
version_map: &'a VersionMap,
) -> Option<Candidate<'a>> {
// If the package has a preference (e.g., an existing version from an existing lockfile),
@ -130,6 +130,19 @@ impl CandidateSelector {
}
};
if let Some(version) = range.as_singleton() {
if !version.any_prerelease() {
let maybe_dist_with_version = version_map.get_with_version(version);
tracing::trace!(
"range {:?} for package {:?} has exactly one version, found? {:?}",
range,
package_name,
maybe_dist_with_version.is_some(),
);
return maybe_dist_with_version
.map(|(version, dist)| Candidate::new(package_name, version, dist));
}
}
tracing::trace!(
"selecting candidate for package {:?} with range {:?} with {} versions",
package_name,