Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions pgdog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fips = ["tokio-rustls/fips"]
bench = []

[dependencies]
foldhash = { version = "0.2", default-features = false }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make it conditional, i.e., only if bench feature is enabled. Although LLVM will probably remove it anyway during compilation / dead code elimination.

bon.workspace = true
pin-project = "1"
tokio.workspace = true
Expand Down
8 changes: 5 additions & 3 deletions pgdog/src/auth/token_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -101,14 +101,16 @@ struct Stats {
#[derive(Clone, Debug)]
pub(crate) struct AuthTokenCache {
// Lookups use peek to preserve FIFO eviction order.
inner: Arc<Mutex<LruCache<Key, Arc<Entry>>>>,
inner: Arc<Mutex<LruCache<Key, Arc<Entry>, LruHasher>>>,
stats: Arc<Stats>,
}

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()),
}
}
Expand Down
5 changes: 3 additions & 2 deletions pgdog/src/backend/prepared_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +98,7 @@ pub(super) enum HandleResult {
#[derive(Debug)]
pub(crate) struct PreparedStatements {
global_cache: Arc<RwLock<GlobalCache>>,
local_cache: LruCache<String, LocalStatement>,
local_cache: LruCache<String, LocalStatement, LruHasher>,
state: ProtocolState,
// Prepared statements being prepared now on the connection.
parses: VecDeque<String>,
Expand All @@ -123,7 +124,7 @@ impl PreparedStatements {
pub(crate) fn new(oids: Arc<Oids>) -> 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(),
Expand Down
6 changes: 3 additions & 3 deletions pgdog/src/frontend/router/parser/cache/cache_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Stats {
#[derive(Debug)]
pub(super) struct Inner {
/// Least-recently-used cache.
queries: LruCache<Arc<str>, Arc<Ast>>,
queries: LruCache<Arc<str>, Arc<Ast>, crate::util::LruHasher>,
/// Cache global stats.
pub(super) stats: Stats,
/// Cache hits.
Expand Down Expand Up @@ -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(),
}),
Expand Down Expand Up @@ -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(),
};
Expand Down
4 changes: 3 additions & 1 deletion pgdog/src/stats/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ impl<V: MemoryUsage> MemoryUsage for HashSet<V> {
}
}

impl<K: MemoryUsage + Hash + Eq, V: MemoryUsage + Eq> MemoryUsage for LruCache<K, V> {
impl<K: MemoryUsage + Hash + Eq, V: MemoryUsage + Eq, S: std::hash::BuildHasher> MemoryUsage
for LruCache<K, V, S>
{
fn memory_usage(&self) -> usize {
self.iter()
.map(|(k, v)| k.memory_usage() + v.memory_usage())
Expand Down
5 changes: 5 additions & 0 deletions pgdog/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading