diff --git a/Cargo.toml b/Cargo.toml index 68221ffe2..3a984c499 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,12 +12,11 @@ description = "The rust language implementation of Raft algorithm." categories = ["algorithms", "database-implementations"] [dependencies] -flat_map = "0.0.4" -fnv = "1.0" log = "0.3" protobuf = "1.2" quick-error = "0.2" rand = "0.4" +fxhash = "0.2.1" [badges] travis-ci = { repository = "pingcap/raft-rs" } diff --git a/src/lib.rs b/src/lib.rs index cdcdea138..2343eb807 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,8 +29,7 @@ #![cfg_attr(feature = "dev", plugin(clippy))] #![cfg_attr(not(feature = "dev"), allow(unknown_lints))] -extern crate flat_map; -extern crate fnv; +extern crate fxhash; #[macro_use] extern crate log; extern crate protobuf; diff --git a/src/progress.rs b/src/progress.rs index 19a41ba6f..676c46369 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -26,9 +26,9 @@ // limitations under the License. use std::cmp; -use flat_map::FlatMap; -use flat_map::flat_map; use std::iter::Chain; +use std::collections::hash_map::{HashMap, Iter, IterMut}; +use fxhash::FxHashMap; #[derive(Debug, PartialEq, Clone, Copy)] pub enum ProgressState { @@ -47,23 +47,23 @@ impl Default for ProgressState { /// which could be `Leader`, `Follower` and `Learner`. #[derive(Default, Clone)] pub struct ProgressSet { - voters: FlatMap, - learners: FlatMap, + voters: FxHashMap, + learners: FxHashMap, } impl ProgressSet { pub fn new(voter_size: usize, learner_size: usize) -> Self { ProgressSet { - voters: FlatMap::with_capacity(voter_size), - learners: FlatMap::with_capacity(learner_size), + voters: HashMap::with_capacity_and_hasher(voter_size, Default::default()), + learners: HashMap::with_capacity_and_hasher(learner_size, Default::default()), } } - pub fn voters(&self) -> &FlatMap { + pub fn voters(&self) -> &FxHashMap { &self.voters } - pub fn learners(&self) -> &FlatMap { + pub fn learners(&self) -> &FxHashMap { &self.learners } @@ -93,13 +93,11 @@ impl ProgressSet { progress } - pub fn iter(&self) -> Chain, flat_map::Iter> { + pub fn iter(&self) -> Chain, Iter> { self.voters.iter().chain(&self.learners) } - pub fn iter_mut( - &mut self, - ) -> Chain, flat_map::IterMut> { + pub fn iter_mut(&mut self) -> Chain, IterMut> { self.voters.iter_mut().chain(&mut self.learners) } diff --git a/src/raft.rs b/src/raft.rs index 22f8d9f1f..e5ea4a865 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -29,6 +29,7 @@ use std::cmp; use rand::{self, Rng}; use eraftpb::{Entry, EntryType, HardState, Message, MessageType, Snapshot}; +use fxhash::FxHashMap; use protobuf::repeated::RepeatedField; use super::storage::Storage; @@ -36,7 +37,6 @@ use super::progress::{Inflights, Progress, ProgressSet, ProgressState}; use super::errors::{Error, Result, StorageError}; use super::raft_log::{self, RaftLog}; use super::read_only::{ReadOnly, ReadOnlyOption, ReadState}; -use flat_map::FlatMap; // CAMPAIGN_PRE_ELECTION represents the first phase of a normal election when // Config.pre_vote is true. @@ -192,7 +192,7 @@ pub struct Raft { pub is_learner: bool, - pub votes: FlatMap, + pub votes: FxHashMap, pub msgs: Vec, @@ -650,7 +650,7 @@ impl Raft { self.abort_leader_transfer(); - self.votes = FlatMap::default(); + self.votes = FxHashMap::default(); self.pending_conf_index = 0; self.read_only = ReadOnly::new(self.read_only.option); diff --git a/src/read_only.rs b/src/read_only.rs index be36ea9e5..0efcc88b0 100644 --- a/src/read_only.rs +++ b/src/read_only.rs @@ -29,7 +29,7 @@ use std::collections::VecDeque; use eraftpb::Message; -use fnv::{FnvHashMap, FnvHashSet}; +use fxhash::{FxHashMap, FxHashSet}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum ReadOnlyOption { @@ -65,13 +65,13 @@ pub struct ReadState { pub struct ReadIndexStatus { pub req: Message, pub index: u64, - pub acks: FnvHashSet, + pub acks: FxHashSet, } #[derive(Default, Debug, Clone)] pub struct ReadOnly { pub option: ReadOnlyOption, - pub pending_read_index: FnvHashMap, ReadIndexStatus>, + pub pending_read_index: FxHashMap, ReadIndexStatus>, pub read_index_queue: VecDeque>, } @@ -79,7 +79,7 @@ impl ReadOnly { pub fn new(option: ReadOnlyOption) -> ReadOnly { ReadOnly { option: option, - pending_read_index: FnvHashMap::default(), + pending_read_index: FxHashMap::default(), read_index_queue: VecDeque::new(), } } @@ -99,7 +99,7 @@ impl ReadOnly { let status = ReadIndexStatus { req: m, index: index, - acks: FnvHashSet::default(), + acks: FxHashSet::default(), }; self.pending_read_index.insert(ctx.clone(), status); self.read_index_queue.push_back(ctx); diff --git a/src/status.rs b/src/status.rs index 40fe6b43a..301bf8552 100644 --- a/src/status.rs +++ b/src/status.rs @@ -26,21 +26,20 @@ // limitations under the License. use eraftpb::HardState; +use fxhash::FxHashMap; use raft::{Raft, SoftState, StateRole}; use storage::Storage; use progress::Progress; -use flat_map::FlatMap; - #[derive(Default)] pub struct Status { pub id: u64, pub hs: HardState, pub ss: SoftState, pub applied: u64, - pub progress: FlatMap, - pub learner_progress: FlatMap, + pub progress: FxHashMap, + pub learner_progress: FxHashMap, } impl Status {