Use a SmallString for the Yanked enum (#11715)

## Summary

This is stored on `File`, which we create extensively. Easy way to
reduce size.
This commit is contained in:
Charlie Marsh 2025-02-24 09:03:56 -10:00 committed by GitHub
parent 1f7f9fdeb4
commit 4fc181dbf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 18 additions and 4 deletions

1
Cargo.lock generated
View file

@ -5431,6 +5431,7 @@ dependencies = [
"uv-normalize",
"uv-pep440",
"uv-pep508",
"uv-small-str",
]
[[package]]

View file

@ -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
};

View file

@ -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"] }

View file

@ -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)
}
}

View file

@ -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()),
});
}
}

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::cmp::PartialEq;
use std::ops::Deref;
@ -26,6 +27,15 @@ impl From<String> for SmallString {
}
}
impl From<Cow<'_, str>> for SmallString {
fn from(s: Cow<'_, str>) -> Self {
match s {
Cow::Borrowed(s) => Self::from(s),
Cow::Owned(s) => Self::from(s),
}
}
}
impl AsRef<str> for SmallString {
#[inline]
fn as_ref(&self) -> &str {