Skip to content

Commit 7c0ca2e

Browse files
committed
store pointers to entry, not copy of entry
1 parent ff5ea3d commit 7c0ca2e

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

bitswap/client/wantlist/wantlist.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ 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.
18-
cached []Entry
18+
cached []*Entry
1919
}
2020

2121
// Entry is an entry in a want list, consisting of a cid and its priority
@@ -37,7 +37,7 @@ func NewRefEntry(c cid.Cid, p int32) Entry {
3737
// New generates a new raw Wantlist
3838
func New() *Wantlist {
3939
return &Wantlist{
40-
set: make(map[cid.Cid]Entry),
40+
set: make(map[cid.Cid]*Entry),
4141
}
4242
}
4343

@@ -55,7 +55,7 @@ func (w *Wantlist) Add(c cid.Cid, priority int32, wantType pb.Message_Wantlist_W
5555
return false
5656
}
5757

58-
w.put(c, Entry{
58+
w.put(c, &Entry{
5959
Cid: c,
6060
Priority: priority,
6161
WantType: wantType,
@@ -91,7 +91,7 @@ func (w *Wantlist) delete(c cid.Cid) {
9191
w.cached = nil
9292
}
9393

94-
func (w *Wantlist) put(c cid.Cid, e Entry) {
94+
func (w *Wantlist) put(c cid.Cid, e *Entry) {
9595
w.cached = nil
9696
w.set[c] = e
9797
}
@@ -103,23 +103,23 @@ func (w *Wantlist) Has(c cid.Cid) bool {
103103

104104
// Get returns the entry, if present, for the given CID, plus whether it was
105105
// present.
106-
func (w *Wantlist) Get(c cid.Cid) (Entry, bool) {
106+
func (w *Wantlist) Get(c cid.Cid) (*Entry, bool) {
107107
e, ok := w.set[c]
108108
return e, ok
109109
}
110110

111111
// Entries returns all wantlist entries for a want list, sorted by priority.
112112
//
113113
// DO NOT MODIFY. The returned list is cached.
114-
func (w *Wantlist) Entries() []Entry {
114+
func (w *Wantlist) Entries() []*Entry {
115115
if w.cached != nil {
116116
return w.cached
117117
}
118-
es := make([]Entry, 0, len(w.set))
118+
es := make([]*Entry, 0, len(w.set))
119119
for _, e := range w.set {
120120
es = append(es, e)
121121
}
122-
slices.SortFunc(es, func(a, b Entry) int {
122+
slices.SortFunc(es, func(a, b *Entry) int {
123123
return cmp.Compare(b.Priority, a.Priority)
124124
})
125125
w.cached = es

bitswap/client/wantlist/wantlist_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func init() {
2626
}
2727

2828
type wli interface {
29-
Get(cid.Cid) (Entry, bool)
29+
Get(cid.Cid) (*Entry, bool)
3030
Has(cid.Cid) bool
3131
}
3232

0 commit comments

Comments
 (0)