Skip to content

Commit c7d52d6

Browse files
authoredNov 29, 2016
Merge pull request ipfs#38 from libp2p/kevina/faster-query
Improve GetProviders Performance by around 2x
2 parents 2e4e4ba + ce68333 commit c7d52d6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed
 

‎providers/providers.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ func loadProvSet(dstore ds.Datastore, k *cid.Cid) (*providerSet, error) {
118118
}
119119

120120
out := newProviderSet()
121-
for e := range res.Next() {
121+
for {
122+
e, ok := res.NextSync()
123+
if !ok {
124+
break
125+
}
122126
if e.Error != nil {
123127
log.Error("got an error: ", e.Error)
124128
continue

‎providers/providers_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010

1111
cid "github.com/ipfs/go-cid"
1212
ds "github.com/ipfs/go-datastore"
13-
//lds "github.com/ipfs/go-ds-leveldb"
1413
u "github.com/ipfs/go-ipfs-util"
1514
peer "github.com/libp2p/go-libp2p-peer"
15+
//
16+
// used by TestLargeProvidersSet: do not remove
17+
// lds "github.com/ipfs/go-ds-leveldb"
1618
)
1719

1820
func TestProviderManager(t *testing.T) {
@@ -200,12 +202,16 @@ func TestLargeProvidersSet(t *testing.T) {
200202
}
201203
}
202204
203-
for _, c := range cids {
204-
_ = p.GetProviders(ctx, c)
205+
for i := 0; i < 5; i++ {
206+
start := time.Now()
207+
for _, c := range cids {
208+
_ = p.GetProviders(ctx, c)
209+
}
210+
elapsed := time.Since(start)
211+
fmt.Printf("query %f ms\n", elapsed.Seconds()*1000)
205212
}
206-
207213
}
208-
//*/
214+
*/
209215

210216
func TestUponCacheMissProvidersAreReadFromDatastore(t *testing.T) {
211217
old := lruCacheSize

0 commit comments

Comments
 (0)