gompool

package module
v0.0.0-...-bb37df0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2018 License: MIT Imports: 1 Imported by: 0

README

gompool

gompool is a simple allocation-free and lock-free memory pool library written in golang using treiber stack

Requirement

  • go (>= 1.8)

Installation

go get github.com/hlts2/gompool

Example


// Create 10 pools of `*bytes.Buffer`
pools := gompool.NewGompool(10, func() interface{} {
    return &bytes.Buffer{}
})

pool1 := pools.Get()
pool2 := pools.Get()

buf1 := pool1.Value.(*bytes.Buffer)
buf1.WriteString("Hello world1")

buf2 := pool2.Value.(*bytes.Buffer)
buf2.WriteString("Hello world2")

pools.Put(pool1)    // Return pool1 to pools
pools.Put(pool2)    // Return pool2 to pools

Benchmark

gompool vs sync.Pool(Standard library)

goos: darwin
goarch: amd64
pkg: github.com/hlts2/gompool
BenchmarkGompool-4       	30000000	        54.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkGompool-4       	30000000	        54.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkGompool-4       	30000000	        55.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkGompool-4       	30000000	        54.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkGompool-4       	30000000	        53.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefaultPool-4   	20000000	        66.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefaultPool-4   	20000000	        67.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefaultPool-4   	20000000	        67.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefaultPool-4   	20000000	        68.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkDefaultPool-4   	20000000	        68.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/hlts2/gompool	15.530s

Author

hlts2

LICENSE

gompool released under MIT license, refer LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gompool

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

Gompool is base gompool structor

func NewGompool

func NewGompool(uSize uint, fn func() interface{}) *Gompool

NewGompool returns Gompool instance

func (*Gompool) Cap

func (g *Gompool) Cap() int

Cap returns current capacity of pool

func (*Gompool) DestPool

func (g *Gompool) DestPool()

DestPool destroys all pools

func (*Gompool) Get

func (g *Gompool) Get() *treiber.Node

Get takes out of the pool

func (*Gompool) Put

func (g *Gompool) Put(node *treiber.Node)

Put puts node to pool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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