mempool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 3 Imported by: 0

README

A improved mempool

Original forked from @Dreamacro

Why

@xiaci wrote this pool, and it's improved by @Dreamacro.

It's used for the common memory usage case in Go to reduce STW time every GC round.

However, it will still cause allocation in Dreamacro version, to know why, see the description about sync.Pool

Even though using a non-pointer element will be more convenience for developer, it will cause unnecessary allocation.

According to the benchmark, using non-pointer []byte spend 70ns+ while pointer one only spend 20ns.

How to use

buf := mempool.Get( YOUR_SIZE(from 1-65536) )
file.Read(buf.B)
file.Write(buf.B)
...after many read and write operation
mempool.Put(buf)
DON'T DO

This will make buffer starting from 5, and it cannot be resized after that.

buf.B = buf.B[5:]
Should DO
newbuf := buf.B[5:]

But this is allowed

buf.B = buf.B[:5]

Or more convenience

buf.SetLen(5)

When to Use

Frequently allocating large buffers case, like Proxy or HTTP Server.

If you can reuse the buffer, the mempool is unnecessary even making your program slower.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Put

func Put(buf *Buffer) error

Types

type Allocator

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

Allocator for incoming frames, optimized to prevent overwriting after zeroing

func NewAllocator

func NewAllocator() *Allocator

NewAllocator initiates a []byte allocator for frames less than 65536 bytes, the waste(memory fragmentation) of space allocation is guaranteed to be no more than 50%.

func (*Allocator) Get

func (alloc *Allocator) Get(size int) *Buffer

Get a []byte from pool with most appropriate cap

func (*Allocator) Put

func (alloc *Allocator) Put(b *Buffer) error

Put returns a []byte to pool for future use, which the cap must be exactly 2^n

type Buffer

type Buffer struct {
	B []byte
}

func Get

func Get(size int) *Buffer

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) SetLen

func (b *Buffer) SetLen(len int)

Jump to

Keyboard shortcuts

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