Use FxHash in uv-auth (#6149)

This commit is contained in:
Charlie Marsh
2024-08-16 13:14:51 -04:00
committed by GitHub
parent 6766124fd6
commit 91fba4e1e6
5 changed files with 25 additions and 22 deletions
Generated
+1
View File
@@ -4553,6 +4553,7 @@ dependencies = [
"reqwest",
"reqwest-middleware",
"rust-netrc",
"rustc-hash 2.0.0",
"tempfile",
"test-log",
"tokio",
+10 -3
View File
@@ -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),
+1
View File
@@ -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 }
+13 -8
View File
@@ -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>,
}
@@ -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()),
}
}
-11
View File
@@ -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