From ce3654da7713938f65445beccc0649deafadf84f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 24 Feb 2025 20:30:54 -1000 Subject: [PATCH] Use a Boxed slice for version specifiers (#11766) ## Summary These are never modified, and we create _tons_ of them. --- crates/uv-pep440/src/version_specifier.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/uv-pep440/src/version_specifier.rs b/crates/uv-pep440/src/version_specifier.rs index 91e7171e5..4cbf6da74 100644 --- a/crates/uv-pep440/src/version_specifier.rs +++ b/crates/uv-pep440/src/version_specifier.rs @@ -30,7 +30,7 @@ use tracing::warn; derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize) )] #[cfg_attr(feature = "rkyv", rkyv(derive(Debug)))] -pub struct VersionSpecifiers(Vec); +pub struct VersionSpecifiers(Box<[VersionSpecifier]>); impl std::ops::Deref for VersionSpecifiers { type Target = [VersionSpecifier]; @@ -43,7 +43,7 @@ impl std::ops::Deref for VersionSpecifiers { impl VersionSpecifiers { /// Matches all versions. pub fn empty() -> Self { - Self(Vec::new()) + Self(Box::new([])) } /// Whether all specifiers match the given version. @@ -61,7 +61,7 @@ impl VersionSpecifiers { // TODO(konsti): This seems better than sorting on insert and not getting the size hint, // but i haven't measured it. specifiers.sort_by(|a, b| a.version().cmp(b.version())); - Self(specifiers) + Self(specifiers.into_boxed_slice()) } /// Returns the [`VersionSpecifiers`] whose union represents the given range. @@ -117,7 +117,7 @@ impl IntoIterator for VersionSpecifiers { type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() + self.0.into_vec().into_iter() } } @@ -131,7 +131,7 @@ impl FromStr for VersionSpecifiers { impl From for VersionSpecifiers { fn from(specifier: VersionSpecifier) -> Self { - Self(vec![specifier]) + Self(Box::new([specifier])) } } @@ -1285,7 +1285,7 @@ mod tests { fn test_parse_version_specifiers() { let result = VersionSpecifiers::from_str("~= 0.9, >= 1.0, != 1.3.4.*, < 2.0").unwrap(); assert_eq!( - result.0, + result.0.as_ref(), [ VersionSpecifier { operator: Operator::TildeEqual,