diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-pools')
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/LICENSE | 9 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/README.md | 2 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/bufio.go | 75 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/bytes.go | 46 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/fastpath.go | 46 |
5 files changed, 178 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-pools/LICENSE b/vendor/codeberg.org/gruf/go-pools/LICENSE new file mode 100644 index 000000000..b7c4417ac --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2021 gruf + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/codeberg.org/gruf/go-pools/README.md b/vendor/codeberg.org/gruf/go-pools/README.md new file mode 100644 index 000000000..673324271 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/README.md @@ -0,0 +1,2 @@ +A selection of type-defined `sync.Pool` implementations with redefined "getter" and "putter" +methods to handle their appropriate types.
\ No newline at end of file diff --git a/vendor/codeberg.org/gruf/go-pools/bufio.go b/vendor/codeberg.org/gruf/go-pools/bufio.go new file mode 100644 index 000000000..8c2ef9730 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/bufio.go @@ -0,0 +1,75 @@ +package pools + +import ( + "bufio" + "io" + "sync" +) + +// BufioReaderPool is a pooled allocator for bufio.Reader objects +type BufioReaderPool interface { + // Get fetches a bufio.Reader from pool and resets to supplied reader + Get(io.Reader) *bufio.Reader + + // Put places supplied bufio.Reader back in pool + Put(*bufio.Reader) +} + +// NewBufioReaderPool returns a newly instantiated bufio.Reader pool +func NewBufioReaderPool(size int) BufioReaderPool { + return &bufioReaderPool{ + Pool: sync.Pool{ + New: func() interface{} { + return bufio.NewReaderSize(nil, size) + }, + }, + } +} + +// bufioReaderPool is our implementation of BufioReaderPool +type bufioReaderPool struct{ sync.Pool } + +func (p *bufioReaderPool) Get(r io.Reader) *bufio.Reader { + br := p.Pool.Get().(*bufio.Reader) + br.Reset(r) + return br +} + +func (p *bufioReaderPool) Put(br *bufio.Reader) { + br.Reset(nil) + p.Pool.Put(br) +} + +// BufioWriterPool is a pooled allocator for bufio.Writer objects +type BufioWriterPool interface { + // Get fetches a bufio.Writer from pool and resets to supplied writer + Get(io.Writer) *bufio.Writer + + // Put places supplied bufio.Writer back in pool + Put(*bufio.Writer) +} + +// NewBufioWriterPool returns a newly instantiated bufio.Writer pool +func NewBufioWriterPool(size int) BufioWriterPool { + return &bufioWriterPool{ + Pool: sync.Pool{ + New: func() interface{} { + return bufio.NewWriterSize(nil, size) + }, + }, + } +} + +// bufioWriterPool is our implementation of BufioWriterPool +type bufioWriterPool struct{ sync.Pool } + +func (p *bufioWriterPool) Get(w io.Writer) *bufio.Writer { + bw := p.Pool.Get().(*bufio.Writer) + bw.Reset(w) + return bw +} + +func (p *bufioWriterPool) Put(bw *bufio.Writer) { + bw.Reset(nil) + p.Pool.Put(bw) +} diff --git a/vendor/codeberg.org/gruf/go-pools/bytes.go b/vendor/codeberg.org/gruf/go-pools/bytes.go new file mode 100644 index 000000000..76fe18616 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/bytes.go @@ -0,0 +1,46 @@ +package pools + +import ( + "sync" + + "codeberg.org/gruf/go-bytes" +) + +// BufferPool is a pooled allocator for bytes.Buffer objects +type BufferPool interface { + // Get fetches a bytes.Buffer from pool + Get() *bytes.Buffer + + // Put places supplied bytes.Buffer in pool + Put(*bytes.Buffer) +} + +// NewBufferPool returns a newly instantiated bytes.Buffer pool +func NewBufferPool(size int) BufferPool { + return &bufferPool{ + pool: sync.Pool{ + New: func() interface{} { + return &bytes.Buffer{B: make([]byte, 0, size)} + }, + }, + size: size, + } +} + +// bufferPool is our implementation of BufferPool +type bufferPool struct { + pool sync.Pool + size int +} + +func (p *bufferPool) Get() *bytes.Buffer { + return p.pool.Get().(*bytes.Buffer) +} + +func (p *bufferPool) Put(buf *bytes.Buffer) { + if buf.Cap() < p.size { + return + } + buf.Reset() + p.pool.Put(buf) +} diff --git a/vendor/codeberg.org/gruf/go-pools/fastpath.go b/vendor/codeberg.org/gruf/go-pools/fastpath.go new file mode 100644 index 000000000..eb76f03e4 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/fastpath.go @@ -0,0 +1,46 @@ +package pools + +import ( + "sync" + + "codeberg.org/gruf/go-fastpath" +) + +// PathBuilderPool is a pooled allocator for fastpath.Builder objects +type PathBuilderPool interface { + // Get fetches a fastpath.Builder from pool + Get() *fastpath.Builder + + // Put places supplied fastpath.Builder back in pool + Put(*fastpath.Builder) +} + +// NewPathBuilderPool returns a newly instantiated fastpath.Builder pool +func NewPathBuilderPool(size int) PathBuilderPool { + return &pathBuilderPool{ + pool: sync.Pool{ + New: func() interface{} { + return &fastpath.Builder{B: make([]byte, 0, size)} + }, + }, + size: size, + } +} + +// pathBuilderPool is our implementation of PathBuilderPool +type pathBuilderPool struct { + pool sync.Pool + size int +} + +func (p *pathBuilderPool) Get() *fastpath.Builder { + return p.pool.Get().(*fastpath.Builder) +} + +func (p *pathBuilderPool) Put(pb *fastpath.Builder) { + if pb.Cap() < p.size { + return + } + pb.Reset() + p.pool.Put(pb) +} |