Use an `Arc` for Index URLs (#11586)

## Summary

I realized that we clone this for every distribution. It looks like it's
consistently 1-2% faster to use an `Arc` here.
This commit is contained in:
Charlie Marsh 2025-02-17 21:22:36 -05:00 committed by GitHub
parent 29c2be3e97
commit 204f46fc83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 10 deletions

View File

@ -17,15 +17,17 @@ use crate::{Index, Verbatim};
static PYPI_URL: LazyLock<Url> = LazyLock::new(|| Url::parse("https://pypi.org/simple").unwrap()); static PYPI_URL: LazyLock<Url> = LazyLock::new(|| Url::parse("https://pypi.org/simple").unwrap());
static DEFAULT_INDEX: LazyLock<Index> = LazyLock::new(|| { static DEFAULT_INDEX: LazyLock<Index> = LazyLock::new(|| {
Index::from_index_url(IndexUrl::Pypi(VerbatimUrl::from_url(PYPI_URL.clone()))) Index::from_index_url(IndexUrl::Pypi(Arc::new(VerbatimUrl::from_url(
PYPI_URL.clone(),
))))
}); });
/// The URL of an index to use for fetching packages (e.g., PyPI). /// The URL of an index to use for fetching packages (e.g., PyPI).
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] #[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum IndexUrl { pub enum IndexUrl {
Pypi(VerbatimUrl), Pypi(Arc<VerbatimUrl>),
Url(VerbatimUrl), Url(Arc<VerbatimUrl>),
Path(VerbatimUrl), Path(Arc<VerbatimUrl>),
} }
impl IndexUrl { impl IndexUrl {
@ -96,9 +98,9 @@ impl IndexUrl {
/// Convert the index URL into a [`Url`]. /// Convert the index URL into a [`Url`].
pub fn into_url(self) -> Url { pub fn into_url(self) -> Url {
match self { match self {
Self::Pypi(url) => url.into_url(), Self::Pypi(url) => url.to_url(),
Self::Url(url) => url.into_url(), Self::Url(url) => url.to_url(),
Self::Path(url) => url.into_url(), Self::Path(url) => url.to_url(),
} }
} }
@ -177,11 +179,11 @@ impl<'de> serde::de::Deserialize<'de> for IndexUrl {
impl From<VerbatimUrl> for IndexUrl { impl From<VerbatimUrl> for IndexUrl {
fn from(url: VerbatimUrl) -> Self { fn from(url: VerbatimUrl) -> Self {
if url.scheme() == "file" { if url.scheme() == "file" {
Self::Path(url) Self::Path(Arc::new(url))
} else if *url.raw() == *PYPI_URL { } else if *url.raw() == *PYPI_URL {
Self::Pypi(url) Self::Pypi(Arc::new(url))
} else { } else {
Self::Url(url) Self::Url(Arc::new(url))
} }
} }
} }