Skip to content

Commit 8854a6f

Browse files
authored
Remove unnecessary CID copying in SessionInterestManager (#761)
Also reduce lock scope in some places by moving non-critical work outside of lock.
1 parent 8d6ac57 commit 8854a6f

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

bitswap/client/internal/sessioninterestmanager/sessioninterestmanager.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (sim *SessionInterestManager) RemoveSession(ses uint64) []cid.Cid {
5353
defer sim.lk.Unlock()
5454

5555
// The keys that no session is interested in
56-
deletedKs := make([]cid.Cid, 0)
56+
var deletedKs []cid.Cid
5757

5858
// For each known key
5959
for c := range sim.wants {
@@ -119,18 +119,19 @@ func (sim *SessionInterestManager) RemoveSessionInterested(ses uint64, ks []cid.
119119
// The session calls FilterSessionInterested() to filter the sets of keys for
120120
// those that the session is interested in
121121
func (sim *SessionInterestManager) FilterSessionInterested(ses uint64, ksets ...[]cid.Cid) [][]cid.Cid {
122+
kres := make([][]cid.Cid, len(ksets))
123+
122124
sim.lk.RLock()
123125
defer sim.lk.RUnlock()
124126

125127
// For each set of keys
126-
kres := make([][]cid.Cid, len(ksets))
127128
for i, ks := range ksets {
128129
// The set of keys that at least one session is interested in
129-
has := make([]cid.Cid, 0, len(ks))
130+
var has []cid.Cid
130131

131132
// For each key in the list
132133
for _, c := range ks {
133-
// If there is a session that's interested, add the key to the set
134+
// If the session is interested, add the key to the set
134135
if _, ok := sim.wants[c][ses]; ok {
135136
has = append(has, c)
136137
}
@@ -144,7 +145,6 @@ func (sim *SessionInterestManager) FilterSessionInterested(ses uint64, ksets ...
144145
// unwanted blocks
145146
func (sim *SessionInterestManager) SplitWantedUnwanted(blks []blocks.Block) ([]blocks.Block, []blocks.Block) {
146147
sim.lk.RLock()
147-
defer sim.lk.RUnlock()
148148

149149
// Get the wanted block keys as a set
150150
wantedKs := cid.NewSet()
@@ -160,6 +160,8 @@ func (sim *SessionInterestManager) SplitWantedUnwanted(blks []blocks.Block) ([]b
160160
}
161161
}
162162

163+
sim.lk.RUnlock()
164+
163165
// Separate the blocks into wanted and unwanted
164166
wantedBlks := make([]blocks.Block, 0, len(blks))
165167
notWantedBlks := make([]blocks.Block, 0)
@@ -175,23 +177,22 @@ func (sim *SessionInterestManager) SplitWantedUnwanted(blks []blocks.Block) ([]b
175177

176178
// When the SessionManager receives a message it calls InterestedSessions() to
177179
// find out which sessions are interested in the message.
178-
func (sim *SessionInterestManager) InterestedSessions(blks []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) []uint64 {
179-
sim.lk.RLock()
180-
defer sim.lk.RUnlock()
180+
func (sim *SessionInterestManager) InterestedSessions(keySets ...[]cid.Cid) []uint64 {
181+
sesSet := make(map[uint64]struct{})
181182

182-
ks := make([]cid.Cid, 0, len(blks)+len(haves)+len(dontHaves))
183-
ks = append(ks, blks...)
184-
ks = append(ks, haves...)
185-
ks = append(ks, dontHaves...)
183+
sim.lk.RLock()
186184

187185
// Create a set of sessions that are interested in the keys
188-
sesSet := make(map[uint64]struct{})
189-
for _, c := range ks {
190-
for s := range sim.wants[c] {
191-
sesSet[s] = struct{}{}
186+
for _, keySet := range keySets {
187+
for _, c := range keySet {
188+
for s := range sim.wants[c] {
189+
sesSet[s] = struct{}{}
190+
}
192191
}
193192
}
194193

194+
sim.lk.RUnlock()
195+
195196
// Convert the set into a list
196197
ses := make([]uint64, 0, len(sesSet))
197198
for s := range sesSet {

0 commit comments

Comments
 (0)