Skip to content

Commit

Permalink
Less allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Dec 3, 2023
1 parent 81edfcf commit be94f85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ TVolatileState& TVolatileState::Vote(uint32_t nodeId)
TVolatileState& TVolatileState::CommitAdvance(int nservers, const TState& state)
{
auto lastIndex = state.Log.size();
std::vector<uint64_t> indices; indices.reserve(nservers);
Indices.clear(); Indices.reserve(nservers);
for (auto [_, index] : MatchIndex) {
indices.push_back(index);
Indices.push_back(index);
}
indices.push_back(lastIndex);
while (indices.size() < nservers) {
indices.push_back(0);
Indices.push_back(lastIndex);
while (Indices.size() < nservers) {
Indices.push_back(0);
}
std::sort(indices.begin(), indices.end());
auto commitIndex = std::max(CommitIndex, indices[nservers / 2]);
std::sort(Indices.begin(), Indices.end());
auto commitIndex = std::max(CommitIndex, Indices[nservers / 2]);
if (state.LogTerm(commitIndex) == state.CurrentTerm) {
CommitIndex = commitIndex;
}
Expand Down
2 changes: 2 additions & 0 deletions src/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ struct TVolatileState {
std::unordered_map<uint32_t, int> BackOff;
ITimeSource::Time ElectionDue;

std::vector<uint64_t> Indices;

TVolatileState& Vote(uint32_t id);
TVolatileState& SetLastApplied(int index);
TVolatileState& CommitAdvance(int nservers, const TState& state);
Expand Down

0 comments on commit be94f85

Please sign in to comment.