Documentation
¶
Overview ¶
Package bufpool recycles off-heap byte buffers of power-of-two sizes, for I/O and decoding that would otherwise churn []byte through the Go heap.
Sizes start at 4 KiB, and every buffer is aligned to its size up to the operating system's page, which direct I/O and registered ring buffers need. Contents of a buffer are undefined when handed out, because the point is not paying to clear what a read is about to overwrite.
Example ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/bufpool"
)
func main() {
var a memory.Local
defer a.Close()
p, err := bufpool.New(&a, 1<<20)
if err != nil {
panic(err)
}
b, err := p.Get(5000)
if err != nil {
panic(err)
}
fmt.Println(len(b.Bytes()), b.Cap())
if err := p.Put(b); err != nil {
panic(err)
}
}
Output: 5000 8192
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrForeign = errors.New("buffer is not checked out of this pool")
ErrForeign reports a Buf that did not come from this pool or was put back already.
Functions ¶
This section is empty.
Types ¶
type Buf ¶
type Buf struct {
// contains filtered or unexported fields
}
Buf is a buffer checked out of a Pool. It may be stored off-heap.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool hands out buffers and keeps returned ones for reuse, up to a limit. It is safe for concurrent use if its allocator is a Shared.
func (*Pool) Get ¶
Get returns a buffer of length n with undefined contents. Requests too large to pool are allocated and freed directly.