2018-03-30 22:43:18 +02:00
|
|
|
package archiver
|
|
|
|
|
|
2025-10-13 22:08:58 +02:00
|
|
|
import "sync"
|
|
|
|
|
|
2024-08-27 11:26:52 +02:00
|
|
|
// buffer is a reusable buffer. After the buffer has been used, Release should
|
2018-03-30 22:43:18 +02:00
|
|
|
// be called so the underlying slice is put back into the pool.
|
2024-08-27 11:26:52 +02:00
|
|
|
type buffer struct {
|
2018-03-30 22:43:18 +02:00
|
|
|
Data []byte
|
2024-08-27 11:26:52 +02:00
|
|
|
pool *bufferPool
|
2018-03-30 22:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Release puts the buffer back into the pool it came from.
|
2024-08-27 11:26:52 +02:00
|
|
|
func (b *buffer) Release() {
|
2022-05-29 17:07:37 +02:00
|
|
|
pool := b.pool
|
|
|
|
|
if pool == nil || cap(b.Data) > pool.defaultSize {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 22:08:58 +02:00
|
|
|
pool.pool.Put(b)
|
2018-03-30 22:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 11:26:52 +02:00
|
|
|
// bufferPool implements a limited set of reusable buffers.
|
|
|
|
|
type bufferPool struct {
|
2025-10-13 22:08:58 +02:00
|
|
|
pool sync.Pool
|
2018-03-30 22:43:18 +02:00
|
|
|
defaultSize int
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 11:26:52 +02:00
|
|
|
// newBufferPool initializes a new buffer pool. The pool stores at most max
|
2022-05-29 17:07:37 +02:00
|
|
|
// items. New buffers are created with defaultSize. Buffers that have grown
|
|
|
|
|
// larger are not put back.
|
2025-10-13 22:08:58 +02:00
|
|
|
func newBufferPool(defaultSize int) *bufferPool {
|
2024-08-27 11:26:52 +02:00
|
|
|
b := &bufferPool{
|
2018-03-30 22:43:18 +02:00
|
|
|
defaultSize: defaultSize,
|
|
|
|
|
}
|
2025-10-13 22:08:58 +02:00
|
|
|
b.pool = sync.Pool{New: func() any {
|
|
|
|
|
return &buffer{
|
|
|
|
|
Data: make([]byte, defaultSize),
|
|
|
|
|
pool: b,
|
|
|
|
|
}
|
|
|
|
|
}}
|
2018-03-30 22:43:18 +02:00
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get returns a new buffer, either from the pool or newly allocated.
|
2024-08-27 11:26:52 +02:00
|
|
|
func (pool *bufferPool) Get() *buffer {
|
2025-10-13 22:08:58 +02:00
|
|
|
return pool.pool.Get().(*buffer)
|
2018-03-30 22:43:18 +02:00
|
|
|
}
|