Browse Source

Merge pull request #1806 from xiang90/no_copy

No copy
Xiang Li 11 years ago
parent
commit
19ccdbee18
4 changed files with 22 additions and 16 deletions
  1. 1 2
      raft/log.go
  2. 15 10
      raft/log_unstable.go
  3. 4 2
      raft/node_bench_test.go
  4. 2 2
      raft/storage.go

+ 1 - 2
raft/log.go

@@ -123,8 +123,7 @@ func (l *raftLog) unstableEntries() []pb.Entry {
 	if len(l.unstable.entries) == 0 {
 		return nil
 	}
-	// copy unstable entries to an empty slice
-	return append([]pb.Entry{}, l.unstable.entries...)
+	return l.unstable.entries
 }
 
 // nextEnts returns all the available entries for execution.

+ 15 - 10
raft/log_unstable.go

@@ -99,18 +99,23 @@ func (u *unstable) restore(s pb.Snapshot) {
 	u.snapshot = &s
 }
 
-func (u *unstable) resetEntries(offset uint64) {
-	u.entries = nil
-	u.offset = offset
-}
-
 func (u *unstable) truncateAndAppend(after uint64, ents []pb.Entry) {
-	if after < u.offset {
-		// The log is being truncated to before our current unstable
-		// portion, so discard it and reset unstable.
-		u.resetEntries(after + 1)
+	switch {
+	case after < u.offset:
+		// The log is being truncated to before our current offset
+		// portion, so set the offset and replace the entries
+		u.offset = after + 1
+		u.entries = ents
+	case after == u.offset+uint64(len(u.entries))-1:
+		// after is the last index in the u.entries
+		// directly append
+		u.entries = append(u.entries, ents...)
+	default:
+		// truncate to after and copy to u.entries
+		// then append
+		u.entries = append([]pb.Entry{}, u.slice(u.offset, after+1)...)
+		u.entries = append(u.entries, ents...)
 	}
-	u.entries = append(u.slice(u.offset, after+1), ents...)
 }
 
 func (u *unstable) slice(lo uint64, hi uint64) []pb.Entry {

+ 4 - 2
raft/node_bench_test.go

@@ -27,14 +27,16 @@ func BenchmarkOneNode(b *testing.B) {
 	defer cancel()
 
 	n := newNode()
-	r := newRaft(1, []uint64{1}, 10, 1, nil)
+	s := NewMemoryStorage()
+	r := newRaft(1, []uint64{1}, 10, 1, s)
 	go n.run(r)
 
 	defer n.Stop()
 
 	n.Campaign(ctx)
 	for i := 0; i < b.N; i++ {
-		<-n.Ready()
+		rd := <-n.Ready()
+		s.Append(rd.Entries)
 		n.Advance()
 		n.Propose(ctx, []byte("foo"))
 	}

+ 2 - 2
raft/storage.go

@@ -184,8 +184,8 @@ func (ms *MemoryStorage) Append(entries []pb.Entry) {
 	if offset < 0 {
 		return
 	}
-	if uint64(len(ms.ents)) >= offset {
-		ms.ents = ms.ents[:offset]
+	if uint64(len(ms.ents)) > offset {
+		ms.ents = append([]pb.Entry{}, ms.ents[:offset]...)
 	}
 	ms.ents = append(ms.ents, entries...)
 }