Skip to content

Commit

Permalink
raft: reduce allocation (tikv#2391)
Browse files Browse the repository at this point in the history
  • Loading branch information
BusyJay committed Oct 30, 2017
1 parent 64bffdf commit 94de336
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
28 changes: 16 additions & 12 deletions src/raft/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions src/raft/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 94de336

Please sign in to comment.