From 13b6db7b3f845cb69a0aa1385da05894b4f25b68 Mon Sep 17 00:00:00 2001 From: jkaczman Date: Tue, 10 Nov 2026 09:17:04 -0800 Subject: [PATCH] fix: in microbench, hash LRU caches with fixed seed --- Cargo.lock | 1 + pgdog/Cargo.toml | 1 + pgdog/src/auth/token_cache.rs | 8 +++++--- pgdog/src/backend/prepared_statements.rs | 5 +++-- pgdog/src/frontend/router/parser/cache/cache_impl.rs | 6 +++--- pgdog/src/stats/memory.rs | 4 +++- pgdog/src/util.rs | 5 +++++ 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 750afb660..48374fb89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3252,6 +3252,7 @@ dependencies = [ "divan", "dyn-clone", "fnv", + "foldhash 0.2.0", "futures", "hex", "hickory-resolver", diff --git a/pgdog/Cargo.toml b/pgdog/Cargo.toml index 599acdc32..238225a11 100644 --- a/pgdog/Cargo.toml +++ b/pgdog/Cargo.toml @@ -15,6 +15,7 @@ fips = ["tokio-rustls/fips"] bench = [] [dependencies] +foldhash = { version = "0.2", default-features = false } bon.workspace = true pin-project = "1" tokio.workspace = true diff --git a/pgdog/src/auth/token_cache.rs b/pgdog/src/auth/token_cache.rs index 01706d355..c45439cc6 100644 --- a/pgdog/src/auth/token_cache.rs +++ b/pgdog/src/auth/token_cache.rs @@ -21,8 +21,8 @@ use tokio::sync::OnceCell; use tracing::error; use super::Error; -use crate::tasks; use crate::{backend::pool::Connection, config::config}; +use crate::{tasks, util::LruHasher}; /// Client token cache. /// @@ -101,14 +101,16 @@ struct Stats { #[derive(Clone, Debug)] pub(crate) struct AuthTokenCache { // Lookups use peek to preserve FIFO eviction order. - inner: Arc>>>, + inner: Arc, LruHasher>>>, stats: Arc, } impl Default for AuthTokenCache { fn default() -> Self { Self { - inner: Arc::new(Mutex::new(LruCache::unbounded())), + inner: Arc::new(Mutex::new(LruCache::unbounded_with_hasher( + Default::default(), + ))), stats: Arc::new(Stats::default()), } } diff --git a/pgdog/src/backend/prepared_statements.rs b/pgdog/src/backend/prepared_statements.rs index 9e5310a92..2b924cc07 100644 --- a/pgdog/src/backend/prepared_statements.rs +++ b/pgdog/src/backend/prepared_statements.rs @@ -13,6 +13,7 @@ use crate::{ messages::{ParameterDescription, RowDescription, parse::Parse}, }, state::State, + util::LruHasher, }; use crate::{net::ErrorResponse, util::time::deadline}; use parking_lot::RwLock; @@ -97,7 +98,7 @@ pub(super) enum HandleResult { #[derive(Debug)] pub(crate) struct PreparedStatements { global_cache: Arc>, - local_cache: LruCache, + local_cache: LruCache, state: ProtocolState, // Prepared statements being prepared now on the connection. parses: VecDeque, @@ -123,7 +124,7 @@ impl PreparedStatements { pub(crate) fn new(oids: Arc) -> Self { Self { global_cache: frontend::PreparedStatements::global(), - local_cache: LruCache::unbounded(), + local_cache: LruCache::unbounded_with_hasher(Default::default()), state: ProtocolState::default(), parses: VecDeque::new(), parameter_describes: VecDeque::new(), diff --git a/pgdog/src/frontend/router/parser/cache/cache_impl.rs b/pgdog/src/frontend/router/parser/cache/cache_impl.rs index 53810846e..e17566764 100644 --- a/pgdog/src/frontend/router/parser/cache/cache_impl.rs +++ b/pgdog/src/frontend/router/parser/cache/cache_impl.rs @@ -48,7 +48,7 @@ impl Stats { #[derive(Debug)] pub(super) struct Inner { /// Least-recently-used cache. - queries: LruCache, Arc>, + queries: LruCache, Arc, crate::util::LruHasher>, /// Cache global stats. pub(super) stats: Stats, /// Cache hits. @@ -96,7 +96,7 @@ impl Cache { fn new() -> Self { Self { inner: RwLock::new(Inner { - queries: LruCache::unbounded(), + queries: LruCache::unbounded_with_hasher(Default::default()), stats: Stats::default(), hits: AtomicUsize::default(), }), @@ -344,7 +344,7 @@ mod test { #[test] fn test_used_statement_survives_eviction() { let mut inner = Inner { - queries: LruCache::new(2.try_into().unwrap()), + queries: LruCache::with_hasher(2.try_into().unwrap(), Default::default()), stats: Stats::default(), hits: AtomicUsize::default(), }; diff --git a/pgdog/src/stats/memory.rs b/pgdog/src/stats/memory.rs index ac6079f91..0b86dfa84 100644 --- a/pgdog/src/stats/memory.rs +++ b/pgdog/src/stats/memory.rs @@ -72,7 +72,9 @@ impl MemoryUsage for HashSet { } } -impl MemoryUsage for LruCache { +impl MemoryUsage + for LruCache +{ fn memory_usage(&self) -> usize { self.iter() .map(|(k, v)| k.memory_usage() + v.memory_usage()) diff --git a/pgdog/src/util.rs b/pgdog/src/util.rs index 6a63b60ec..fd6af6be4 100644 --- a/pgdog/src/util.rs +++ b/pgdog/src/util.rs @@ -340,3 +340,8 @@ mod test { assert_eq!(sanitize_log_sample("a\nb\nc", 3), "a b"); } } + +#[cfg(not(feature = "bench"))] +pub(crate) type LruHasher = lru::DefaultHasher; +#[cfg(feature = "bench")] +pub(crate) type LruHasher = foldhash::fast::FixedState;