فهرست منبع

Replace HashMap with FxHashMap

ghassmo 4 سال پیش
والد
کامیت
937e06ec4f

+ 3 - 0
Cargo.lock

@@ -841,6 +841,7 @@ dependencies = [
  "darkfi",
  "easy-parallel",
  "futures",
+ "fxhash",
  "hash-db",
  "hex",
  "keccak-hasher",
@@ -1585,6 +1586,7 @@ dependencies = [
  "clap 3.1.6",
  "darkfi",
  "easy-parallel",
+ "fxhash",
  "log",
  "num-bigint",
  "num_cpus",
@@ -2888,6 +2890,7 @@ dependencies = [
  "darkfi",
  "easy-parallel",
  "futures",
+ "fxhash",
  "log",
  "rand 0.8.5",
  "serde_json",

+ 3 - 0
Cargo.toml

@@ -174,12 +174,15 @@ blockchain = [
 ]
 
 system = [
+	"fxhash",
     "rand",
 
     "async-runtime",
 ]
 
 net = [
+	"fxhash",
+
     "util",
     "system",
 ]

+ 1 - 0
bin/cashierd/Cargo.toml

@@ -27,6 +27,7 @@ num_cpus = "1.13.1"
 simplelog = "0.11.2"
 thiserror = "1.0.30"
 url = "2.2.2"
+fxhash = "0.2.1"
 
 # Encoding and parsing
 serde = {version = "1.0.136", features = ["derive"]}

+ 7 - 4
bin/cashierd/src/service/bridge.rs

@@ -1,9 +1,9 @@
-use std::collections::HashMap;
+use async_std::sync::{Arc, Mutex};
 
 use async_executor::Executor;
-use async_std::sync::{Arc, Mutex};
 use async_trait::async_trait;
 use futures::stream::{FuturesUnordered, StreamExt};
+use fxhash::FxHashMap;
 use log::{debug, error};
 
 use darkfi::{
@@ -64,13 +64,16 @@ pub struct TokenNotification {
 }
 
 pub struct Bridge {
-    clients: Mutex<HashMap<NetworkName, Arc<dyn NetworkClient + Send + Sync>>>,
+    clients: Mutex<FxHashMap<NetworkName, Arc<dyn NetworkClient + Send + Sync>>>,
     notifiers: FuturesUnordered<async_channel::Receiver<TokenNotification>>,
 }
 
 impl Bridge {
     pub fn new() -> Arc<Self> {
-        Arc::new(Self { clients: Mutex::new(HashMap::new()), notifiers: FuturesUnordered::new() })
+        Arc::new(Self {
+            clients: Mutex::new(FxHashMap::default()),
+            notifiers: FuturesUnordered::new(),
+        })
     }
 
     pub async fn add_clients(

+ 1 - 0
bin/darkfid/Cargo.toml

@@ -22,6 +22,7 @@ url = "2.2.2"
 log = "0.4.14"
 num_cpus = "1.13.1"
 simplelog = "0.11.2"
+fxhash = "0.2.1"
 
 # Encoding and parsing
 serde_json = "1.0.79"

+ 4 - 3
bin/darkfid/src/main.rs

@@ -1,10 +1,11 @@
-use std::{collections::HashMap, path::PathBuf, str::FromStr};
+use std::{path::PathBuf, str::FromStr};
 
 use async_executor::Executor;
 use async_std::sync::{Arc, Mutex};
 use async_trait::async_trait;
 use clap::{IntoApp, Parser};
 use easy_parallel::Parallel;
+use fxhash::FxHashMap;
 use log::{debug, info};
 use num_bigint::BigUint;
 use serde::{Deserialize, Serialize};
@@ -392,9 +393,9 @@ impl Darkfid {
     // --> {"jsonrpc": "2.0", "method": "get_balances", "params": [], "id": 1}
     // <-- {"jsonrpc": "2.0", "result": [{"btc": [100, "Bitcoin"]}, {...}], "id": 1}
     async fn get_balances(&self, id: Value, _params: Value) -> JsonResult {
-        let result: Result<HashMap<String, (String, String)>> = async {
+        let result: Result<FxHashMap<String, (String, String)>> = async {
             let balances = self.client.lock().await.get_balances().await?;
-            let mut symbols: HashMap<String, (String, String)> = HashMap::new();
+            let mut symbols: FxHashMap<String, (String, String)> = FxHashMap::default();
 
             for b in balances.list.iter() {
                 let network: String;

+ 1 - 0
bin/ircd/Cargo.toml

@@ -24,6 +24,7 @@ rand = "0.8.5"
 clap = "3.1.6"
 log = "0.4.14"
 simplelog = "0.11.2"
+fxhash = "0.2.1"
 
 # Encoding and parsing
 serde_json = "1.0.79"

+ 5 - 4
bin/ircd/src/privmsg.rs

@@ -1,6 +1,7 @@
-use std::{collections::HashSet, io, sync::Arc};
-
 use async_std::sync::Mutex;
+use std::{io, sync::Arc};
+
+use fxhash::FxHashSet;
 
 use darkfi::{
     net,
@@ -47,14 +48,14 @@ impl Decodable for PrivMsg {
 }
 
 pub struct SeenPrivMsgIds {
-    privmsg_ids: Mutex<HashSet<PrivMsgId>>,
+    privmsg_ids: Mutex<FxHashSet<PrivMsgId>>,
 }
 
 pub type SeenPrivMsgIdsPtr = Arc<SeenPrivMsgIds>;
 
 impl SeenPrivMsgIds {
     pub fn new() -> Arc<Self> {
-        Arc::new(Self { privmsg_ids: Mutex::new(HashSet::new()) })
+        Arc::new(Self { privmsg_ids: Mutex::new(FxHashSet::default()) })
     }
 
     pub async fn add_seen(&self, id: u32) {

+ 11 - 11
src/crypto/token_list.rs

@@ -1,5 +1,4 @@
-use std::collections::HashMap;
-
+use fxhash::FxHashMap;
 use serde_json::Value;
 
 use crate::{
@@ -57,7 +56,7 @@ impl TokenList {
 
 #[derive(Debug, Clone)]
 pub struct DrkTokenList {
-    pub tokens: HashMap<NetworkName, HashMap<String, DrkTokenId>>,
+    pub tokens: FxHashMap<NetworkName, FxHashMap<String, DrkTokenId>>,
 }
 
 impl DrkTokenList {
@@ -66,32 +65,33 @@ impl DrkTokenList {
         let eth_symbols = eth_list.get_symbols()?;
         let btc_symbols = btc_list.get_symbols()?;
 
-        let sol_tokens: HashMap<String, DrkTokenId> = sol_symbols
+        let sol_tokens: FxHashMap<String, DrkTokenId> = sol_symbols
             .iter()
             .filter_map(|symbol| {
                 Self::generate_hash_pair(sol_list, &NetworkName::Solana, symbol).ok()
             })
             .collect();
 
-        let eth_tokens: HashMap<String, DrkTokenId> = eth_symbols
+        let eth_tokens: FxHashMap<String, DrkTokenId> = eth_symbols
             .iter()
             .filter_map(|symbol| {
                 Self::generate_hash_pair(eth_list, &NetworkName::Ethereum, symbol).ok()
             })
             .collect();
 
-        let btc_tokens: HashMap<String, DrkTokenId> = btc_symbols
+        let btc_tokens: FxHashMap<String, DrkTokenId> = btc_symbols
             .iter()
             .filter_map(|symbol| {
                 Self::generate_hash_pair(btc_list, &NetworkName::Bitcoin, symbol).ok()
             })
             .collect();
 
-        let tokens: HashMap<NetworkName, HashMap<String, DrkTokenId>> = HashMap::from([
-            (NetworkName::Solana, sol_tokens),
-            (NetworkName::Ethereum, eth_tokens),
-            (NetworkName::Bitcoin, btc_tokens),
-        ]);
+        let mut tokens: FxHashMap<NetworkName, FxHashMap<String, DrkTokenId>> =
+            FxHashMap::default();
+
+        tokens.insert(NetworkName::Solana, sol_tokens);
+        tokens.insert(NetworkName::Ethereum, eth_tokens);
+        tokens.insert(NetworkName::Bitcoin, btc_tokens);
 
         Ok(Self { tokens })
     }

+ 4 - 2
src/net/hosts.rs

@@ -1,6 +1,8 @@
 use async_std::sync::Mutex;
+use std::{net::SocketAddr, sync::Arc};
+
+use fxhash::FxHashSet;
 use rand::seq::SliceRandom;
-use std::{collections::HashSet, net::SocketAddr, sync::Arc};
 
 /// Pointer to hosts class.
 pub type HostsPtr = Arc<Hosts>;
@@ -18,7 +20,7 @@ impl Hosts {
 
     /// Checks if a host address is in the host list.
     async fn contains(&self, addrs: &[SocketAddr]) -> bool {
-        let a_set: HashSet<_> = addrs.iter().copied().collect();
+        let a_set: FxHashSet<_> = addrs.iter().copied().collect();
         self.addrs.lock().await.iter().any(|item| a_set.contains(item))
     }
 

+ 7 - 5
src/net/message_subscriber.rs

@@ -1,8 +1,10 @@
 use async_std::sync::Mutex;
+use std::{any::Any, io, io::Cursor, sync::Arc};
+
 use async_trait::async_trait;
+use fxhash::FxHashMap;
 use log::{debug, error, warn};
 use rand::Rng;
-use std::{any::Any, collections::HashMap, io, io::Cursor, sync::Arc};
 
 use crate::{
     net::message::Message,
@@ -52,13 +54,13 @@ trait MessageDispatcherInterface: Send + Sync {
 /// Maintains a list of active subscribers and handles sending messages across
 /// subscriptions.
 struct MessageDispatcher<M: Message> {
-    subs: Mutex<HashMap<MessageSubscriptionId, async_channel::Sender<MessageResult<M>>>>,
+    subs: Mutex<FxHashMap<MessageSubscriptionId, async_channel::Sender<MessageResult<M>>>>,
 }
 
 impl<M: Message> MessageDispatcher<M> {
     /// Create a new message dispatcher.
     fn new() -> Self {
-        MessageDispatcher { subs: Mutex::new(HashMap::new()) }
+        MessageDispatcher { subs: Mutex::new(FxHashMap::default()) }
     }
 
     /// Create a random ID.
@@ -161,13 +163,13 @@ impl<M: Message> MessageDispatcherInterface for MessageDispatcher<M> {
 /// Publish/subscribe class that can dispatch any kind of message to a
 /// list of dispatchers.
 pub struct MessageSubsystem {
-    dispatchers: Mutex<HashMap<&'static str, Arc<dyn MessageDispatcherInterface>>>,
+    dispatchers: Mutex<FxHashMap<&'static str, Arc<dyn MessageDispatcherInterface>>>,
 }
 
 impl MessageSubsystem {
     /// Create a new message subsystem.
     pub fn new() -> Self {
-        MessageSubsystem { dispatchers: Mutex::new(HashMap::new()) }
+        MessageSubsystem { dispatchers: Mutex::new(FxHashMap::default()) }
     }
 
     /// Add a new message dispatcher.

+ 6 - 10
src/net/p2p.rs

@@ -1,12 +1,8 @@
 use async_std::sync::Mutex;
-use std::{
-    collections::{HashMap, HashSet},
-    fmt,
-    net::SocketAddr,
-    sync::Arc,
-};
+use std::{fmt, net::SocketAddr, sync::Arc};
 
 use async_executor::Executor;
+use fxhash::{FxHashMap, FxHashSet};
 use log::debug;
 use serde_json::json;
 
@@ -22,9 +18,9 @@ use crate::{
 };
 
 /// List of channels that are awaiting connection.
-pub type PendingChannels = Mutex<HashSet<SocketAddr>>;
+pub type PendingChannels = Mutex<FxHashSet<SocketAddr>>;
 /// List of connected channels.
-pub type ConnectedChannels<T> = Mutex<HashMap<SocketAddr, Arc<T>>>;
+pub type ConnectedChannels<T> = Mutex<fxhash::FxHashMap<SocketAddr, Arc<T>>>;
 /// Atomic pointer to p2p interface.
 pub type P2pPtr = Arc<P2p>;
 
@@ -80,8 +76,8 @@ impl P2p {
         let settings = Arc::new(settings);
 
         let self_ = Arc::new(Self {
-            pending: Mutex::new(HashSet::new()),
-            channels: Mutex::new(HashMap::new()),
+            pending: Mutex::new(FxHashSet::default()),
+            channels: Mutex::new(FxHashMap::default()),
             channel_subscriber: Subscriber::new(),
             stop_subscriber: Subscriber::new(),
             hosts: Hosts::new(),

+ 4 - 4
src/net/session/inbound_session.rs

@@ -2,12 +2,12 @@ use async_std::sync::Mutex;
 use async_trait::async_trait;
 use serde_json::json;
 use std::{
-    collections::HashMap,
     net::SocketAddr,
     sync::{Arc, Weak},
 };
 
 use async_executor::Executor;
+use fxhash::FxHashMap;
 use log::{error, info};
 
 use crate::{
@@ -34,7 +34,7 @@ pub struct InboundSession {
     p2p: Weak<P2p>,
     acceptor: AcceptorPtr,
     accept_task: StoppableTaskPtr,
-    connect_infos: Mutex<HashMap<SocketAddr, InboundInfo>>,
+    connect_infos: Mutex<FxHashMap<SocketAddr, InboundInfo>>,
 }
 
 impl InboundSession {
@@ -46,7 +46,7 @@ impl InboundSession {
             p2p,
             acceptor,
             accept_task: StoppableTask::new(),
-            connect_infos: Mutex::new(HashMap::new()),
+            connect_infos: Mutex::new(FxHashMap::default()),
         })
     }
 
@@ -136,7 +136,7 @@ impl InboundSession {
 #[async_trait]
 impl Session for InboundSession {
     async fn get_info(&self) -> serde_json::Value {
-        let mut infos = HashMap::new();
+        let mut infos = FxHashMap::default();
         for (addr, info) in self.connect_infos.lock().await.iter() {
             infos.insert(addr.to_string(), info.get_info().await);
         }

+ 5 - 3
src/system/subscriber.rs

@@ -1,6 +1,8 @@
 use async_std::sync::Mutex;
+use std::sync::Arc;
+
+use fxhash::FxHashMap;
 use rand::Rng;
-use std::{collections::HashMap, sync::Arc};
 
 pub type SubscriberPtr<T> = Arc<Subscriber<T>>;
 
@@ -32,12 +34,12 @@ impl<T: Clone> Subscription<T> {
 
 // Simple broadcast (publish-subscribe) class
 pub struct Subscriber<T> {
-    subs: Mutex<HashMap<u64, async_channel::Sender<T>>>,
+    subs: Mutex<FxHashMap<u64, async_channel::Sender<T>>>,
 }
 
 impl<T: Clone> Subscriber<T> {
     pub fn new() -> Arc<Self> {
-        Arc::new(Self { subs: Mutex::new(HashMap::new()) })
+        Arc::new(Self { subs: Mutex::new(FxHashMap::default()) })
     }
 
     fn random_id() -> SubscriptionId {