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",
"futures 0.3.21",
"futures-util",
"indexmap",
"indicatif",
"itertools",
"jsonrpc-core",

View File

@ -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"

View File

@ -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<SocketAddr, Connection>,
list_of_peers: Vec<SocketAddr>,
map: IndexMap<SocketAddr, Connection>,
stats: Arc<ConnectionCacheStats>,
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,

View File

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