From 349f5e0846278edd17b167a64d70ac61eeb83203 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Wed, 2 Aug 2023 21:54:45 +0000 Subject: [PATCH] read-locks Turbine QUIC connection cache for lookups (#32687) Frequently accessed remote addresses are cached, making a shared-lock sufficient which would reduce lock contention for the more common cache-hit lookups. --- turbine/src/quic_endpoint.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/turbine/src/quic_endpoint.rs b/turbine/src/quic_endpoint.rs index 5568262d64..0f93391e04 100644 --- a/turbine/src/quic_endpoint.rs +++ b/turbine/src/quic_endpoint.rs @@ -294,8 +294,7 @@ async fn get_connection( sender: Sender<(Pubkey, SocketAddr, Bytes)>, cache: Arc>, ) -> Result { - let key = (remote_address, /*remote_pubkey:*/ None); - let entry = cache.write().await.entry(key).or_default().clone(); + let entry = get_cache_entry(remote_address, &cache).await; { let connection: Option = entry.read().await.clone(); if let Some(connection) = connection { @@ -342,6 +341,17 @@ fn get_remote_pubkey(connection: &Connection) -> Result { } } +async fn get_cache_entry( + remote_address: SocketAddr, + cache: &RwLock, +) -> Arc>> { + let key = (remote_address, /*remote_pubkey:*/ None); + if let Some(entry) = cache.read().await.get(&key) { + return entry.clone(); + } + cache.write().await.entry(key).or_default().clone() +} + async fn cache_connection( remote_address: SocketAddr, remote_pubkey: Pubkey,