Replace HashMap and Vec with IndexMap in connection cache (#24967)

This commit is contained in:
Pankaj Garg 2022-05-04 12:38:03 -07:00 committed by GitHub
parent 40986daddf
commit b4c3b66f1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 9 deletions

1
Cargo.lock generated
View File

@ -4510,6 +4510,7 @@ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"futures 0.3.21", "futures 0.3.21",
"futures-util", "futures-util",
"indexmap",
"indicatif", "indicatif",
"itertools", "itertools",
"jsonrpc-core", "jsonrpc-core",

View File

@ -20,6 +20,7 @@ clap = "2.33.0"
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
futures = "0.3" futures = "0.3"
futures-util = "0.3.21" futures-util = "0.3.21"
indexmap = "1.8.1"
indicatif = "0.16.2" indicatif = "0.16.2"
itertools = "0.10.2" itertools = "0.10.2"
jsonrpc-core = "18.0.0" jsonrpc-core = "18.0.0"

View File

@ -4,6 +4,7 @@ use {
tpu_connection::{ClientStats, TpuConnection}, tpu_connection::{ClientStats, TpuConnection},
udp_client::UdpTpuConnection, udp_client::UdpTpuConnection,
}, },
indexmap::map::IndexMap,
lazy_static::lazy_static, lazy_static::lazy_static,
quinn_proto::ConnectionStats, quinn_proto::ConnectionStats,
rand::{thread_rng, Rng}, rand::{thread_rng, Rng},
@ -13,7 +14,6 @@ use {
timing::AtomicInterval, transaction::VersionedTransaction, transport::TransportError, timing::AtomicInterval, transaction::VersionedTransaction, transport::TransportError,
}, },
std::{ std::{
collections::HashMap,
net::{IpAddr, Ipv4Addr, SocketAddr}, net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{ sync::{
atomic::{AtomicU64, Ordering}, atomic::{AtomicU64, Ordering},
@ -172,8 +172,7 @@ impl ConnectionCacheStats {
} }
struct ConnectionMap { struct ConnectionMap {
map: HashMap<SocketAddr, Connection>, map: IndexMap<SocketAddr, Connection>,
list_of_peers: Vec<SocketAddr>,
stats: Arc<ConnectionCacheStats>, stats: Arc<ConnectionCacheStats>,
last_stats: AtomicInterval, last_stats: AtomicInterval,
use_quic: bool, use_quic: bool,
@ -182,8 +181,7 @@ struct ConnectionMap {
impl ConnectionMap { impl ConnectionMap {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
map: HashMap::with_capacity(MAX_CONNECTIONS), map: IndexMap::with_capacity(MAX_CONNECTIONS),
list_of_peers: vec![],
stats: Arc::new(ConnectionCacheStats::default()), stats: Arc::new(ConnectionCacheStats::default()),
last_stats: AtomicInterval::default(), last_stats: AtomicInterval::default(),
use_quic: false, use_quic: false,
@ -270,17 +268,15 @@ fn get_or_add_connection(addr: &SocketAddr) -> GetConnectionResult {
let mut num_evictions = 0; let mut num_evictions = 0;
let mut get_connection_cache_eviction_measure = let mut get_connection_cache_eviction_measure =
Measure::start("get_connection_cache_eviction_measure"); Measure::start("get_connection_cache_eviction_measure");
while map.list_of_peers.len() >= MAX_CONNECTIONS { while map.map.len() >= MAX_CONNECTIONS {
let mut rng = thread_rng(); let mut rng = thread_rng();
let n = rng.gen_range(0, MAX_CONNECTIONS); let n = rng.gen_range(0, MAX_CONNECTIONS);
let nth_addr = map.list_of_peers.swap_remove(n); map.map.swap_remove_index(n);
map.map.remove(&nth_addr);
num_evictions += 1; num_evictions += 1;
} }
get_connection_cache_eviction_measure.stop(); get_connection_cache_eviction_measure.stop();
map.map.insert(*addr, connection.clone()); map.map.insert(*addr, connection.clone());
map.list_of_peers.push(*addr);
( (
connection, connection,
false, false,

View File

@ -4213,6 +4213,7 @@ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"futures 0.3.21", "futures 0.3.21",
"futures-util", "futures-util",
"indexmap",
"indicatif", "indicatif",
"itertools", "itertools",
"jsonrpc-core", "jsonrpc-core",