Skip to content

Commit ec9ce83

Browse files
committed
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: Jeromy <[email protected]>
1 parent c7e3d5d commit ec9ce83

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pin/set.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
132132
sort.Stable(s)
133133
}
134134

135-
// wasteful but simple
136-
type item struct {
137-
c *cid.Cid
138-
data []byte
139-
}
140-
hashed := make(map[uint32][]item)
135+
hashed := make([][]*cid.Cid, defaultFanout)
141136
for {
142137
// This loop essentially enumerates every single item in the set
143138
// 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
152147
// and losing pins. The fix (a few lines down from this comment), is to
153148
// map the hash value down to the 8 bit keyspace here while creating the
154149
// buckets. This way, we avoid any overlapping later on.
155-
k, data, ok := iter()
150+
k, _, ok := iter()
156151
if !ok {
157152
break
158153
}
159154
h := hash(seed, k) % defaultFanout
160-
hashed[h] = append(hashed[h], item{k, data})
155+
hashed[h] = append(hashed[h], k)
161156
}
157+
162158
for h, items := range hashed {
159+
if len(items) == 0 {
160+
// recursion base case
161+
continue
162+
}
163+
163164
childIter := func() (c *cid.Cid, data []byte, ok bool) {
164165
if len(items) == 0 {
165166
return nil, nil, false
166167
}
167168
first := items[0]
168169
items = items[1:]
169-
return first.c, first.data, true
170+
return first, nil, true
170171
}
172+
171173
child, err := storeItems(ctx, dag, uint64(len(items)), childIter, internalKeys)
172174
if err != nil {
173175
return nil, err
174176
}
177+
175178
size, err := child.Size()
176179
if err != nil {
177180
return nil, err
178181
}
182+
179183
childKey, err := dag.Add(child)
180184
if err != nil {
181185
return nil, err
182186
}
187+
183188
internalKeys(childKey)
184-
l := &merkledag.Link{
185-
Name: "",
189+
n.Links[int(h)] = &merkledag.Link{
186190
Hash: childKey.Hash(),
187191
Size: size,
188192
}
189-
n.Links[int(h%defaultFanout)] = l
190193
}
191194
return n, nil
192195
}

0 commit comments

Comments
 (0)