Skip to content

Commit 608cdd8

Browse files
committed
Store commit message in linked list instead of map
1 parent 83773a7 commit 608cdd8

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

lib/grandpa/commits_tracker.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@ import (
1414
// its maximum capacity is reached.
1515
// It is NOT THREAD SAFE to use.
1616
type commitsTracker struct {
17-
// map of commit block hash to data
18-
// data = message + tracking linked list element pointer
19-
mapping map[common.Hash]commitMessageMapData
20-
// double linked list of block hash
17+
// map of commit block hash to linked list commit message.
18+
mapping map[common.Hash]*list.Element
19+
// double linked list of commit messages
2120
// to track the order commit messages were added in.
2221
linkedList *list.List
2322
capacity int
2423
}
2524

26-
type commitMessageMapData struct {
27-
message *CommitMessage
28-
// element contains a block hash value.
29-
element *list.Element
30-
}
31-
3225
// newCommitsTracker creates a new commit messages tracker
3326
// with the capacity specified.
3427
func newCommitsTracker(capacity int) commitsTracker {
3528
return commitsTracker{
36-
mapping: make(map[common.Hash]commitMessageMapData, capacity),
29+
mapping: make(map[common.Hash]*list.Element, capacity),
3730
linkedList: list.New(),
3831
capacity: capacity,
3932
}
@@ -45,26 +38,21 @@ func newCommitsTracker(capacity int) commitsTracker {
4538
func (ct *commitsTracker) add(commitMessage *CommitMessage) {
4639
blockHash := commitMessage.Vote.Hash
4740

48-
data, has := ct.mapping[blockHash]
41+
listElement, has := ct.mapping[blockHash]
4942
if has {
50-
// commit already exists so override the commit for the block hash;
43+
// commit already exists so override the commit message in the linked list;
5144
// do not move the list element in the linked list to avoid
5245
// someone re-sending the same commit message and going at the
5346
// front of the list, hence erasing other possible valid commit messages
5447
// in the tracker.
55-
data.message = commitMessage
56-
ct.mapping[blockHash] = data
48+
listElement.Value = commitMessage
5749
return
5850
}
5951

6052
// add new block hash in tracker
6153
ct.cleanup()
62-
element := ct.linkedList.PushFront(blockHash)
63-
data = commitMessageMapData{
64-
message: commitMessage,
65-
element: element,
66-
}
67-
ct.mapping[blockHash] = data
54+
listElement = ct.linkedList.PushFront(commitMessage)
55+
ct.mapping[blockHash] = listElement
6856
}
6957

7058
// cleanup removes the oldest commit message from the tracker
@@ -79,40 +67,42 @@ func (ct *commitsTracker) cleanup() {
7967
oldestElement := ct.linkedList.Back()
8068
ct.linkedList.Remove(oldestElement)
8169

82-
oldestBlockHash := oldestElement.Value.(common.Hash)
70+
oldestCommitMessage := oldestElement.Value.(*CommitMessage)
71+
oldestBlockHash := oldestCommitMessage.Vote.Hash
8372
delete(ct.mapping, oldestBlockHash)
8473
}
8574

8675
// delete deletes all the vote messages for a particular
8776
// block hash from the vote messages tracker.
8877
func (ct *commitsTracker) delete(blockHash common.Hash) {
89-
data, has := ct.mapping[blockHash]
78+
listElement, has := ct.mapping[blockHash]
9079
if !has {
9180
return
9281
}
9382

94-
ct.linkedList.Remove(data.element)
83+
ct.linkedList.Remove(listElement)
9584
delete(ct.mapping, blockHash)
9685
}
9786

9887
// message returns a pointer to the
9988
// commit message for a particular block hash from
10089
// the tracker. It returns nil if the block hash
10190
// does not exist in the tracker
102-
func (ct *commitsTracker) message(
103-
blockHash common.Hash) (message *CommitMessage) {
104-
data, ok := ct.mapping[blockHash]
91+
func (ct *commitsTracker) message(blockHash common.Hash) (
92+
message *CommitMessage) {
93+
listElement, ok := ct.mapping[blockHash]
10594
if !ok {
10695
return nil
10796
}
10897

109-
return data.message
98+
return listElement.Value.(*CommitMessage)
11099
}
111100

112101
// forEach runs the function `f` on each
113102
// commit message stored in the tracker.
114103
func (ct *commitsTracker) forEach(f func(message *CommitMessage)) {
115104
for _, data := range ct.mapping {
116-
f(data.message)
105+
message := data.Value.(*CommitMessage)
106+
f(message)
117107
}
118108
}

lib/grandpa/commits_tracker_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ func buildCommitMessage(blockHash common.Hash) *CommitMessage {
2626
}
2727

2828
func assertCommitsMapping(t *testing.T,
29-
mapping map[common.Hash]commitMessageMapData,
29+
mapping map[common.Hash]*list.Element,
3030
expected map[common.Hash]*CommitMessage) {
3131
t.Helper()
3232

3333
require.Len(t, mapping, len(expected), "mapping does not have the expected length")
3434
for expectedBlockHash, expectedCommitMessage := range expected {
35-
data, ok := mapping[expectedBlockHash]
35+
listElement, ok := mapping[expectedBlockHash]
3636
assert.Truef(t, ok, "block hash %s not found in mapping", expectedBlockHash)
37-
assert.Equalf(t, expectedCommitMessage, data.message,
37+
assert.Equalf(t, expectedCommitMessage, listElement.Value.(*CommitMessage),
3838
"commit message for block hash %s is not as expected",
3939
expectedBlockHash)
4040
}
@@ -45,7 +45,7 @@ func Test_newCommitsTracker(t *testing.T) {
4545

4646
const capacity = 1
4747
expected := commitsTracker{
48-
mapping: make(map[common.Hash]commitMessageMapData, capacity),
48+
mapping: make(map[common.Hash]*list.Element, capacity),
4949
linkedList: list.New(),
5050
capacity: capacity,
5151
}
@@ -197,17 +197,17 @@ func Test_commitsTracker_message(t *testing.T) {
197197
}{
198198
"non existing block hash": {
199199
commitsTracker: &commitsTracker{
200-
mapping: map[common.Hash]commitMessageMapData{
200+
mapping: map[common.Hash]*list.Element{
201201
{1}: {},
202202
},
203203
},
204204
blockHash: common.Hash{2},
205205
},
206206
"existing block hash": {
207207
commitsTracker: &commitsTracker{
208-
mapping: map[common.Hash]commitMessageMapData{
208+
mapping: map[common.Hash]*list.Element{
209209
{1}: {
210-
message: &CommitMessage{Round: 1},
210+
Value: &CommitMessage{Round: 1},
211211
},
212212
},
213213
},
@@ -272,7 +272,7 @@ func Benchmark_ForEachVsSlice(b *testing.B) {
272272
getMessages := func(ct *commitsTracker) (messages []*CommitMessage) {
273273
messages = make([]*CommitMessage, 0, len(ct.mapping))
274274
for _, data := range ct.mapping {
275-
messages = append(messages, data.message)
275+
messages = append(messages, data.Value.(*CommitMessage))
276276
}
277277
return messages
278278
}
@@ -285,15 +285,15 @@ func Benchmark_ForEachVsSlice(b *testing.B) {
285285
const trackerSize = 10e4
286286
makeSeededTracker := func() (ct *commitsTracker) {
287287
ct = &commitsTracker{
288-
mapping: make(map[common.Hash]commitMessageMapData),
288+
mapping: make(map[common.Hash]*list.Element),
289289
}
290290
for i := 0; i < trackerSize; i++ {
291291
hashBytes := make([]byte, 32)
292292
_, _ = rand.Read(hashBytes)
293293
var blockHash common.Hash
294294
copy(blockHash[:], hashBytes)
295-
ct.mapping[blockHash] = commitMessageMapData{
296-
message: &CommitMessage{
295+
ct.mapping[blockHash] = &list.Element{
296+
Value: &CommitMessage{
297297
Round: uint64(i),
298298
SetID: uint64(i),
299299
},

0 commit comments

Comments
 (0)