xsync

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

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

Go to latest
Published: Jun 19, 2023 License: CC0-1.0 Imports: 4 Imported by: 0

README

GoDoc

This package provides BunchAllocator which is close to sync.Pool by use cases, but it does not reuse memory to gain performance. Instead it allocates data by bunches (avoiding a lot of small allocated pieces). It is much less effective than sync.Pool, but does not require to put data back to the pool.

name                               old time/op    new time/op    delta
BunchAllocator/structSize1-16        7.62ns ± 5%    7.34ns ± 4%      ~     (p=0.310 n=5+5)
BunchAllocator/structSize8-16        10.5ns ± 2%     8.1ns ±15%   -22.84%  (p=0.008 n=5+5)
BunchAllocator/structSize32-16       17.0ns ± 7%    11.9ns ± 5%   -29.60%  (p=0.008 n=5+5)
BunchAllocator/structSize256-16      44.7ns ± 3%    33.1ns ±14%   -26.06%  (p=0.008 n=5+5)
BunchAllocator/structSize1024-16      156ns ±11%     190ns ± 5%   +21.78%  (p=0.008 n=5+5)
BunchAllocator/structSize65536-16    4.35µs ±27%    5.41µs ± 5%      ~     (p=0.095 n=5+5)

name                               old alloc/op   new alloc/op   delta
BunchAllocator/structSize8-16         8.00B ± 0%     9.00B ± 0%   +12.50%  (p=0.008 n=5+5)
BunchAllocator/structSize32-16        32.0B ± 0%     37.0B ± 0%   +15.62%  (p=0.008 n=5+5)
BunchAllocator/structSize256-16        256B ± 0%      288B ± 0%   +12.50%  (p=0.008 n=5+5)
BunchAllocator/structSize1024-16     1.02kB ± 0%    1.06kB ± 0%    +3.09%  (p=0.008 n=5+5)
BunchAllocator/structSize65536-16    65.5kB ± 0%    65.6kB ± 0%    +0.05%  (p=0.008 n=5+5)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BunchAllocator

type BunchAllocator[T any] struct {
	// contains filtered or unexported fields
}

BunchAllocator allocates a bunch of objects and then returns them one by one. This supposed to reduce overhead on memory management processes, but currently it works well only for objects of small size (around 8-256 bytes). If the structure is large, then it only adds overhead.

Latest benchmark results:

name old time/op new time/op delta BunchAllocator/structSize1-16 7.62ns ± 5% 7.34ns ± 4% ~ (p=0.310 n=5+5) BunchAllocator/structSize8-16 10.5ns ± 2% 8.1ns ±15% -22.84% (p=0.008 n=5+5) BunchAllocator/structSize32-16 17.0ns ± 7% 11.9ns ± 5% -29.60% (p=0.008 n=5+5) BunchAllocator/structSize256-16 44.7ns ± 3% 33.1ns ±14% -26.06% (p=0.008 n=5+5) BunchAllocator/structSize1024-16 156ns ±11% 190ns ± 5% +21.78% (p=0.008 n=5+5) BunchAllocator/structSize65536-16 4.35µs ±27% 5.41µs ± 5% ~ (p=0.095 n=5+5)

name old alloc/op new alloc/op delta BunchAllocator/structSize8-16 8.00B ± 0% 9.00B ± 0% +12.50% (p=0.008 n=5+5) BunchAllocator/structSize32-16 32.0B ± 0% 37.0B ± 0% +15.62% (p=0.008 n=5+5) BunchAllocator/structSize256-16 256B ± 0% 288B ± 0% +12.50% (p=0.008 n=5+5) BunchAllocator/structSize1024-16 1.02kB ± 0% 1.06kB ± 0% +3.09% (p=0.008 n=5+5) BunchAllocator/structSize65536-16 65.5kB ± 0% 65.6kB ± 0% +0.05% (p=0.008 n=5+5)

func NewBunchAllocator

func NewBunchAllocator[T any]() *BunchAllocator[T]

NewBunchAllocator returns a new instance of BunchAllocator

func (*BunchAllocator[T]) Get

func (a *BunchAllocator[T]) Get() *T

Get returns a new zero-valued instance of type T

type LazyInitSpinlock

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

func (*LazyInitSpinlock) ImmutableRun

func (once *LazyInitSpinlock) ImmutableRun(fn func())

func (*LazyInitSpinlock) InitedRun

func (once *LazyInitSpinlock) InitedRun(fn func())

type Pool

type Pool[T any] struct {
	sync.Pool
	Reset func(*T)
}

Pool is a type-asserted variant of sync.Pool.

func NewPoolR

func NewPoolR[T any](
	initFunc func(*T),
	resetFunc func(*T),
) *Pool[Releasable[T]]

NewPoolR returns a new Pool which returns value with method Release to put then back to the Pool (after use).

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get analogous to (*sync.Pool).Get, but already type asserted.

func (*Pool[T]) Put

func (p *Pool[T]) Put(x T)

Put analogous to (*sync.Pool).Put, but already type asserted, and with Reset function called.

type Releasable

type Releasable[T any] struct {
	// TODO: make the field embedded when
	// `type Value[T any] = T` will be permitted (Go1.22?).
	//
	// Partially related ticket: https://github.com/golang/go/issues/46477
	Value T
	Pool  *Pool[Releasable[T]]
}

Releasable is a wrapper for a value to make it releasable pack to its Pool.

func (Releasable[T]) Release

func (v Releasable[T]) Release()

Release puts the value back to the Pool.

Jump to

Keyboard shortcuts

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