summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-pools/fastpath.go
blob: eb76f03e4cfb6cc41532bee6ea3f24de13b66b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package pools

import (
	"sync"

	"codeberg.org/gruf/go-fastpath"
)

// PathBuilderPool is a pooled allocator for fastpath.Builder objects
type PathBuilderPool interface {
	// Get fetches a fastpath.Builder from pool
	Get() *fastpath.Builder

	// Put places supplied fastpath.Builder back in pool
	Put(*fastpath.Builder)
}

// NewPathBuilderPool returns a newly instantiated fastpath.Builder pool
func NewPathBuilderPool(size int) PathBuilderPool {
	return &pathBuilderPool{
		pool: sync.Pool{
			New: func() interface{} {
				return &fastpath.Builder{B: make([]byte, 0, size)}
			},
		},
		size: size,
	}
}

// pathBuilderPool is our implementation of PathBuilderPool
type pathBuilderPool struct {
	pool sync.Pool
	size int
}

func (p *pathBuilderPool) Get() *fastpath.Builder {
	return p.pool.Get().(*fastpath.Builder)
}

func (p *pathBuilderPool) Put(pb *fastpath.Builder) {
	if pb.Cap() < p.size {
		return
	}
	pb.Reset()
	p.pool.Put(pb)
}