mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-29 02:26:31 +00:00
e43a46e982
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package util
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.iim.gay/grufwub/fastpath"
|
|
"git.iim.gay/grufwub/go-bufpool"
|
|
"git.iim.gay/grufwub/go-bytes"
|
|
)
|
|
|
|
// pathBuilderPool is the global fastpath.Builder pool, we implement
|
|
// our own here instead of using fastpath's default one because we
|
|
// don't want to deal with fastpath's sync.Once locks on every Acquire/Release
|
|
var pathBuilderPool = sync.Pool{
|
|
New: func() interface{} {
|
|
pb := fastpath.NewBuilder(make([]byte, 0, 512))
|
|
return &pb
|
|
},
|
|
}
|
|
|
|
// AcquirePathBuilder returns a reset fastpath.Builder instance
|
|
func AcquirePathBuilder() *fastpath.Builder {
|
|
return pathBuilderPool.Get().(*fastpath.Builder)
|
|
}
|
|
|
|
// ReleasePathBuilder resets and releases provided fastpath.Builder instance to global pool
|
|
func ReleasePathBuilder(pb *fastpath.Builder) {
|
|
pb.Reset()
|
|
pathBuilderPool.Put(pb)
|
|
}
|
|
|
|
// bufferPool is the global BufferPool, we implement this here
|
|
// so we can share allocations across whatever libaries need them.
|
|
var bufferPool = bufpool.BufferPool{}
|
|
|
|
// AcquireBuffer returns a reset bytes.Buffer with at least requested capacity
|
|
func AcquireBuffer(cap int) *bytes.Buffer {
|
|
return bufferPool.Get(cap)
|
|
}
|
|
|
|
// ReleaseBuffer resets and releases provided bytes.Buffer to global BufferPool
|
|
func ReleaseBuffer(buf *bytes.Buffer) {
|
|
bufferPool.Put(buf)
|
|
}
|