Documentation ¶
Overview ¶
Package goadder contains a collection of thread-safe, concurrent data structures for reading and writing numeric int64-counter, inspired by OpenJDK9 LongAdder.
Beside JDKAdder, ported version of OpenJDK9 LongAdder, package also provides other alternatives for various use cases.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtomicAdder ¶
type AtomicAdder struct {
// contains filtered or unexported fields
}
AtomicAdder is simple atomic-based adder. Fastest at single routine but slow at multi routine when high-contention happens.
func (*AtomicAdder) Reset ¶
func (a *AtomicAdder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy.
func (*AtomicAdder) Store ¶
func (a *AtomicAdder) Store(v int64)
Store value. This function is only effective if there are no concurrent updates.
func (*AtomicAdder) Sum ¶
func (a *AtomicAdder) Sum() int64
Sum return the current sum. The returned value is NOT an atomic snapshot because of concurrent update.
func (*AtomicAdder) SumAndReset ¶
func (a *AtomicAdder) SumAndReset() (sum int64)
SumAndReset equivalent in effect to sum followed by reset. Like the nature of Sum and Reset, this function is only effective if there are no concurrent updates.
type AtomicF64Adder ¶ added in v0.1.2
type AtomicF64Adder struct {
// contains filtered or unexported fields
}
AtomicF64Adder is simple atomic-based adder. Fastest at single routine but slow at multi routine when high-contention happens.
func NewAtomicF64Adder ¶ added in v0.1.2
func NewAtomicF64Adder() *AtomicF64Adder
NewAtomicF64Adder create new AtomicF64Adder
func (*AtomicF64Adder) Add ¶ added in v0.1.2
func (a *AtomicF64Adder) Add(v float64)
Add the given value
func (*AtomicF64Adder) Reset ¶ added in v0.1.2
func (a *AtomicF64Adder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy.
func (*AtomicF64Adder) Store ¶ added in v0.1.2
func (a *AtomicF64Adder) Store(v float64)
Store value. This function is only effective if there are no concurrent updates.
func (*AtomicF64Adder) Sum ¶ added in v0.1.2
func (a *AtomicF64Adder) Sum() float64
Sum return the current sum. The returned value is NOT an atomic snapshot because of concurrent update.
func (*AtomicF64Adder) SumAndReset ¶ added in v0.1.2
func (a *AtomicF64Adder) SumAndReset() (sum float64)
SumAndReset equivalent in effect to sum followed by reset. Like the nature of Sum and Reset, this function is only effective if there are no concurrent updates.
type Float64Adder ¶ added in v0.1.2
type Float64Adder interface { Add(x float64) Inc() Dec() Sum() float64 Reset() SumAndReset() float64 Store(v float64) }
Float64Adder interface
func NewFloat64Adder ¶ added in v0.1.2
func NewFloat64Adder(t Type) Float64Adder
NewFloat64Adder create new float64 adder upon type
type FloatBinaryOperator ¶ added in v0.1.2
FloatBinaryOperator represents an operation upon two float64-valued operands and producing an float64-valued result
type JDKAdder ¶
type JDKAdder struct {
Striped64
}
JDKAdder is ported version of OpenJDK9 LongAdder.
When multiple routines update a common sum that is used for purposes such as collecting statistics, not for fine-grained synchronization control, contention overhead could be a pain.
JDKAdder is preferable to atomic, delivers significantly higher throughput under high contention, at the expense of higher space consumption, while keeping same characteristics under low contention.
One or more variables, called Cells, together maintain an initially zero sum. When updates are contended across routines, the set of variables may grow dynamically to reduce contention. In other words, updates are distributed over Cells. The value is lazy, only aggregated (sum) over Cells when needed.
JDKAdder is high performance, non-blocking and safe for concurrent use.
func (*JDKAdder) Reset ¶
func (u *JDKAdder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy.
func (*JDKAdder) Store ¶
Store value. This function is only effective if there are no concurrent updates.
func (*JDKAdder) Sum ¶
Sum return the current sum. The returned value is NOT an atomic snapshot because of concurrent update.
func (*JDKAdder) SumAndReset ¶
SumAndReset equivalent in effect to sum followed by reset. Like the nature of Sum and Reset, this function is only effective if there are no concurrent updates.
type JDKF64Adder ¶ added in v0.1.2
type JDKF64Adder struct {
StripedF64
}
JDKF64Adder is ported version of OpenJDK9 DoubleAdder.
When multiple routines update a common sum that is used for purposes such as collecting statistics, not for fine-grained synchronization control, contention overhead could be a pain.
JDKAdder is preferable to atomic, delivers significantly higher throughput under high contention, at the expense of higher space consumption, while keeping same characteristics under low contention.
One or more variables, called Cells, together maintain an initially zero sum. When updates are contended across routines, the set of variables may grow dynamically to reduce contention. In other words, updates are distributed over Cells. The value is lazy, only aggregated (sum) over Cells when needed.
JDKF64Adder is high performance, non-blocking and safe for concurrent use.
func NewJDKF64Adder ¶ added in v0.1.2
func NewJDKF64Adder() *JDKF64Adder
NewJDKF64Adder create new JDKF64Adder
func (*JDKF64Adder) Reset ¶ added in v0.1.2
func (f *JDKF64Adder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy.
func (*JDKF64Adder) Store ¶ added in v0.1.2
func (f *JDKF64Adder) Store(v float64)
Store value. This function is only effective if there are no concurrent updates.
func (*JDKF64Adder) Sum ¶ added in v0.1.2
func (f *JDKF64Adder) Sum() float64
Sum return the current sum. The returned value is NOT an atomic snapshot because of concurrent update.
func (*JDKF64Adder) SumAndReset ¶ added in v0.1.2
func (f *JDKF64Adder) SumAndReset() (sum float64)
SumAndReset equivalent in effect to sum followed by reset. Like the nature of Sum and Reset, this function is only effective if there are no concurrent updates.
type LongAdder ¶
type LongAdder interface { Add(x int64) Inc() Dec() Sum() int64 Reset() SumAndReset() int64 Store(v int64) }
LongAdder interface
func NewLongAdder ¶
NewLongAdder create new long adder upon type
type LongBinaryOperator ¶
LongBinaryOperator represents an operation upon two int64-valued operands and producing an int64-valued result
type MutexAdder ¶
type MutexAdder struct {
// contains filtered or unexported fields
}
MutexAdder is mutex-based LongAdder. Slowest compared to other alternatives.
func (*MutexAdder) Reset ¶
func (m *MutexAdder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy.
func (*MutexAdder) Store ¶
func (m *MutexAdder) Store(v int64)
Store value. This function is only effective if there are no concurrent updates.
func (*MutexAdder) Sum ¶
func (m *MutexAdder) Sum() (sum int64)
Sum return the current sum. The returned value is NOT an atomic snapshot because of concurrent update.
func (*MutexAdder) SumAndReset ¶
func (m *MutexAdder) SumAndReset() (sum int64)
SumAndReset equivalent in effect to sum followed by reset. Like the nature of Sum and Reset, this function is only effective if there are no concurrent updates.
type RandomCellAdder ¶
type RandomCellAdder struct {
// contains filtered or unexported fields
}
RandomCellAdder takes idea from JDKAdder by preallocating a fixed number of Cells. Unlike JDKAdder, in each update, RandomCellAdder assign a random-fixed Cell to invoker instead of retry/reassign Cell when contention.
RandomCellAdder is often faster than JDKAdder in multi routine race benchmark but slower in case of single routine (no race).
RandomCellAdder consume ~1KB for storing cells, which is often larger than JDKAdder which number of cells is dynamic.
func NewRandomCellAdder ¶
func NewRandomCellAdder() *RandomCellAdder
NewRandomCellAdder create new RandomCellAdder
func (*RandomCellAdder) Reset ¶
func (r *RandomCellAdder) Reset()
Reset variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy
func (*RandomCellAdder) Store ¶
func (r *RandomCellAdder) Store(v int64)
Store value. This function is only effective if there are no concurrent updates.
func (*RandomCellAdder) Sum ¶
func (r *RandomCellAdder) Sum() (sum int64)
Sum return the current sum. The returned value is NOT an atomic snapshot; invocation in the absence of concurrent updates returns an accurate result, but concurrent updates that occur while the sum is being calculated might not be incorporated.
func (*RandomCellAdder) SumAndReset ¶
func (r *RandomCellAdder) SumAndReset() (sum int64)
SumAndReset equivalent in effect to sum followed by reset. This method may apply for example during quiescent points between multithreaded computations. If there are updates concurrent with this method, the returned value is guaranteed to be the final value occurring before the reset.
type Striped64 ¶
type Striped64 struct {
// contains filtered or unexported fields
}
It is possible for a Cell to become unused when routines that once hashed to it terminate, as well as in the case where doubling the table causes no routine to hash to it under expanded mask. We do not try to detect or remove such cells, under the assumption that for long-running instances, observed contention levels will recur, so the cells will eventually be needed again; and for short-lived ones, it does not matter.
type StripedF64 ¶ added in v0.1.2
type StripedF64 struct {
// contains filtered or unexported fields
}
StripedF64 same as Striped64 but for float64
type Type ¶
type Type int
Type of LongAdder
const ( // JDKAdderType is type for JDK-based LongAdder JDKAdderType Type = iota // RandomCellAdderType is type for RandomCellAdder RandomCellAdderType // AtomicAdderType is type for atomic-based adder AtomicAdderType // MutexAdderType is type for MutexAdder MutexAdderType // JDKF64AdderType is type for JDK-based DoubleAdder JDKF64AdderType // AtomicF64AdderType is type for atomic-based float64 adder AtomicF64AdderType )