Browse Source

raft: ins: []*index -> []index

It could make raft faster, use less storage.
Yicheng Qin 11 years ago
parent
commit
c1c45575be
2 changed files with 7 additions and 8 deletions
  1. 5 6
      raft/raft.go
  2. 2 2
      raft/raft_test.go

+ 5 - 6
raft/raft.go

@@ -95,7 +95,7 @@ type stateMachine struct {
 	// the log
 	log *log
 
-	ins []*index
+	ins []index
 
 	state stateType
 
@@ -178,9 +178,9 @@ func (sm *stateMachine) reset() {
 	sm.lead = none
 	sm.vote = none
 	sm.votes = make(map[int]bool)
-	sm.ins = make([]*index, sm.k)
+	sm.ins = make([]index, sm.k)
 	for i := range sm.ins {
-		sm.ins[i] = &index{next: sm.log.lastIndex() + 1}
+		sm.ins[i] = index{next: sm.log.lastIndex() + 1}
 	}
 }
 
@@ -273,12 +273,11 @@ func (sm *stateMachine) Step(m Message) {
 	case stateLeader:
 		switch m.Type {
 		case msgAppResp:
-			in := sm.ins[m.From]
 			if m.Index < 0 {
-				in.decr()
+				sm.ins[m.From].decr()
 				sm.sendAppend()
 			} else {
-				in.update(m.Index)
+				sm.ins[m.From].update(m.Index)
 				if sm.maybeCommit() {
 					sm.sendAppend()
 				}

+ 2 - 2
raft/raft_test.go

@@ -331,9 +331,9 @@ func TestCommit(t *testing.T) {
 	}
 
 	for i, tt := range tests {
-		ins := make([]*index, len(tt.matches))
+		ins := make([]index, len(tt.matches))
 		for j := 0; j < len(ins); j++ {
-			ins[j] = &index{tt.matches[j], tt.matches[j] + 1}
+			ins[j] = index{tt.matches[j], tt.matches[j] + 1}
 		}
 		sm := &stateMachine{log: &log{ents: tt.logs}, ins: ins, k: len(ins), term: tt.smTerm}
 		sm.maybeCommit()