@@ -214,20 +214,20 @@ func (d *Decoder) ReadUInt128() (hi, lo uint64, err error) {
214
214
return hi , lo , nil
215
215
}
216
216
217
- // ReadMap returns an iterator to read the map. The first value from the
218
- // iterator is the key. Please note that this byte slice is only valid during
219
- // the iteration. This is done to avoid an unnecessary allocation. You must
220
- // make a copy of it if you are storing it for later use. The second value is
221
- // an error indicating that the database is malformed or that the pointed
222
- // value is not a map.
223
- func (d * Decoder ) ReadMap () iter.Seq2 [[]byte , error ] {
224
- return func (yield func ([]byte , error ) bool ) {
225
- size , offset , err := d .decodeCtrlDataAndFollow (KindMap )
226
- if err != nil {
227
- yield (nil , d .wrapError (err ))
228
- return
229
- }
217
+ // ReadMap returns an iterator to read the map along with the map size. The
218
+ // size can be used to pre-allocate a map with the correct capacity for better
219
+ // performance. The first value from the iterator is the key. Please note that
220
+ // this byte slice is only valid during the iteration. This is done to avoid
221
+ // an unnecessary allocation. You must make a copy of it if you are storing it
222
+ // for later use. The second value is an error indicating that the database is
223
+ // malformed or that the pointed value is not a map.
224
+ func (d * Decoder ) ReadMap () (iter.Seq2 [[]byte , error ], uint , error ) {
225
+ size , offset , err := d .decodeCtrlDataAndFollow (KindMap )
226
+ if err != nil {
227
+ return nil , 0 , d .wrapError (err )
228
+ }
230
229
230
+ iterator := func (yield func ([]byte , error ) bool ) {
231
231
currentOffset := offset
232
232
233
233
for range size {
@@ -257,19 +257,21 @@ func (d *Decoder) ReadMap() iter.Seq2[[]byte, error] {
257
257
// Set the final offset after map iteration
258
258
d .reset (currentOffset )
259
259
}
260
+
261
+ return iterator , size , nil
260
262
}
261
263
262
- // ReadSlice returns an iterator over the values of the slice. The iterator
263
- // returns an error if the database is malformed or if the pointed value is
264
- // not a slice.
265
- func (d * Decoder ) ReadSlice () iter.Seq [error ] {
266
- return func (yield func (error ) bool ) {
267
- size , offset , err := d .decodeCtrlDataAndFollow (KindSlice )
268
- if err != nil {
269
- yield (d .wrapError (err ))
270
- return
271
- }
264
+ // ReadSlice returns an iterator over the values of the slice along with the
265
+ // slice size. The size can be used to pre-allocate a slice with the correct
266
+ // capacity for better performance. The iterator returns an error if the
267
+ // database is malformed or if the pointed value is not a slice.
268
+ func (d * Decoder ) ReadSlice () (iter.Seq [error ], uint , error ) {
269
+ size , offset , err := d .decodeCtrlDataAndFollow (KindSlice )
270
+ if err != nil {
271
+ return nil , 0 , d .wrapError (err )
272
+ }
272
273
274
+ iterator := func (yield func (error ) bool ) {
273
275
currentOffset := offset
274
276
275
277
for i := range size {
@@ -301,6 +303,8 @@ func (d *Decoder) ReadSlice() iter.Seq[error] {
301
303
// Set final offset after slice iteration
302
304
d .reset (currentOffset )
303
305
}
306
+
307
+ return iterator , size , nil
304
308
}
305
309
306
310
// SkipValue skips over the current value without decoding it.
0 commit comments