Debian Codegraph @

exp/saturating_counter.go

raw
1 // saturating_counter.go - packed multi-bit saturating counters using atomic CAS
2 //
3 // Generalizes atomicBitfield to k-bit counters per slot. For k=1 this is
4 // equivalent to a visited bitfield (64 slots per word). For k=3, each slot
5 // uses 2 bits (32 slots per word), and counters saturate at 3.
6 //
7 // Read() is a single atomic load. Increment()/Decrement() use CAS loops
8 // with saturation early-exit.
9
10 package sieve
11
12 import (
13 "math/bits"
14 "sync/atomic"
15 )
16
17 // atomicSaturatingCounter is a packed array of multi-bit counters.
18 // Each counter occupies bitsPerSlot bits within a uint64 word and
19 // saturates at maxVal (which equals k).
20 type atomicSaturatingCounter struct {
21 words []uint64
22 bitsPerSlot uint
23 slotsPerWord uint
24 maxVal uint64
25 mask uint64
26 }
27
28 var _ visitor = &atomicSaturatingCounter{}
29
30 // newAtomicSaturatingCounter creates a counter array with enough words
31 // for capacity slots, where each counter saturates at k.
32 // k must be >= 1.
33 func newAtomicSaturatingCounter(capacity int, k int) *atomicSaturatingCounter {
34 if k < 1 {
35 k = 1
36 }
37 bps := uint(bits.Len(uint(k))) // k=1→1, k=2..3→2, k=4..7→3
38 spw := 64 / bps
39 nwords := (uint(capacity) + spw - 1) / spw
40
41 return &atomicSaturatingCounter{
42 words: make([]uint64, nwords),
43 bitsPerSlot: bps,
44 slotsPerWord: spw,
45 maxVal: uint64(k),
46 mask: (1 << bps) - 1,
47 }
48 }
49
50 // Mark increments the counter for slot idx, saturating at maxVal (k).
51 // Since the saturation check prevents overflow into the adjacent slot,
52 // we can add 1<<shift directly instead of extract→clear→reinsert.
53 func (sc *atomicSaturatingCounter) Mark(idx int32) {
54 w := uint(idx) / sc.slotsPerWord
55 word := &sc.words[w]
56
57 shift := (uint(idx) % sc.slotsPerWord) * sc.bitsPerSlot
58 incr := uint64(1) << shift
59 vmax := sc.maxVal << shift
60 mask := sc.mask << shift
61
62 for {
63 z := atomic.LoadUint64(word)
64 if z&mask >= vmax {
65 return // saturated
66 }
67 if atomic.CompareAndSwapUint64(word, z, z+incr) {
68 return
69 }
70 }
71 }
72
73 // Clear decrements the counter for slot idx, saturating at 0.
74 // Since the zero check prevents underflow/borrow from the adjacent slot,
75 // we can subtract 1<<shift directly.
76 func (sc *atomicSaturatingCounter) Clear(idx int32) {
77 w := uint(idx) / sc.slotsPerWord
78 word := &sc.words[w]
79
80 shift := (uint(idx) % sc.slotsPerWord) * sc.bitsPerSlot
81 decr := uint64(1) << shift
82 mask := sc.mask << shift
83
84 for {
85 z := atomic.LoadUint64(word)
86 if z&mask == 0 {
87 return
88 }
89 if atomic.CompareAndSwapUint64(word, z, z-decr) {
90 return
91 }
92 }
93 }
94
95 // Reset sets the counter for slot idx to 0.
96 func (sc *atomicSaturatingCounter) Reset(idx int32) {
97 w := uint(idx) / sc.slotsPerWord
98 word := &sc.words[w]
99
100 shift := (uint(idx) % sc.slotsPerWord) * sc.bitsPerSlot
101 mask := sc.mask << shift
102
103 for {
104 z := atomic.LoadUint64(word)
105 if z&mask == 0 {
106 return // already zero
107 }
108 if atomic.CompareAndSwapUint64(word, z, z&^mask) {
109 return
110 }
111 }
112 }
113
114 // read returns the raw counter value for slot idx (used in tests).
115 func (sc *atomicSaturatingCounter) read(idx int32) uint64 {
116 w := uint(idx) / sc.slotsPerWord
117 shift := (uint(idx) % sc.slotsPerWord) * sc.bitsPerSlot
118 word := atomic.LoadUint64(&sc.words[w])
119 return (word >> shift) & sc.mask
120 }
121
122 // IsVisited returns true if the counter for slot idx is > 0.
123 // Masks in place to avoid the right-shift.
124 func (sc *atomicSaturatingCounter) IsVisited(idx int32) bool {
125 w := uint(idx) / sc.slotsPerWord
126 shift := (uint(idx) % sc.slotsPerWord) * sc.bitsPerSlot
127 word := atomic.LoadUint64(&sc.words[w])
128 return word&(sc.mask<<shift) != 0
129 }
130
131 // ResetAll clears all counters. Called from Purge() under lock — plain stores are fine.
132 func (sc *atomicSaturatingCounter) ResetAll() {
133 for i := range sc.words {
134 sc.words[i] = 0
135 }
136 }