@@ -214,20 +214,20 @@ func (d *Decoder) ReadUInt128() (hi, lo uint64, err error) {
214214 return hi , lo , nil
215215}
216216
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+ }
230229
230+ iterator := func (yield func ([]byte , error ) bool ) {
231231 currentOffset := offset
232232
233233 for range size {
@@ -257,19 +257,21 @@ func (d *Decoder) ReadMap() iter.Seq2[[]byte, error] {
257257 // Set the final offset after map iteration
258258 d .reset (currentOffset )
259259 }
260+
261+ return iterator , size , nil
260262}
261263
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+ }
272273
274+ iterator := func (yield func (error ) bool ) {
273275 currentOffset := offset
274276
275277 for i := range size {
@@ -301,6 +303,8 @@ func (d *Decoder) ReadSlice() iter.Seq[error] {
301303 // Set final offset after slice iteration
302304 d .reset (currentOffset )
303305 }
306+
307+ return iterator , size , nil
304308}
305309
306310// SkipValue skips over the current value without decoding it.
0 commit comments