From f3468ba436be2f900ee8bca5d055412cee65b8f2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 16 Aug 2024 12:29:46 -0400 Subject: [PATCH] Use FxHash in uv-auth --- Cargo.lock | 1 + crates/once-map/src/lib.rs | 13 ++++++++++--- crates/uv-auth/Cargo.toml | 1 + crates/uv-auth/src/cache.rs | 21 +++++++++++++-------- crates/uv-resolver/src/resolver/index.rs | 11 ----------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca443cb85ca1..c88ab5639971 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4553,6 +4553,7 @@ dependencies = [ "reqwest", "reqwest-middleware", "rust-netrc", + "rustc-hash 2.0.0", "tempfile", "test-log", "tokio", diff --git a/crates/once-map/src/lib.rs b/crates/once-map/src/lib.rs index 77e342676375..2de152619c25 100644 --- a/crates/once-map/src/lib.rs +++ b/crates/once-map/src/lib.rs @@ -15,12 +15,19 @@ use tokio::sync::Notify; /// /// Note that this always clones the value out of the underlying map. Because /// of this, it's common to wrap the `V` in an `Arc` to make cloning cheap. -pub struct OnceMap { - items: DashMap, H>, +pub struct OnceMap { + items: DashMap, S>, } impl OnceMap { - // Create a [`OnceMap`] with the specified capacity and hasher. + /// Create a [`OnceMap`] with the specified hasher. + pub fn with_hasher(hasher: H) -> OnceMap { + OnceMap { + items: DashMap::with_hasher(hasher), + } + } + + /// Create a [`OnceMap`] with the specified capacity and hasher. pub fn with_capacity_and_hasher(capacity: usize, hasher: H) -> OnceMap { OnceMap { items: DashMap::with_capacity_and_hasher(capacity, hasher), diff --git a/crates/uv-auth/Cargo.toml b/crates/uv-auth/Cargo.toml index a909f454a2d6..e748ee372f78 100644 --- a/crates/uv-auth/Cargo.toml +++ b/crates/uv-auth/Cargo.toml @@ -16,6 +16,7 @@ once-map = { workspace = true } reqwest = { workspace = true } reqwest-middleware = { workspace = true } rust-netrc = { workspace = true } +rustc-hash = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } url = { workspace = true } diff --git a/crates/uv-auth/src/cache.rs b/crates/uv-auth/src/cache.rs index 1504ce798899..b28bbedccbbb 100644 --- a/crates/uv-auth/src/cache.rs +++ b/crates/uv-auth/src/cache.rs @@ -1,18 +1,23 @@ +use std::hash::BuildHasherDefault; use std::sync::Arc; -use std::{collections::HashMap, sync::RwLock}; +use std::sync::RwLock; + +use rustc_hash::{FxHashMap, FxHasher}; +use tracing::trace; +use url::Url; + +use once_map::OnceMap; use crate::credentials::{Credentials, Username}; use crate::Realm; -use once_map::OnceMap; -use tracing::trace; -use url::Url; +type FxOnceMap = OnceMap>; pub struct CredentialsCache { /// A cache per realm and username - realms: RwLock>>, + realms: RwLock>>, /// A cache tracking the result of fetches from external services - pub(crate) fetches: OnceMap<(Realm, Username), Option>>, + pub(crate) fetches: FxOnceMap<(Realm, Username), Option>>, /// A cache per URL, uses a trie for efficient prefix queries. urls: RwLock, } @@ -27,8 +32,8 @@ impl CredentialsCache { /// Create a new cache. pub fn new() -> Self { Self { - fetches: OnceMap::default(), - realms: RwLock::new(HashMap::new()), + fetches: FxOnceMap::default(), + realms: RwLock::new(FxHashMap::default()), urls: RwLock::new(UrlTrie::new()), } } diff --git a/crates/uv-resolver/src/resolver/index.rs b/crates/uv-resolver/src/resolver/index.rs index aa0c3aa45456..24e52c477a31 100644 --- a/crates/uv-resolver/src/resolver/index.rs +++ b/crates/uv-resolver/src/resolver/index.rs @@ -25,17 +25,6 @@ struct SharedInMemoryIndex { pub(crate) type FxOnceMap = OnceMap>; impl InMemoryIndex { - /// Create an `InMemoryIndex` with pre-filled packages and distributions. - pub fn with( - packages: FxOnceMap>, - distributions: FxOnceMap>, - ) -> InMemoryIndex { - InMemoryIndex(Arc::new(SharedInMemoryIndex { - packages, - distributions, - })) - } - /// Returns a reference to the package metadata map. pub fn packages(&self) -> &FxOnceMap> { &self.0.packages