package shardval

import "gomo.prashantv.com/sync/exp/shardval"

Package shardval provides an implementation of a sharded value.

This is useful for performance-sensitive code that is running across many cores where contention can cause significant slowdowns. See benchmarks for more details.

Index

Types

type Value

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

Value is a sharded value implemented using a sync.Pool for per-P locality. It is used to represent a value that's sharded into many pieces which are updated in `With`, and then assembled in `ForEach`.

The number of shards should scale with O(GOMAXPROCS), though there is no hard limit.

func (*Value[T]) ForEach

func (val *Value[T]) ForEach(fn func(*T))

ForEach iterates over all shards.

If called concurretnly with [With], the same value may be processed by both functions so addititional synchronization within T is required.

func (*Value[T]) Shards

func (val *Value[T]) Shards() int

Shards returns the number of shards.

func (*Value[T]) With

func (val *Value[T]) With(fn func(*T))

With runs the given function with exclusive access to the shard.

Many callers may call With concurrently.

If called concurrently with [ForEach], the same value may be processed by both functions so addititional synchronization within T is required.