Permalink
Browse files

raft: reduce allocation (tikv#2391)

  • Loading branch information...
BusyJay committed Oct 19, 2017
1 parent 64bffdf commit 94de33696de68f82195394b43f17b6205907ebee
Showing with 24 additions and 16 deletions.
  1. +16 −12 src/raft/raft.rs
  2. +8 −4 src/raft/raw_node.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<T: Storage> Raft<T> {
// 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) {
@@ -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(),

0 comments on commit 94de336

Please sign in to comment.