Browse Source

raft: using fxhashmap

ghassmo 4 years ago
parent
commit
be36b79927
2 changed files with 11 additions and 9 deletions
  1. 8 7
      src/raft/consensus.rs
  2. 3 2
      src/raft/primitives.rs

+ 8 - 7
src/raft/consensus.rs

@@ -2,10 +2,11 @@ use async_std::{
     sync::{Arc, Mutex},
     sync::{Arc, Mutex},
     task,
     task,
 };
 };
-use std::{cmp::min, collections::HashMap, path::PathBuf, time::Duration};
+use std::{cmp::min, path::PathBuf, time::Duration};
 
 
 use async_executor::Executor;
 use async_executor::Executor;
 use futures::{select, FutureExt};
 use futures::{select, FutureExt};
+use fxhash::FxHashMap;
 use log::{debug, error, info, warn};
 use log::{debug, error, info, warn};
 use rand::{rngs::OsRng, Rng, RngCore};
 use rand::{rngs::OsRng, Rng, RngCore};
 use url::Url;
 use url::Url;
@@ -33,7 +34,7 @@ const SYNC_TIMEOUT_FOR_EACH_ATTEMPT: u64 = 2000;
 const SYNC_ATTEMPTS: u64 = 30;
 const SYNC_ATTEMPTS: u64 = 30;
 
 
 async fn load_node_ids_loop(
 async fn load_node_ids_loop(
-    nodes: Arc<Mutex<HashMap<NodeId, Url>>>,
+    nodes: Arc<Mutex<FxHashMap<NodeId, Url>>>,
     p2p: net::P2pPtr,
     p2p: net::P2pPtr,
     role: Role,
     role: Role,
 ) -> Result<()> {
 ) -> Result<()> {
@@ -75,7 +76,7 @@ pub struct Raft<T> {
     sent_length: MapLength,
     sent_length: MapLength,
     acked_length: MapLength,
     acked_length: MapLength,
 
 
-    nodes: Arc<Mutex<HashMap<NodeId, Url>>>,
+    nodes: Arc<Mutex<FxHashMap<NodeId, Url>>>,
 
 
     last_term: u64,
     last_term: u64,
 
 
@@ -116,9 +117,9 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
             role,
             role,
             current_leader: None,
             current_leader: None,
             votes_received: vec![],
             votes_received: vec![],
-            sent_length: MapLength(HashMap::new()),
-            acked_length: MapLength(HashMap::new()),
-            nodes: Arc::new(Mutex::new(HashMap::new())),
+            sent_length: MapLength(FxHashMap::default()),
+            acked_length: MapLength(FxHashMap::default()),
+            nodes: Arc::new(Mutex::new(FxHashMap::default())),
             last_term: 0,
             last_term: 0,
             sender,
             sender,
             msgs_channel,
             msgs_channel,
@@ -599,7 +600,7 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
         Ok(())
         Ok(())
     }
     }
 
 
-    fn acks(&self, nodes: HashMap<NodeId, Url>, length: u64) -> HashMap<NodeId, Url> {
+    fn acks(&self, nodes: FxHashMap<NodeId, Url>, length: u64) -> FxHashMap<NodeId, Url> {
         nodes
         nodes
             .into_iter()
             .into_iter()
             .filter(|n| {
             .filter(|n| {

+ 3 - 2
src/raft/primitives.rs

@@ -1,5 +1,6 @@
-use std::{collections::HashMap, io};
+use std::io;
 
 
+use fxhash::FxHashMap;
 use url::Url;
 use url::Url;
 
 
 use crate::{
 use crate::{
@@ -132,7 +133,7 @@ impl Logs {
 }
 }
 
 
 #[derive(Clone, Debug)]
 #[derive(Clone, Debug)]
-pub struct MapLength(pub HashMap<NodeId, u64>);
+pub struct MapLength(pub FxHashMap<NodeId, u64>);
 
 
 impl MapLength {
 impl MapLength {
     pub fn get(&self, key: &NodeId) -> Result<u64> {
     pub fn get(&self, key: &NodeId) -> Result<u64> {