Skip to content

Commit f9be3f2

Browse files
committed
Do not store cid multiple times in wantlist
1 parent ff5ea3d commit f9be3f2

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

bitswap/client/wantlist/wantlist.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import (
1212

1313
// Wantlist is a raw list of wanted blocks and their priorities
1414
type Wantlist struct {
15-
set map[cid.Cid]Entry
15+
set map[cid.Cid]entry
1616

1717
// Re-computing this can get expensive so we memoize it.
1818
cached []Entry
1919
}
2020

21+
type entry struct {
22+
Priority int32
23+
WantType pb.Message_Wantlist_WantType
24+
}
25+
2126
// Entry is an entry in a want list, consisting of a cid and its priority
2227
type Entry struct {
2328
Cid cid.Cid
@@ -37,7 +42,7 @@ func NewRefEntry(c cid.Cid, p int32) Entry {
3742
// New generates a new raw Wantlist
3843
func New() *Wantlist {
3944
return &Wantlist{
40-
set: make(map[cid.Cid]Entry),
45+
set: make(map[cid.Cid]entry),
4146
}
4247
}
4348

@@ -55,8 +60,7 @@ func (w *Wantlist) Add(c cid.Cid, priority int32, wantType pb.Message_Wantlist_W
5560
return false
5661
}
5762

58-
w.put(c, Entry{
59-
Cid: c,
63+
w.put(c, entry{
6064
Priority: priority,
6165
WantType: wantType,
6266
})
@@ -91,7 +95,7 @@ func (w *Wantlist) delete(c cid.Cid) {
9195
w.cached = nil
9296
}
9397

94-
func (w *Wantlist) put(c cid.Cid, e Entry) {
98+
func (w *Wantlist) put(c cid.Cid, e entry) {
9599
w.cached = nil
96100
w.set[c] = e
97101
}
@@ -105,7 +109,11 @@ func (w *Wantlist) Has(c cid.Cid) bool {
105109
// present.
106110
func (w *Wantlist) Get(c cid.Cid) (Entry, bool) {
107111
e, ok := w.set[c]
108-
return e, ok
112+
return Entry{
113+
Cid: c,
114+
Priority: e.Priority,
115+
WantType: e.WantType,
116+
}, ok
109117
}
110118

111119
// Entries returns all wantlist entries for a want list, sorted by priority.
@@ -116,8 +124,12 @@ func (w *Wantlist) Entries() []Entry {
116124
return w.cached
117125
}
118126
es := make([]Entry, 0, len(w.set))
119-
for _, e := range w.set {
120-
es = append(es, e)
127+
for c, e := range w.set {
128+
es = append(es, Entry{
129+
Cid: c,
130+
Priority: e.Priority,
131+
WantType: e.WantType,
132+
})
121133
}
122134
slices.SortFunc(es, func(a, b Entry) int {
123135
return cmp.Compare(b.Priority, a.Priority)

0 commit comments

Comments
 (0)