pool

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2023 License: Apache-2.0, MIT Imports: 2 Imported by: 0

README

pbytes

The initial code cloned from https://github.com/gobwas/pool

Problem

https://research.swtch.com/interfaces

If you try to assign a []byte to an interface{}, the compiler will generate additional conversion code runtime.convTslice and cause memory allocation.

func main() {
	buffer := make([]byte, 0, 64)
	var o interface{} = buffer // CALL    runtime.convTslice(SB)
	fmt.Println(o)
}

convTslice source code https://go.dev/src/runtime/iface.go

func convTslice(val []byte) (x unsafe.Pointer) {
	// Note: this must work for any element type, not just byte.
	if (*slice)(unsafe.Pointer(&val)).array == nil {
		x = unsafe.Pointer(&zeroVal[0])
	} else {
		x = mallocgc(unsafe.Sizeof(val), sliceType, true)
		*(*[]byte)(x) = val
	}
	return
}

(*sync.Pool).Put source code https://go.dev/src/sync/pool.go

// Put adds x to the pool
func (p *Pool) Put(x any) {
    ...
}
var buf = make([]byte, 0, 16)
// equals:
// var x interface{} = buf // CALL    runtime.convTslice(SB)
// pool.Put(x)
pool.Put(buf) 

Resolved

Don't put the []byte into sync.Pool, to resolve the problems, put *[]byte instead.

Documentation

Overview

Package pool contains helpers for pooling structures distinguishable by size.

Quick example:

import "github.com/gobwas/pool"

func main() {
   // Reuse objects in logarithmic range from 0 to 64 (0,1,2,4,6,8,16,32,64).
   p := pool.New(0, 64)

   buf, n := p.Get(10) // Returns buffer with 16 capacity.
   if buf == nil {
       buf = bytes.NewBuffer(make([]byte, n))
   }
   defer p.Put(buf, n)

   // Work with buf.
}

There are non-generic implementations for pooling: - pool/pbytes for []byte reuse; - pool/pbufio for *bufio.Reader and *bufio.Writer reuse;

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	AddSize(n int)
	SetSizeMapping(func(int) int)
}

Config describes generic pool configuration.

type Option

type Option func(Config)

Option configures pool.

func WithIdentitySizeMapping

func WithIdentitySizeMapping() Option

func WithLogSizeMapping

func WithLogSizeMapping() Option

func WithLogSizeRange

func WithLogSizeRange(min, max int) Option

WithSizeLogRange returns an Option that will add logarithmic range of pooling sizes containing [min, max] values.

func WithSize

func WithSize(n int) Option

WithSize returns an Option that will add given pooling size to the pool.

func WithSizeMapping

func WithSizeMapping(sz func(int) int) Option

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool contains logic of reusing objects distinguishable by size in generic way.

func Custom

func Custom(opts ...Option) *Pool

Custom creates new Pool with given options.

func New

func New(min, max int) *Pool

New creates new Pool that reuses objects which size is in logarithmic range [min, max].

Note that it is a shortcut for Custom() constructor with Options provided by WithLogSizeMapping() and WithLogSizeRange(min, max) calls.

func (*Pool) Get

func (p *Pool) Get(size int) (interface{}, int)

Get pulls object whose generic size is at least of given size. It also returns a real size of x for further pass to Put() even if x is nil. Note that size could be ceiled to the next power of two.

func (*Pool) Put

func (p *Pool) Put(x interface{}, size int)

Put takes x and its size for future reuse.

Directories

Path Synopsis
internal
Package pbytes contains tools for pooling byte pool.
Package pbytes contains tools for pooling byte pool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL