mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-13 08:18:35 -07:00
Safer and faster atomicity
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Percentile struct {
|
type Percentile struct {
|
||||||
@@ -14,18 +13,13 @@ type Percentile struct {
|
|||||||
offset int64
|
offset int64
|
||||||
|
|
||||||
values []float64
|
values []float64
|
||||||
value *unsafe.Pointer
|
value uint64 // These bits are really a float64.
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPercentile(percentile float64, sampleWindow int) *Percentile {
|
func NewPercentile(percentile float64, sampleWindow int) *Percentile {
|
||||||
initial := 0
|
|
||||||
ptr := unsafe.Pointer(&initial)
|
|
||||||
|
|
||||||
return &Percentile{
|
return &Percentile{
|
||||||
percentile: percentile,
|
percentile: percentile,
|
||||||
|
values: make([]float64, 0, sampleWindow),
|
||||||
values: make([]float64, 0, sampleWindow),
|
|
||||||
value: &ptr,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,13 +58,13 @@ func (p *Percentile) AddSample(sample float64) {
|
|||||||
p.values[idx] = sample
|
p.values[idx] = sample
|
||||||
}
|
}
|
||||||
|
|
||||||
value := p.values[p.index()]
|
bits := math.Float64bits(p.values[p.index()])
|
||||||
atomic.SwapPointer(p.value, unsafe.Pointer(&value))
|
atomic.StoreUint64(&p.value, bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Percentile) Value() float64 {
|
func (p *Percentile) Value() float64 {
|
||||||
pointer := atomic.LoadPointer(p.value)
|
bits := atomic.LoadUint64(&p.value)
|
||||||
return *(*float64)(pointer)
|
return math.Float64frombits(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Percentile) index() int64 {
|
func (p *Percentile) index() int64 {
|
||||||
|
|||||||
Reference in New Issue
Block a user