From 204f46fc83017fd5c4915aa2021f04a0c7f9b8b7 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 17 Feb 2025 21:22:36 -0500 Subject: [PATCH] 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. --- crates/uv-distribution-types/src/index_url.rs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/uv-distribution-types/src/index_url.rs b/crates/uv-distribution-types/src/index_url.rs index 49bd7b896..49ad9e648 100644 --- a/crates/uv-distribution-types/src/index_url.rs +++ b/crates/uv-distribution-types/src/index_url.rs @@ -17,15 +17,17 @@ use crate::{Index, Verbatim}; static PYPI_URL: LazyLock = LazyLock::new(|| Url::parse("https://pypi.org/simple").unwrap()); static DEFAULT_INDEX: LazyLock = 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). #[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum IndexUrl { - Pypi(VerbatimUrl), - Url(VerbatimUrl), - Path(VerbatimUrl), + Pypi(Arc), + Url(Arc), + Path(Arc), } impl IndexUrl { @@ -96,9 +98,9 @@ impl IndexUrl { /// Convert the index URL into a [`Url`]. pub fn into_url(self) -> Url { match self { - Self::Pypi(url) => url.into_url(), - Self::Url(url) => url.into_url(), - Self::Path(url) => url.into_url(), + Self::Pypi(url) => url.to_url(), + Self::Url(url) => url.to_url(), + Self::Path(url) => url.to_url(), } } @@ -177,11 +179,11 @@ impl<'de> serde::de::Deserialize<'de> for IndexUrl { impl From for IndexUrl { fn from(url: VerbatimUrl) -> Self { if url.scheme() == "file" { - Self::Path(url) + Self::Path(Arc::new(url)) } else if *url.raw() == *PYPI_URL { - Self::Pypi(url) + Self::Pypi(Arc::new(url)) } else { - Self::Url(url) + Self::Url(Arc::new(url)) } } }