From 94de33696de68f82195394b43f17b6205907ebee Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 19 Oct 2017 17:53:00 +0800 Subject: [PATCH] raft: reduce allocation (#2391) --- src/raft/raft.rs | 28 ++++++++++++++++------------ src/raft/raw_node.rs | 12 ++++++++---- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/raft/raft.rs b/src/raft/raft.rs index 70e0582602c..85bd31c45a6 100644 --- a/src/raft/raft.rs +++ b/src/raft/raft.rs @@ -32,12 +32,11 @@ use rand::{self, Rng}; use kvproto::eraftpb::{Entry, EntryType, HardState, Message, MessageType, Snapshot}; use protobuf::repeated::RepeatedField; -use raft::storage::Storage; -use raft::progress::{Inflights, Progress, ProgressState}; -use raft::errors::{Error, Result, StorageError}; -use raft::raft_log::{self, RaftLog}; -use raft::read_only::{ReadOnly, ReadOnlyOption, ReadState}; - +use super::storage::Storage; +use super::progress::{Inflights, Progress, ProgressState}; +use super::errors::{Error, Result, StorageError}; +use super::raft_log::{self, RaftLog}; +use super::read_only::{ReadOnly, ReadOnlyOption, ReadState}; use super::FlatMap; // CAMPAIGN_PRE_ELECTION represents the first phase of a normal election when @@ -600,16 +599,21 @@ impl Raft { // the commit index changed (in which case the caller should call // r.bcast_append). pub fn maybe_commit(&mut self) -> bool { - // TODO: optimize - let mut mis = Vec::with_capacity(self.prs.len()); - for p in self.prs.values() { - mis.push(p.matched); + let mut mis_arr = [0; 5]; + let mut mis_vec; + let mis = if self.prs.len() <= 5 { + &mut mis_arr[..self.prs.len()] + } else { + mis_vec = vec![0; self.prs.len()]; + mis_vec.as_mut_slice() + }; + for (i, pr) in self.prs.values().enumerate() { + mis[i] = pr.matched; } // reverse sort mis.sort_by(|a, b| b.cmp(a)); let mci = mis[self.quorum() - 1]; - let term = self.term; - self.raft_log.maybe_commit(mci, term) + self.raft_log.maybe_commit(mci, self.term) } pub fn reset(&mut self, term: u64) { diff --git a/src/raft/raw_node.rs b/src/raft/raw_node.rs index 61ee1a23c88..92cd6dc4fb2 100644 --- a/src/raft/raw_node.rs +++ b/src/raft/raw_node.rs @@ -26,14 +26,16 @@ // limitations under the License. +use std::mem; + use raft::errors::{Error, Result}; use raft::Storage; use protobuf::{self, RepeatedField}; use kvproto::eraftpb::{ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType, Snapshot}; -use raft::raft::{Config, Raft, SoftState, INVALID_ID}; -use raft::Status; -use raft::read_only::ReadState; +use super::raft::{Config, Raft, SoftState, INVALID_ID}; +use super::Status; +use super::read_only::ReadState; #[derive(Debug, Default)] pub struct Peer { @@ -126,9 +128,11 @@ impl Ready { ) -> Ready { let mut rd = Ready { entries: raft.raft_log.unstable_entries().unwrap_or(&[]).to_vec(), - messages: raft.msgs.drain(..).collect(), ..Default::default() }; + if !raft.msgs.is_empty() { + mem::swap(&mut raft.msgs, &mut rd.messages); + } rd.committed_entries = Some( (match since_idx { None => raft.raft_log.next_entries(),