diff --git a/Cargo.lock b/Cargo.lock index acd9a0b76d..d14b1a1a30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4510,6 +4510,7 @@ dependencies = [ "crossbeam-channel", "futures 0.3.21", "futures-util", + "indexmap", "indicatif", "itertools", "jsonrpc-core", diff --git a/client/Cargo.toml b/client/Cargo.toml index e819d189d5..9a55e4212c 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -20,6 +20,7 @@ clap = "2.33.0" crossbeam-channel = "0.5" futures = "0.3" futures-util = "0.3.21" +indexmap = "1.8.1" indicatif = "0.16.2" itertools = "0.10.2" jsonrpc-core = "18.0.0" diff --git a/client/src/connection_cache.rs b/client/src/connection_cache.rs index 4b55644878..8a209b4a13 100644 --- a/client/src/connection_cache.rs +++ b/client/src/connection_cache.rs @@ -4,6 +4,7 @@ use { tpu_connection::{ClientStats, TpuConnection}, udp_client::UdpTpuConnection, }, + indexmap::map::IndexMap, lazy_static::lazy_static, quinn_proto::ConnectionStats, rand::{thread_rng, Rng}, @@ -13,7 +14,6 @@ use { timing::AtomicInterval, transaction::VersionedTransaction, transport::TransportError, }, std::{ - collections::HashMap, net::{IpAddr, Ipv4Addr, SocketAddr}, sync::{ atomic::{AtomicU64, Ordering}, @@ -172,8 +172,7 @@ impl ConnectionCacheStats { } struct ConnectionMap { - map: HashMap, - list_of_peers: Vec, + map: IndexMap, stats: Arc, last_stats: AtomicInterval, use_quic: bool, @@ -182,8 +181,7 @@ struct ConnectionMap { impl ConnectionMap { pub fn new() -> Self { Self { - map: HashMap::with_capacity(MAX_CONNECTIONS), - list_of_peers: vec![], + map: IndexMap::with_capacity(MAX_CONNECTIONS), stats: Arc::new(ConnectionCacheStats::default()), last_stats: AtomicInterval::default(), use_quic: false, @@ -270,17 +268,15 @@ fn get_or_add_connection(addr: &SocketAddr) -> GetConnectionResult { let mut num_evictions = 0; let mut 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 n = rng.gen_range(0, MAX_CONNECTIONS); - let nth_addr = map.list_of_peers.swap_remove(n); - map.map.remove(&nth_addr); + map.map.swap_remove_index(n); num_evictions += 1; } get_connection_cache_eviction_measure.stop(); map.map.insert(*addr, connection.clone()); - map.list_of_peers.push(*addr); ( connection, false, diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index 2a5c3fa97b..3f6eb4a76f 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -4213,6 +4213,7 @@ dependencies = [ "crossbeam-channel", "futures 0.3.21", "futures-util", + "indexmap", "indicatif", "itertools", "jsonrpc-core",