Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Slice ¶
type Slice[T any] struct { // Init is an optional callback for initializing the created item x. Init func(x *T) // contains filtered or unexported fields }
Slice allows goroutine-safe access to []*T items with automatic growth of the slice.
This is a replacement for [workersCount]*T where workersCount isn't known beforehand.
It also prevents from false sharing of the created T items on multi-CPU systems.
func (*Slice[T]) Get ¶
Get returns *T item for the given workerID in a goroutine-safe manner.
The returned item is automatically created via s.New on the first access.
It is expected that only a single goroutine can access *T at workerID index at any given time.
func (*Slice[T]) GetSlice ¶
func (s *Slice[T]) GetSlice() []*T
GetSlice returns the underlying []*T.
The length of the returned slice equals to the max(workerID)+1 passed to s.Get(). It is guaranteed that all the items in the returned slice are non-nil.
It is unsafe calling this function when concurrent goroutines access s.
GetSlice() is relatively slow, so it shouldn't be called in hot paths.