提交 ec9ce83d 作者: Jeromy

pinset: clean up storeItems logic a bit

Switched from using a map to an array since the bounds are
small and fixed. This should save us some significant time and on
accesses

License: MIT
Signed-off-by: 's avatarJeromy <why@ipfs.io>
上级 c7e3d5d2
...@@ -132,12 +132,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint ...@@ -132,12 +132,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
sort.Stable(s) sort.Stable(s)
} }
// wasteful but simple hashed := make([][]*cid.Cid, defaultFanout)
type item struct {
c *cid.Cid
data []byte
}
hashed := make(map[uint32][]item)
for { for {
// This loop essentially enumerates every single item in the set // This loop essentially enumerates every single item in the set
// and maps them all into a set of buckets. Each bucket will be recursively // and maps them all into a set of buckets. Each bucket will be recursively
...@@ -152,41 +147,49 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint ...@@ -152,41 +147,49 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
// and losing pins. The fix (a few lines down from this comment), is to // and losing pins. The fix (a few lines down from this comment), is to
// map the hash value down to the 8 bit keyspace here while creating the // map the hash value down to the 8 bit keyspace here while creating the
// buckets. This way, we avoid any overlapping later on. // buckets. This way, we avoid any overlapping later on.
k, data, ok := iter() k, _, ok := iter()
if !ok { if !ok {
break break
} }
h := hash(seed, k) % defaultFanout h := hash(seed, k) % defaultFanout
hashed[h] = append(hashed[h], item{k, data}) hashed[h] = append(hashed[h], k)
} }
for h, items := range hashed { for h, items := range hashed {
if len(items) == 0 {
// recursion base case
continue
}
childIter := func() (c *cid.Cid, data []byte, ok bool) { childIter := func() (c *cid.Cid, data []byte, ok bool) {
if len(items) == 0 { if len(items) == 0 {
return nil, nil, false return nil, nil, false
} }
first := items[0] first := items[0]
items = items[1:] items = items[1:]
return first.c, first.data, true return first, nil, true
} }
child, err := storeItems(ctx, dag, uint64(len(items)), childIter, internalKeys) child, err := storeItems(ctx, dag, uint64(len(items)), childIter, internalKeys)
if err != nil { if err != nil {
return nil, err return nil, err
} }
size, err := child.Size() size, err := child.Size()
if err != nil { if err != nil {
return nil, err return nil, err
} }
childKey, err := dag.Add(child) childKey, err := dag.Add(child)
if err != nil { if err != nil {
return nil, err return nil, err
} }
internalKeys(childKey) internalKeys(childKey)
l := &merkledag.Link{ n.Links[int(h)] = &merkledag.Link{
Name: "",
Hash: childKey.Hash(), Hash: childKey.Hash(),
Size: size, Size: size,
} }
n.Links[int(h%defaultFanout)] = l
} }
return n, nil return n, nil
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论