Skip to content

Commit

Permalink
Use FxHash in uv-auth (#6149)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Aug 16, 2024
1 parent 6766124 commit 91fba4e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions crates/once-map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V>` to make cloning cheap.
pub struct OnceMap<K, V, H = RandomState> {
items: DashMap<K, Value<V>, H>,
pub struct OnceMap<K, V, S = RandomState> {
items: DashMap<K, Value<V>, S>,
}

impl<K: Eq + Hash, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
// Create a [`OnceMap`] with the specified capacity and hasher.
/// Create a [`OnceMap`] with the specified hasher.
pub fn with_hasher(hasher: H) -> OnceMap<K, V, H> {
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<K, V, H> {
OnceMap {
items: DashMap::with_capacity_and_hasher(capacity, hasher),
Expand Down
1 change: 1 addition & 0 deletions crates/uv-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
21 changes: 13 additions & 8 deletions crates/uv-auth/src/cache.rs
Original file line number Diff line number Diff line change
@@ -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<K, V> = OnceMap<K, V, BuildHasherDefault<FxHasher>>;

pub struct CredentialsCache {
/// A cache per realm and username
realms: RwLock<HashMap<(Realm, Username), Arc<Credentials>>>,
realms: RwLock<FxHashMap<(Realm, Username), Arc<Credentials>>>,
/// A cache tracking the result of fetches from external services
pub(crate) fetches: OnceMap<(Realm, Username), Option<Arc<Credentials>>>,
pub(crate) fetches: FxOnceMap<(Realm, Username), Option<Arc<Credentials>>>,
/// A cache per URL, uses a trie for efficient prefix queries.
urls: RwLock<UrlTrie>,
}
Expand All @@ -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()),
}
}
Expand Down
11 changes: 0 additions & 11 deletions crates/uv-resolver/src/resolver/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ struct SharedInMemoryIndex {
pub(crate) type FxOnceMap<K, V> = OnceMap<K, V, BuildHasherDefault<FxHasher>>;

impl InMemoryIndex {
/// Create an `InMemoryIndex` with pre-filled packages and distributions.
pub fn with(
packages: FxOnceMap<PackageName, Arc<VersionsResponse>>,
distributions: FxOnceMap<VersionId, Arc<MetadataResponse>>,
) -> InMemoryIndex {
InMemoryIndex(Arc::new(SharedInMemoryIndex {
packages,
distributions,
}))
}

/// Returns a reference to the package metadata map.
pub fn packages(&self) -> &FxOnceMap<PackageName, Arc<VersionsResponse>> {
&self.0.packages
Expand Down

0 comments on commit 91fba4e

Please sign in to comment.