Commit bd024d24 authored by Steven Allen's avatar Steven Allen

bitswap: better wantlist allocation patterns

License: MIT
Signed-off-by: default avatarSteven Allen <steven@stebalien.com>
parent 75294d22
......@@ -451,8 +451,9 @@ func (bs *Bitswap) Close() error {
}
func (bs *Bitswap) GetWantlist() []*cid.Cid {
var out []*cid.Cid
for _, e := range bs.wm.wl.Entries() {
entries := bs.wm.wl.Entries()
out := make([]*cid.Cid, 0, len(entries))
for _, e := range entries {
out = append(out, e.Cid)
}
return out
......
......@@ -126,7 +126,7 @@ func (w *ThreadSafe) Contains(k *cid.Cid) (*Entry, bool) {
func (w *ThreadSafe) Entries() []*Entry {
w.lk.RLock()
defer w.lk.RUnlock()
var es entrySlice
es := make([]*Entry, 0, len(w.set))
for _, e := range w.set {
es = append(es, e)
}
......@@ -134,13 +134,8 @@ func (w *ThreadSafe) Entries() []*Entry {
}
func (w *ThreadSafe) SortedEntries() []*Entry {
w.lk.RLock()
defer w.lk.RUnlock()
var es entrySlice
for _, e := range w.set {
es = append(es, e)
}
sort.Sort(es)
es := w.Entries()
sort.Sort(entrySlice(es))
return es
}
......@@ -194,7 +189,7 @@ func (w *Wantlist) Contains(k *cid.Cid) (*Entry, bool) {
}
func (w *Wantlist) Entries() []*Entry {
var es entrySlice
es := make([]*Entry, 0, len(w.set))
for _, e := range w.set {
es = append(es, e)
}
......@@ -202,10 +197,7 @@ func (w *Wantlist) Entries() []*Entry {
}
func (w *Wantlist) SortedEntries() []*Entry {
var es entrySlice
for _, e := range w.set {
es = append(es, e)
}
sort.Sort(es)
es := w.Entries()
sort.Sort(entrySlice(es))
return es
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment