From 4fc181dbf25bbffd58df04a6ab67a33a317d84fe Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 24 Feb 2025 09:03:56 -1000 Subject: [PATCH] Use a `SmallString` for the Yanked enum (#11715) ## Summary This is stored on `File`, which we create extensively. Easy way to reduce size. --- Cargo.lock | 1 + crates/uv-client/src/html.rs | 2 +- crates/uv-pypi-types/Cargo.toml | 1 + crates/uv-pypi-types/src/simple_json.rs | 6 ++++-- crates/uv-resolver/src/resolution/output.rs | 2 +- crates/uv-small-str/src/lib.rs | 10 ++++++++++ 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1a290ba9..f9f263747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5431,6 +5431,7 @@ dependencies = [ "uv-normalize", "uv-pep440", "uv-pep508", + "uv-small-str", ] [[package]] diff --git a/crates/uv-client/src/html.rs b/crates/uv-client/src/html.rs index 2fe521658..a7733bf69 100644 --- a/crates/uv-client/src/html.rs +++ b/crates/uv-client/src/html.rs @@ -184,7 +184,7 @@ impl SimpleHtml { let yanked = if let Some(yanked) = link.attributes().get("data-yanked").flatten() { let yanked = std::str::from_utf8(yanked.as_bytes())?; let yanked = html_escape::decode_html_entities(yanked); - Some(Yanked::Reason(yanked.to_string())) + Some(Yanked::Reason(yanked.into())) } else { None }; diff --git a/crates/uv-pypi-types/Cargo.toml b/crates/uv-pypi-types/Cargo.toml index 9c3f8336c..2afb0d3f2 100644 --- a/crates/uv-pypi-types/Cargo.toml +++ b/crates/uv-pypi-types/Cargo.toml @@ -22,6 +22,7 @@ uv-git-types = { workspace = true } uv-normalize = { workspace = true } uv-pep440 = { workspace = true } uv-pep508 = { workspace = true } +uv-small-str = { workspace = true } hashbrown = { workspace = true } indexmap = { workspace = true, features = ["serde"] } diff --git a/crates/uv-pypi-types/src/simple_json.rs b/crates/uv-pypi-types/src/simple_json.rs index a0188c772..d61518b72 100644 --- a/crates/uv-pypi-types/src/simple_json.rs +++ b/crates/uv-pypi-types/src/simple_json.rs @@ -2,7 +2,9 @@ use std::str::FromStr; use jiff::Timestamp; use serde::{Deserialize, Deserializer, Serialize}; + use uv_pep440::{VersionSpecifiers, VersionSpecifiersParseError}; +use uv_small_str::SmallString; use crate::lenient_requirement::LenientVersionSpecifiers; @@ -101,7 +103,7 @@ impl CoreMetadata { #[rkyv(derive(Debug))] pub enum Yanked { Bool(bool), - Reason(String), + Reason(SmallString), } impl<'de> Deserialize<'de> for Yanked { @@ -111,7 +113,7 @@ impl<'de> Deserialize<'de> for Yanked { { serde_untagged::UntaggedEnumVisitor::new() .bool(|bool| Ok(Yanked::Bool(bool))) - .string(|string| Ok(Yanked::Reason(string.to_owned()))) + .string(|string| Ok(Yanked::Reason(SmallString::from(string)))) .deserialize(deserializer) } } diff --git a/crates/uv-resolver/src/resolution/output.rs b/crates/uv-resolver/src/resolution/output.rs index 7f31460bd..0fc7d9cc0 100644 --- a/crates/uv-resolver/src/resolution/output.rs +++ b/crates/uv-resolver/src/resolution/output.rs @@ -476,7 +476,7 @@ impl ResolverOutput { Some(Yanked::Reason(reason)) => { diagnostics.push(ResolutionDiagnostic::YankedVersion { dist: dist.clone(), - reason: Some(reason.clone()), + reason: Some(reason.to_string()), }); } } diff --git a/crates/uv-small-str/src/lib.rs b/crates/uv-small-str/src/lib.rs index 20d66190e..f8e78d521 100644 --- a/crates/uv-small-str/src/lib.rs +++ b/crates/uv-small-str/src/lib.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::cmp::PartialEq; use std::ops::Deref; @@ -26,6 +27,15 @@ impl From for SmallString { } } +impl From> for SmallString { + fn from(s: Cow<'_, str>) -> Self { + match s { + Cow::Borrowed(s) => Self::from(s), + Cow::Owned(s) => Self::from(s), + } + } +} + impl AsRef for SmallString { #[inline] fn as_ref(&self) -> &str {