From c3d1d14f1628e415729e55809e3037b422ef16ad Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 20 May 2022 01:26:02 +0000 Subject: [PATCH] BTreeMap instead of HashMap --- web3-proxy-minimal/src/connections.rs | 22 ++++++---------------- web3-proxy/src/connections.rs | 6 +++--- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/web3-proxy-minimal/src/connections.rs b/web3-proxy-minimal/src/connections.rs index a9179287..363124a4 100644 --- a/web3-proxy-minimal/src/connections.rs +++ b/web3-proxy-minimal/src/connections.rs @@ -3,9 +3,9 @@ use arc_swap::ArcSwap; use derive_more::From; use ethers::prelude::H256; use futures::future::join_all; -use hashbrown::{HashMap, HashSet}; +use hashbrown::HashMap; use std::cmp; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fmt; use std::sync::Arc; use tokio::task; @@ -14,21 +14,11 @@ use tracing::{info, info_span, instrument, warn}; use crate::connection::Web3Connection; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] struct SyncedConnections { head_block_num: u64, head_block_hash: H256, - inner: HashSet, -} - -impl SyncedConnections { - fn new(max_connections: usize) -> Self { - Self { - head_block_num: 0, - head_block_hash: Default::default(), - inner: HashSet::with_capacity(max_connections), - } - } + inner: BTreeSet, } /// A collection of web3 connections. Sends requests either the current best server or all servers. @@ -72,7 +62,7 @@ impl Web3Connections { )); } - let synced_connections = SyncedConnections::new(num_connections); + let synced_connections = SyncedConnections::default(); let connections = Arc::new(Self { inner: connections, @@ -142,7 +132,7 @@ impl Web3Connections { let mut connection_states: HashMap = HashMap::with_capacity(max_connections); - let mut pending_synced_connections = SyncedConnections::new(max_connections); + let mut pending_synced_connections = SyncedConnections::default(); while let Ok((new_block_num, new_block_hash, rpc_id)) = block_receiver.recv_async().await { // TODO: span with rpc in it, too diff --git a/web3-proxy/src/connections.rs b/web3-proxy/src/connections.rs index 708d63c2..1bdb4462 100644 --- a/web3-proxy/src/connections.rs +++ b/web3-proxy/src/connections.rs @@ -7,10 +7,10 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; use governor::clock::{QuantaClock, QuantaInstant}; use governor::NotUntil; -use hashbrown::{HashMap, HashSet}; +use hashbrown::HashMap; use serde_json::value::RawValue; use std::cmp; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fmt; use std::sync::Arc; use tokio::task; @@ -24,7 +24,7 @@ use crate::connection::{ActiveRequestHandle, Web3Connection}; struct SyncedConnections { head_block_num: u64, head_block_hash: H256, - inner: HashSet, + inner: BTreeSet, } impl fmt::Debug for SyncedConnections {