mirror of
https://github.com/tuneinsight/lattigo.git
synced 2025-09-13 03:27:14 +00:00
freelist + benchmarking
This commit is contained in:
@@ -6,6 +6,7 @@ type BufferPool[T any] interface {
|
||||
Get() T
|
||||
Put(T)
|
||||
}
|
||||
|
||||
type SyncPool[T any] struct {
|
||||
pool *sync.Pool
|
||||
}
|
||||
@@ -26,3 +27,39 @@ func (spool *SyncPool[T]) Get() T {
|
||||
func (spool *SyncPool[T]) Put(buff T) {
|
||||
spool.pool.Put(buff)
|
||||
}
|
||||
|
||||
type FreeList[T any] struct {
|
||||
pool chan T
|
||||
newObject func() T
|
||||
capacity int
|
||||
}
|
||||
|
||||
func NewFreeList[T any](capacity int, f func() T) *FreeList[T] {
|
||||
pool := make(chan T, capacity)
|
||||
for i := 0; i < capacity; i++ {
|
||||
pool <- f()
|
||||
}
|
||||
return &FreeList[T]{
|
||||
pool: pool,
|
||||
newObject: f,
|
||||
capacity: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
func (fl *FreeList[T]) Get() T {
|
||||
var obj T
|
||||
|
||||
select {
|
||||
case obj = <-fl.pool:
|
||||
default:
|
||||
obj = fl.newObject()
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
func (fl *FreeList[T]) Put(obj T) {
|
||||
select {
|
||||
case fl.pool <- obj:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user