From 6a489618950e6351d3c8b6b58e8495828aaae98c Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Fri, 24 Jun 2016 23:46:20 -0700 Subject: [PATCH] raft: len(entries) before Lock, use firstIndex - To avoid unnecessary locking in case len(entries) == 0 - use firstIndex method --- raft/storage.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/raft/storage.go b/raft/storage.go index a5f2d28ea..37f6b378e 100644 --- a/raft/storage.go +++ b/raft/storage.go @@ -226,12 +226,14 @@ func (ms *MemoryStorage) Compact(compactIndex uint64) error { // TODO (xiangli): ensure the entries are continuous and // entries[0].Index > ms.entries[0].Index func (ms *MemoryStorage) Append(entries []pb.Entry) error { - ms.Lock() - defer ms.Unlock() if len(entries) == 0 { return nil } - first := ms.ents[0].Index + 1 + + ms.Lock() + defer ms.Unlock() + + first := ms.firstIndex() last := entries[0].Index + uint64(len(entries)) - 1 // shortcut if there is no new entry.