@cryptotaxi247 / kubo / commits / 0545c4d15

refactor: *Entry -> Entry

in many places, entries are assigned from one slice to another and in different goroutines. In one place, entries were modified (in the queue). To avoid shared mutable state, probably best to handle entries by value. License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 17, 2014 at 00:24 UTC 0545c4d15d4c680a01e6207c45a08480740fade9
2 files changed +14 -14
exchange/bitswap/message/message.go
+6 -6
@@ -19,7 +19,7 @@ import (
19 type BitSwapMessage interface {
20 // Wantlist returns a slice of unique keys that represent data wanted by
21 // the sender.
22 - Wantlist() []*Entry
22 + Wantlist() []Entry
23
24 // Blocks returns a slice of unique blocks
25 Blocks() []*blocks.Block
@@ -48,7 +48,7 @@ type Exportable interface {
48
49 type impl struct {
50 full bool
51 - wantlist map[u.Key]*Entry
51 + wantlist map[u.Key]Entry
52 blocks map[u.Key]*blocks.Block // map to detect duplicates
53 }
54
@@ -59,7 +59,7 @@ func New() BitSwapMessage {
59 func newMsg() *impl {
60 return &impl{
61 blocks: make(map[u.Key]*blocks.Block),
62 - wantlist: make(map[u.Key]*Entry),
62 + wantlist: make(map[u.Key]Entry),
63 full: true,
64 }
65 }
@@ -90,8 +90,8 @@ func (m *impl) Full() bool {
90 return m.full
91 }
92
93 -func (m *impl) Wantlist() []*Entry {
94 - var out []*Entry
93 +func (m *impl) Wantlist() []Entry {
94 + var out []Entry
95 for _, e := range m.wantlist {
96 out = append(out, e)
97 }
@@ -120,7 +120,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
120 e.Priority = priority
121 e.Cancel = cancel
122 } else {
123 - m.wantlist[k] = &Entry{
123 + m.wantlist[k] = Entry{
124 Entry: wantlist.Entry{
125 Key: k,
126 Priority: priority,
exchange/bitswap/wantlist/wantlist.go
+8 -8
@@ -13,7 +13,7 @@ type ThreadSafe struct {
13
14 // not threadsafe
15 type Wantlist struct {
16 - set map[u.Key]*Entry
16 + set map[u.Key]Entry
17 }
18
19 type Entry struct {
@@ -23,7 +23,7 @@ type Entry struct {
23 Priority int
24 }
25
26 -type entrySlice []*Entry
26 +type entrySlice []Entry
27
28 func (es entrySlice) Len() int { return len(es) }
29 func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
@@ -37,7 +37,7 @@ func NewThreadSafe() *ThreadSafe {
37
38 func New() *Wantlist {
39 return &Wantlist{
40 - set: make(map[u.Key]*Entry),
40 + set: make(map[u.Key]Entry),
41 }
42 }
43
@@ -62,13 +62,13 @@ func (w *ThreadSafe) Contains(k u.Key) bool {
62 return w.Wantlist.Contains(k)
63 }
64
65 -func (w *ThreadSafe) Entries() []*Entry {
65 +func (w *ThreadSafe) Entries() []Entry {
66 w.lk.RLock()
67 defer w.lk.RUnlock()
68 return w.Wantlist.Entries()
69 }
70
71 -func (w *ThreadSafe) SortedEntries() []*Entry {
71 +func (w *ThreadSafe) SortedEntries() []Entry {
72 w.lk.RLock()
73 defer w.lk.RUnlock()
74 return w.Wantlist.SortedEntries()
@@ -78,7 +78,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
78 if _, ok := w.set[k]; ok {
79 return
80 }
81 - w.set[k] = &Entry{
81 + w.set[k] = Entry{
82 Key: k,
83 Priority: priority,
84 }
@@ -93,7 +93,7 @@ func (w *Wantlist) Contains(k u.Key) bool {
93 return ok
94 }
95
96 -func (w *Wantlist) Entries() []*Entry {
96 +func (w *Wantlist) Entries() []Entry {
97 var es entrySlice
98 for _, e := range w.set {
99 es = append(es, e)
@@ -101,7 +101,7 @@ func (w *Wantlist) Entries() []*Entry {
101 return es
102 }
103
104 -func (w *Wantlist) SortedEntries() []*Entry {
104 +func (w *Wantlist) SortedEntries() []Entry {
105 var es entrySlice
106 for _, e := range w.set {
107 es = append(es, e)