@cryptotaxi247 / kubo / commits / ec9ce83d8

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 <why@ipfs.io>

Jeromy committed Sep 29, 2016 at 13:41 UTC ec9ce83d88877549641c6ac9698fa150c0e1f224
1 file changed +15 -12
pin/set.go
+15 -12
@@ -132,12 +132,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
132 sort.Stable(s)
133 }
134
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)
136 for {
137 // This loop essentially enumerates every single item in the set
138 // 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
147 // and losing pins. The fix (a few lines down from this comment), is to
148 // map the hash value down to the 8 bit keyspace here while creating the
149 // buckets. This way, we avoid any overlapping later on.
155 - k, data, ok := iter()
150 + k, _, ok := iter()
151 if !ok {
152 break
153 }
154 h := hash(seed, k) % defaultFanout
160 - hashed[h] = append(hashed[h], item{k, data})
155 + hashed[h] = append(hashed[h], k)
156 }
157 +
158 for h, items := range hashed {
159 + if len(items) == 0 {
160 + // recursion base case
161 + continue
162 + }
163 +
164 childIter := func() (c *cid.Cid, data []byte, ok bool) {
165 if len(items) == 0 {
166 return nil, nil, false
167 }
168 first := items[0]
169 items = items[1:]
169 - return first.c, first.data, true
170 + return first, nil, true
171 }
172 +
173 child, err := storeItems(ctx, dag, uint64(len(items)), childIter, internalKeys)
174 if err != nil {
175 return nil, err
176 }
177 +
178 size, err := child.Size()
179 if err != nil {
180 return nil, err
181 }
182 +
183 childKey, err := dag.Add(child)
184 if err != nil {
185 return nil, err
186 }
187 +
188 internalKeys(childKey)
184 - l := &merkledag.Link{
185 - Name: "",
189 + n.Links[int(h)] = &merkledag.Link{
190 Hash: childKey.Hash(),
191 Size: size,
192 }
189 - n.Links[int(h%defaultFanout)] = l
193 }
194 return n, nil
195 }