Compare commits

...

5 Commits

Author SHA1 Message Date
Steven Allen
73e5246a65 gx publish 0.7.25 2018-08-15 08:24:56 -07:00
Steven Allen
83a7594d41 Merge pull request #67 from ipfs/feat/streaming-set
add a streaming CID set
2018-08-11 01:06:53 +00:00
Łukasz Magiera
3655c1cdd4 add a streaming CID set
used in https://github.com/ipfs/go-ipfs/pull/4804
2018-08-10 17:32:43 -07:00
Steven Allen
1543f4a136 Merge pull request #44 from ipfs/feat/bench
add String benchmark
2018-08-10 23:55:30 +00:00
Steven Allen
d6e0b4e5a7 add String benchmark
We call String all over the place so we should make sure it remains fast.
2018-08-10 16:23:25 -07:00
4 changed files with 51 additions and 2 deletions

View File

@@ -1 +1 @@
0.7.24: Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y
0.7.25: QmYjnkEL7i731PirfVH1sis89evN7jt4otSHw5D2xXXwUV

View File

@@ -414,3 +414,17 @@ func TestJsonRoundTrip(t *testing.T) {
t.Fatal("cids not equal for Cid")
}
}
func BenchmarkStringV1(b *testing.B) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
cid := NewCidV1(Raw, hash)
b.ResetTimer()
count := 0
for i := 0; i < b.N; i++ {
count += len(cid.String())
}
if count != 49*b.N {
b.FailNow()
}
}

View File

@@ -25,6 +25,6 @@
"license": "MIT",
"name": "go-cid",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.7.24"
"version": "0.7.25"
}

35
set.go
View File

@@ -1,5 +1,9 @@
package cid
import (
"context"
)
// Set is a implementation of a set of Cids, that is, a structure
// to which holds a single copy of every Cids that is added to it.
type Set struct {
@@ -65,3 +69,34 @@ func (s *Set) ForEach(f func(c *Cid) error) error {
}
return nil
}
// StreamingSet is an extension of Set which allows to implement back-pressure
// for the Visit function
type StreamingSet struct {
Set *Set
New chan *Cid
}
// NewStreamingSet initializes and returns new Set.
func NewStreamingSet() *StreamingSet {
return &StreamingSet{
Set: NewSet(),
New: make(chan *Cid),
}
}
// Visitor creates new visitor which adds a Cids to the set and emits them to
// the set.New channel
func (s *StreamingSet) Visitor(ctx context.Context) func(c *Cid) bool {
return func(c *Cid) bool {
if s.Set.Visit(c) {
select {
case s.New <- c:
case <-ctx.Done():
}
return true
}
return false
}
}