Skip to content

Commit

Permalink
add: add bufferpool for dict
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jun 24, 2024
1 parent 269cf5c commit 6c99611
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
Binary file removed internal/dict/bucket.png
Binary file not shown.
8 changes: 5 additions & 3 deletions internal/dict/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const (
)

var (
bufferpool = pkg.NewBufferPool()
dictAllocator = pkg.NewAllocator[string, Idx]()
)

// Dict is the hashmap for Rotom.
type Dict struct {
mask uint32
Expand All @@ -37,7 +38,7 @@ func New(options Options) *Dict {
dict.shards[i] = &shard{
options: &options,
index: swiss.New(options.IndexSize, swiss.WithAllocator(dictAllocator)),
data: make([]byte, 0, options.BufferSize),
data: bufferpool.Get(options.BufferSize)[:0],
}
}
return dict
Expand Down Expand Up @@ -203,7 +204,7 @@ func (s *shard) evictExpired() {

// migrate transfers valid key-value pairs to a new container to save memory.
func (s *shard) migrate() {
newData := make([]byte, 0, len(s.data))
newData := bufferpool.Get(len(s.data))[:0]
nanosec := time.Now().UnixNano()
newIndex := swiss.New(s.index.Len(), swiss.WithAllocator(dictAllocator))

Expand All @@ -218,6 +219,7 @@ func (s *shard) migrate() {
})

s.index.Close()
bufferpool.Put(s.data)
s.index = newIndex
s.data = newData
s.unused = 0
Expand Down
Binary file removed internal/dict/key.png
Binary file not shown.
12 changes: 6 additions & 6 deletions internal/pkg/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package pkg

import (
"sync"
"sync/atomic"

"github.com/cockroachdb/swiss"
)

// Allocator is a group pool for swissmap.
type Allocator[K comparable, V any] struct {
pool *sync.Pool
miss, hit atomic.Uint64
miss, hit uint64
}

func NewAllocator[K comparable, V any]() *Allocator[K, V] {
Expand All @@ -25,11 +25,11 @@ func (p *Allocator[K, V]) Alloc(want int) []swiss.Group[K, V] {

if cap(*buf) < want {
*buf = make([]swiss.Group[K, V], want)
p.miss.Add(1)
p.miss++

} else {
*buf = (*buf)[:want]
p.hit.Add(1)
p.hit++
}

return *buf
Expand All @@ -40,9 +40,9 @@ func (p *Allocator[K, V]) Free(b []swiss.Group[K, V]) {
}

func (p *Allocator[K, V]) Miss() uint64 {
return p.miss.Load()
return p.miss
}

func (p *Allocator[K, V]) Hit() uint64 {
return p.hit.Load()
return p.hit
}
7 changes: 3 additions & 4 deletions internal/pkg/bufferpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package pkg

import (
"sync"
"sync/atomic"
)

// BufferPool is a bytes buffer pool.
type BufferPool struct {
pool *sync.Pool
miss, hit atomic.Uint64
miss, hit uint64
}

// Get returns buffer with length of want.
Expand All @@ -17,11 +16,11 @@ func (p *BufferPool) Get(want int) []byte {

if cap(*buf) < want {
*buf = make([]byte, want)
p.miss.Add(1)
p.miss++

} else {
*buf = (*buf)[:want]
p.hit.Add(1)
p.hit++
}

return *buf
Expand Down

0 comments on commit 6c99611

Please sign in to comment.