Debian Codegraph @

exp/visitor_uint32.go

raw
1 // visitor_uint32.go - per-slot uint32 visitor implementations
2 //
3 // Two concrete types, zero branches on the hot path:
4 //
5 // visitedBool (k=1): slot is 0 or 1. Mark/Clear are plain stores.
6 // visitedCounter (k>1): slot is a saturating counter 0..k. CAS loops.
7 //
8 // Both use one uint32 per node (16 entries per cache line), reducing
9 // false sharing vs the packed bitfield (512 entries per cache line).
10 // For 1M entries: 4 MB vs 16 KB.
11 //
12 // The visitor interface dispatches at construction time (New vs
13 // NewWithVisits), so the hot path is a direct method call with no
14 // runtime branching.
15
16 package sieve
17
18 import "sync/atomic"
19
20 // visitor is the legacy interface for tracking visited status of cache nodes.
21 // Retained for the standalone visitedBool/visitedCounter types and their tests.
22 // The Sieve struct itself uses slotState (combined lock+visitor).
23 type visitor interface {
24 Mark(idx int32)
25 Clear(idx int32)
26 Reset(idx int32)
27 IsVisited(idx int32) bool
28 ResetAll()
29 }
30
31 // --- k=1: boolean visited flag ---
32
33 // visitedBool tracks visited status with one uint32 per node (0 or 1).
34 // All operations are branchless on the hot path (single atomic op).
35 type visitedBool struct {
36 slots []uint32
37 }
38
39 var _ visitor = &visitedBool{}
40
41 func newVisitedBool(capacity int) *visitedBool {
42 return &visitedBool{
43 slots: make([]uint32, capacity),
44 }
45 }
46
47 // Mark sets the visited flag. Fast-path load skips the store if already set.
48 func (v *visitedBool) Mark(idx int32) {
49 if atomic.LoadUint32(&v.slots[idx]) != 0 {
50 return
51 }
52 atomic.StoreUint32(&v.slots[idx], 1)
53 }
54
55 // Clear clears the visited flag. Single store, no CAS.
56 func (v *visitedBool) Clear(idx int32) {
57 atomic.StoreUint32(&v.slots[idx], 0)
58 }
59
60 // Reset clears the visited flag (same as Clear for k=1).
61 func (v *visitedBool) Reset(idx int32) {
62 atomic.StoreUint32(&v.slots[idx], 0)
63 }
64
65 // IsVisited returns true if the visited flag is set. Single load.
66 func (v *visitedBool) IsVisited(idx int32) bool {
67 return atomic.LoadUint32(&v.slots[idx]) != 0
68 }
69
70 // ResetAll clears all flags. Called from Purge() under s.mu.
71 func (v *visitedBool) ResetAll() {
72 for i := range v.slots {
73 v.slots[i] = 0
74 }
75 }
76
77 // --- k>1: saturating counter ---
78
79 // visitedCounter tracks visited status with a saturating counter per node.
80 // Counter values range from 0 to maxVal (= k). Mark increments, Clear
81 // decrements, both saturate at the boundary.
82 type visitedCounter struct {
83 slots []uint32
84 maxVal uint32
85 }
86
87 var _ visitor = &visitedCounter{}
88
89 func newVisitedCounter(capacity int, k int) *visitedCounter {
90 return &visitedCounter{
91 slots: make([]uint32, capacity),
92 maxVal: uint32(k),
93 }
94 }
95
96 // Mark increments the counter, saturating at maxVal.
97 func (v *visitedCounter) Mark(idx int32) {
98 w := &v.slots[idx]
99 for {
100 old := atomic.LoadUint32(w)
101 if old >= v.maxVal {
102 return
103 }
104 if atomic.CompareAndSwapUint32(w, old, old+1) {
105 return
106 }
107 }
108 }
109
110 // Clear decrements the counter, saturating at 0.
111 func (v *visitedCounter) Clear(idx int32) {
112 w := &v.slots[idx]
113 for {
114 old := atomic.LoadUint32(w)
115 if old == 0 {
116 return
117 }
118 if atomic.CompareAndSwapUint32(w, old, old-1) {
119 return
120 }
121 }
122 }
123
124 // Reset sets the counter to 0.
125 func (v *visitedCounter) Reset(idx int32) {
126 atomic.StoreUint32(&v.slots[idx], 0)
127 }
128
129 // IsVisited returns true if the counter is > 0.
130 func (v *visitedCounter) IsVisited(idx int32) bool {
131 return atomic.LoadUint32(&v.slots[idx]) != 0
132 }
133
134 // ResetAll clears all counters. Called from Purge() under s.mu.
135 func (v *visitedCounter) ResetAll() {
136 for i := range v.slots {
137 v.slots[i] = 0
138 }
139 }