Skip to content

Commit

Permalink
Release entryBufferPool once (#5324) (#5335)
Browse files Browse the repository at this point in the history
* release entryBufferPool once

* reverseEntryIterator.Next is safe to call past expiry

Co-authored-by: Owen Diehl <ow.diehl@gmail.com>
  • Loading branch information
Danny Kopping and owen-d authored Feb 7, 2022
1 parent fd930e5 commit 3255745
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pkg/iter/entry_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,8 @@ func (i *reverseEntryIterator) load() {

func (i *reverseEntryIterator) Next() bool {
i.load()
if len(i.buf.entries) == 0 {
entryBufferPool.Put(i.buf)
i.buf.entries = nil
if i.buf == nil || len(i.buf.entries) == 0 {
i.release()
return false
}
i.cur, i.buf.entries = i.buf.entries[len(i.buf.entries)-1], i.buf.entries[:len(i.buf.entries)-1]
Expand All @@ -736,12 +735,21 @@ func (i *reverseEntryIterator) Labels() string {

func (i *reverseEntryIterator) Error() error { return nil }

func (i *reverseEntryIterator) Close() error {
func (i *reverseEntryIterator) release() {
if i.buf == nil {
return
}

if i.buf.entries != nil {
// preserve the underlying slice before releasing to pool
i.buf.entries = i.buf.entries[:0]
entryBufferPool.Put(i.buf)
i.buf.entries = nil
}
entryBufferPool.Put(i.buf)
i.buf = nil
}

func (i *reverseEntryIterator) Close() error {
i.release()
if !i.loaded {
return i.iter.Close()
}
Expand Down

0 comments on commit 3255745

Please sign in to comment.