Skip to content

Commit

Permalink
raft: add Log.has() fast path for the last index
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 4, 2024
1 parent 83effb2 commit 1bdc337
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/raft/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ impl Log {

/// Checks if the log contains an entry with the given index and term.
pub fn has(&mut self, index: Index, term: Term) -> Result<bool> {
// Fast path: check against last_index. This is the common case when
// followers process appends or heartbeats.
if index == 0 || index > self.last_index {
return Ok(false);
}
if (index, term) == (self.last_index, self.last_term) {
return Ok(true);
}
Ok(self.get(index)?.map(|e| e.term == term).unwrap_or(false))
}

Expand Down

0 comments on commit 1bdc337

Please sign in to comment.