mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
Improve Get/Put performance with optional mempooling (#36)
* avoid unnecessary use of encoder/decoder to decrease memory allocations * add an optional configurable mempool to avoid extra allocs * add doc.go with examples
This commit is contained in:
committed by
James Mills
parent
6ceeccfd64
commit
a407905ae2
@@ -1,26 +1,31 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/oxtoacart/bpool"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/exp/mmap"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
pb "github.com/prologic/bitcask/internal/proto"
|
||||
"github.com/prologic/bitcask/internal/streampb"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultDatafileFilename = "%09d.data"
|
||||
prefixSize = 8
|
||||
)
|
||||
|
||||
var (
|
||||
ErrReadonly = errors.New("error: read only datafile")
|
||||
ErrReadError = errors.New("error: read error")
|
||||
|
||||
memPool *bpool.BufferPool
|
||||
mxMemPool sync.RWMutex
|
||||
)
|
||||
|
||||
type Datafile struct {
|
||||
@@ -136,7 +141,17 @@ func (df *Datafile) Read() (e pb.Entry, n int64, err error) {
|
||||
func (df *Datafile) ReadAt(index, size int64) (e pb.Entry, err error) {
|
||||
var n int
|
||||
|
||||
b := make([]byte, size)
|
||||
var b []byte
|
||||
if memPool == nil {
|
||||
b = make([]byte, size)
|
||||
} else {
|
||||
poolSlice := memPool.Get()
|
||||
if poolSlice.Cap() < int(size) {
|
||||
poolSlice.Grow(int(size) - poolSlice.Cap())
|
||||
}
|
||||
defer memPool.Put(poolSlice)
|
||||
b = poolSlice.Bytes()[:size]
|
||||
}
|
||||
|
||||
if df.w == nil {
|
||||
n, err = df.ra.ReadAt(b, index)
|
||||
@@ -151,9 +166,10 @@ func (df *Datafile) ReadAt(index, size int64) (e pb.Entry, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(b)
|
||||
dec := streampb.NewDecoder(buf)
|
||||
_, err = dec.Decode(&e)
|
||||
err = proto.Unmarshal(b[prefixSize:], &e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -175,3 +191,15 @@ func (df *Datafile) Write(e pb.Entry) (int64, int64, error) {
|
||||
|
||||
return e.Offset, n, nil
|
||||
}
|
||||
|
||||
// ConfigureMemPool configurate the mempool accordingly
|
||||
func ConfigureMemPool(maxConcurrency *int) {
|
||||
mxMemPool.Lock()
|
||||
defer mxMemPool.Unlock()
|
||||
if maxConcurrency == nil {
|
||||
memPool = nil
|
||||
} else {
|
||||
memPool = bpool.NewBufferPool(*maxConcurrency)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user