Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/aggregation/fuzzy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build experimental
//go:build experimental

package aggregation

Expand Down
2 changes: 1 addition & 1 deletion pkg/fastregex/fallback.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux !cgo !pcre2
//go:build !(linux && cgo && pcre2)

package fastregex

Expand Down
2 changes: 1 addition & 1 deletion pkg/fastregex/pcre2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux,cgo,pcre2
//go:build linux && cgo && pcre2

package fastregex

Expand Down
2 changes: 1 addition & 1 deletion pkg/fastregex/pcre2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux,cgo,pcre2
//go:build linux && cgo && pcre2

package fastregex

Expand Down
10 changes: 6 additions & 4 deletions pkg/slicepool/objpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import "sync"
// Technically can accept objects it didn't create, though that's not good as will pollute the size
// operates in non-blocking mode (it will create a new object if it doesn't have one readily available)
type ObjectPool[T any] struct {
pool []*T
m sync.Mutex
pool []*T
newer func() *T
m sync.Mutex
}

// Create an object pool of an initial size. May grow later
Expand All @@ -18,7 +19,8 @@ func NewObjectPool[T any](size int) *ObjectPool[T] {
// Create an object pool with a custom object initializer
func NewObjectPoolEx[T any](size int, newer func() *T) *ObjectPool[T] {
ret := &ObjectPool[T]{
pool: make([]*T, size),
pool: make([]*T, size),
newer: newer,
}
for i := 0; i < size; i++ {
ret.pool[i] = newer()
Expand All @@ -31,7 +33,7 @@ func (s *ObjectPool[T]) Get() (ret *T) {
defer s.m.Unlock()

if len(s.pool) == 0 {
return new(T)
return s.newer()
}

end := len(s.pool) - 1
Expand Down
11 changes: 11 additions & 0 deletions pkg/slicepool/objpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func TestSimpleObjPool(t *testing.T) {
assert.Len(t, op.pool, 2)
}

func TestSimpleObjPoolCustomNew(t *testing.T) {
type testObj struct{ item int }

op := NewObjectPoolEx[testObj](1, func() *testObj {
return &testObj{5}
})

assert.Equal(t, 5, op.Get().item)
assert.Equal(t, 5, op.Get().item)
}

func TestZeroAllocs(t *testing.T) {
if testing.Short() {
t.SkipNow()
Expand Down
Loading