diff options
author | 2021-09-12 10:10:24 +0100 | |
---|---|---|
committer | 2021-09-12 10:10:24 +0100 | |
commit | f6492d12d948507021bbe934de94e87e20464c01 (patch) | |
tree | 6705d6ef6f3c4d70f3b3ebc77c2960d8e508cf37 /vendor/git.iim.gay/grufwub/go-bytes/pool.go | |
parent | Merge pull request #213 from superseriousbusiness/alpine+node_upstep (diff) | |
parent | fix keys used to access storage items (diff) | |
download | gotosocial-f6492d12d948507021bbe934de94e87e20464c01.tar.xz |
Merge pull request #214 from NyaaaWhatsUpDoc/improvement/update-storage-library
add git.iim.gay/grufwub/go-store for storage backend, replacing blob.Storage
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-bytes/pool.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-bytes/pool.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-bytes/pool.go b/vendor/git.iim.gay/grufwub/go-bytes/pool.go new file mode 100644 index 000000000..edd6859c1 --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-bytes/pool.go @@ -0,0 +1,39 @@ +package bytes + +import ( + "bytes" + "sync" +) + +type SizedBufferPool struct { + pool sync.Pool + len int + cap int +} + +func (p *SizedBufferPool) Init(len, cap int) { + p.pool.New = func() interface{} { + buf := NewBuffer(make([]byte, len, cap)) + return &buf + } + p.len = len + p.cap = cap +} + +func (p *SizedBufferPool) Acquire() *bytes.Buffer { + return p.pool.Get().(*bytes.Buffer) +} + +func (p *SizedBufferPool) Release(buf *bytes.Buffer) { + // If not enough cap, ignore + if buf.Cap() < p.cap { + return + } + + // Set length to expected + buf.Reset() + buf.Grow(p.len) + + // Place in pool + p.pool.Put(buf) +} |