erigon-pulse/common/pool/global_pool.go
Alex Sharov c5ffc971c5
[WIP] Badger v2 (#378)
* 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>
2020-03-11 11:02:37 +00:00

83 lines
1.2 KiB
Go

package pool
import (
"github.com/valyala/bytebufferpool"
)
var (
chunkSizeClasses = []uint{
8,
64,
75,
128,
192,
1 << 8,
1 << 9,
1 << 10,
2 << 10,
4 << 10,
8 << 10,
16 << 10,
}
chunkPools []*pool
)
func init() {
// init chunkPools
for _, chunkSize := range chunkSizeClasses {
chunkPools = append(chunkPools, newPool(chunkSize))
}
// preallocate some buffers
const preAlloc = 32
const lastSmallChunkIndex = 5
for i, n := range chunkSizeClasses {
if i > lastSmallChunkIndex {
break
}
for i := 0; i < preAlloc; i++ {
PutBuffer(GetBuffer(n))
}
}
}
func GetBuffer(size uint) *bytebufferpool.ByteBuffer {
var i int
for i = 0; i < len(chunkSizeClasses)-1; i++ {
if size <= chunkSizeClasses[i] {
break
}
}
pp := chunkPools[i].Get()
if capB := cap(pp.B); uint(capB) < size {
if capB == 0 {
_ = pp.WriteByte(0)
}
if capB != 0 {
pp.B = pp.B[:capB]
_, _ = pp.Write(make([]byte, size-uint(capB)))
}
}
pp.B = pp.B[:size]
return pp
}
func PutBuffer(p *bytebufferpool.ByteBuffer) {
if p == nil || cap(p.B) == 0 {
return
}
for i, n := range chunkSizeClasses {
if uint(cap(p.B)) <= n {
p.B = p.B[:0]
chunkPools[i].pool.Put(p)
break
}
}
}