gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

fastpath.go (944B)


      1 package pools
      2 
      3 import (
      4 	"sync"
      5 
      6 	"codeberg.org/gruf/go-fastpath"
      7 )
      8 
      9 // PathBuilderPool is a pooled allocator for fastpath.Builder objects
     10 type PathBuilderPool interface {
     11 	// Get fetches a fastpath.Builder from pool
     12 	Get() *fastpath.Builder
     13 
     14 	// Put places supplied fastpath.Builder back in pool
     15 	Put(*fastpath.Builder)
     16 }
     17 
     18 // NewPathBuilderPool returns a newly instantiated fastpath.Builder pool
     19 func NewPathBuilderPool(size int) PathBuilderPool {
     20 	return &pathBuilderPool{
     21 		pool: sync.Pool{
     22 			New: func() interface{} {
     23 				return &fastpath.Builder{B: make([]byte, 0, size)}
     24 			},
     25 		},
     26 		size: size,
     27 	}
     28 }
     29 
     30 // pathBuilderPool is our implementation of PathBuilderPool
     31 type pathBuilderPool struct {
     32 	pool sync.Pool
     33 	size int
     34 }
     35 
     36 func (p *pathBuilderPool) Get() *fastpath.Builder {
     37 	return p.pool.Get().(*fastpath.Builder)
     38 }
     39 
     40 func (p *pathBuilderPool) Put(pb *fastpath.Builder) {
     41 	if pb.Cap() < p.size {
     42 		return
     43 	}
     44 	pb.Reset()
     45 	p.pool.Put(pb)
     46 }