https://github.com/jille/throughputbuffer
An efficient high throughput buffer in Go
https://github.com/jille/throughputbuffer
buffer golang performance throughput
Last synced: 10 months ago
JSON representation
An efficient high throughput buffer in Go
- Host: GitHub
- URL: https://github.com/jille/throughputbuffer
- Owner: Jille
- License: bsd-2-clause
- Created: 2022-09-06T09:13:07.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-27T16:05:20.000Z (almost 2 years ago)
- Last Synced: 2025-01-22T12:45:48.586Z (12 months ago)
- Topics: buffer, golang, performance, throughput
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# throughputbuffer
[](https://godoc.org/github.com/Jille/throughputbuffer)
Package throughputbuffer provides a high throughput indefinitely growing io.ReadWriter, like bytes.Buffer, but optimized for minimal copies.
It does the minimum amount of copies (1 per read + 1 per write) (or fewer through ReadFrom and WriteTo) and never has to move bytes in the buffer.
Byte slices are shared in a BufferPool. All byte slices are of the same length: the size given to New().
Buffers automatically shrink as data is read from them (they return their data to the pool).
```go
package main
import (
"crypto/rand"
"github.com/Jille/throughputbuffer"
)
func main() {
pool := throughputbuffer.New(64 * 1024 * 1024)
buf1 := pool.Get()
io.CopyN(buf1, rand.Reader, 1024 * 1024 * 1024)
buf2 := pool.Get()
io.Copy(buf2, buf1) // As data is read from buf, the byteslices are freed and reused by buf2.
}
```