mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-28 14:47:16 +00:00
c5ffc971c5
* badger v2 investigation * buf pool - use native New method and avoid double checks * db.Open prototype * db.Tx/Bucket/Cursor prototypes * Chained config * Item concept added * save changes to test on master * make hack resumable * Design document v0 * Cursor concept * less brackets syntax of cursor builder * benchmarks * cleanup fs * test for context cancelations * test for context cancelations * test for cursor.Prefix option * add ForEachKey method * add ForEachKey method * add naming explanation * experiment of non-pointers cursor/bucket * .Bucket() and .Cursor() doesn't returns error * .Bucket() and .Cursor() doesn't returns error * .Bucket() and .Cursor() doesn't returns error * remove CursorOpts concept * more test-cases * simplify open api * Tx, Bucket, Cursor - now are interfaces * Tx, Bucket, Cursor - now are interfaces * switch to interfaces * rebase master Co-authored-by: alex.sharov <alex.sharov@lazada.com>
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package pool
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/valyala/bytebufferpool"
|
|
)
|
|
|
|
// pool represents byte buffer pool.
|
|
//
|
|
// Distinct pools may be used for distinct types of byte buffers.
|
|
// Properly determined byte buffer types with their own pools may help reducing
|
|
// memory waste.
|
|
type pool struct {
|
|
defaultSize uint64
|
|
maxSize uint64
|
|
|
|
pool sync.Pool
|
|
}
|
|
|
|
func newPool(defaultSize uint) *pool {
|
|
return &pool{
|
|
defaultSize: uint64(defaultSize),
|
|
maxSize: uint64(defaultSize),
|
|
pool: sync.Pool{
|
|
New: func() interface{} {
|
|
return &bytebufferpool.ByteBuffer{
|
|
B: make([]byte, 0, defaultSize),
|
|
}
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Get returns new byte buffer with zero length.
|
|
//
|
|
// The byte buffer may be returned to the pool via Put after the use
|
|
// in order to minimize GC overhead.
|
|
func (p *pool) Get() *bytebufferpool.ByteBuffer {
|
|
return p.pool.Get().(*bytebufferpool.ByteBuffer)
|
|
}
|
|
|
|
// Put releases byte buffer obtained via Get to the pool.
|
|
//
|
|
// The buffer mustn't be accessed after returning to the pool.
|
|
func (p *pool) Put(b *bytebufferpool.ByteBuffer) {
|
|
if b == nil || cap(b.B) == 0 {
|
|
return
|
|
}
|
|
|
|
maxSize := int(atomic.LoadUint64(&p.maxSize))
|
|
if maxSize == 0 || cap(b.B) <= maxSize {
|
|
b.B = b.B[:0]
|
|
p.pool.Put(b)
|
|
}
|
|
}
|