Skip to main content

euv_core/reactive/runtime/
struct.rs

1use super::*;
2
3/// Compact 28-byte intrusive doubly-linked edge connecting a reactive source to a subscriber in O(1).
4#[derive(Clone, Copy, CustomDebug, Data)]
5pub(crate) struct ReactiveEdge {
6    /// Packed source tag (`bit 31`: `0` = signal, `1` = memo; low 31 bits = arena index; `EDGE_NONE` when free).
7    #[get(pub(crate), type(copy))]
8    #[get_mut(pub(crate))]
9    #[set(pub(crate))]
10    pub(crate) source_tag: u32,
11    /// Packed subscriber tag (`bit 31`: `0` = memo, `1` = effect; low 31 bits = arena index).
12    #[get(pub(crate), type(copy))]
13    #[get_mut(pub(crate))]
14    #[set(pub(crate))]
15    pub(crate) sub_tag: u32,
16    /// Previous edge index in the source's subscriber doubly-linked list (`EDGE_NONE` at head).
17    #[get(pub(crate), type(copy))]
18    #[get_mut(pub(crate))]
19    #[set(pub(crate))]
20    pub(crate) prev_sub: u32,
21    /// Next edge index in the source's subscriber doubly-linked list (`EDGE_NONE` at tail).
22    #[get(pub(crate), type(copy))]
23    #[get_mut(pub(crate))]
24    #[set(pub(crate))]
25    pub(crate) next_sub: u32,
26    /// Previous edge index in the subscriber's dependency doubly-linked list (`EDGE_NONE` at head).
27    #[get(pub(crate), type(copy))]
28    #[get_mut(pub(crate))]
29    #[set(pub(crate))]
30    pub(crate) prev_dep: u32,
31    /// Next edge index in the subscriber's dependency doubly-linked list (`EDGE_NONE` at tail).
32    #[get(pub(crate), type(copy))]
33    #[get_mut(pub(crate))]
34    #[set(pub(crate))]
35    pub(crate) next_dep: u32,
36    /// Subscriber tracking epoch stamped on the most recent visit of this edge.
37    #[get(pub(crate), type(copy))]
38    #[get_mut(pub(crate))]
39    #[set(pub(crate))]
40    pub(crate) epoch: u32,
41}
42
43/// Active observer tracking frame pushed onto `ReactiveRuntime::observer_stack` for epoch mark-and-sweep tracking.
44#[derive(Clone, Copy, CustomDebug, Data)]
45pub(crate) struct ObserverFrame {
46    /// Packed subscriber tag (`bit 31`: `0` = memo, `1` = effect; low 31 bits = arena index).
47    #[get(pub(crate), type(copy))]
48    #[get_mut(pub(crate))]
49    #[set(pub(crate))]
50    pub(crate) sub_tag: u32,
51    /// Generation snapshot verifying the subscriber slot was not recycled mid-execution.
52    #[get(pub(crate), type(copy))]
53    #[get_mut(pub(crate))]
54    #[set(pub(crate))]
55    pub(crate) generation: u32,
56    /// Current tracking epoch; edges not stamped with `epoch` are detached in `finish_tracking`.
57    #[get(pub(crate), type(copy))]
58    #[get_mut(pub(crate))]
59    #[set(pub(crate))]
60    pub(crate) epoch: u32,
61    /// Next expected dependency edge index along the subscriber's intrusive list (`EDGE_NONE` at end).
62    #[get(pub(crate), type(copy))]
63    #[get_mut(pub(crate))]
64    #[set(pub(crate))]
65    pub(crate) cursor_edge: u32,
66    /// Most recently visited edge index enabling O(1) deduplication of consecutive reads.
67    #[get(pub(crate), type(copy))]
68    #[get_mut(pub(crate))]
69    #[set(pub(crate))]
70    pub(crate) last_edge: u32,
71    /// Count of distinct edges stamped with `epoch` during this pass (`== dep_count` skips sweep in O(1)).
72    #[get(pub(crate), type(copy))]
73    #[get_mut(pub(crate))]
74    #[set(pub(crate))]
75    pub(crate) visited_count: u32,
76}
77
78/// Storage entry for a small-buffer-optimized signal inside the reactive arena.
79#[derive(CustomDebug, Data)]
80pub(crate) struct SignalEntry {
81    /// Generation counter for this slot.
82    #[get(pub(crate), type(copy))]
83    #[get_mut(pub(crate))]
84    #[set(pub(crate))]
85    pub(crate) generation: u32,
86    /// Monotonic version counter incremented on every write.
87    #[get(pub(crate), type(copy))]
88    #[get_mut(pub(crate))]
89    #[set(pub(crate))]
90    pub(crate) version: u64,
91    /// Type-erased 24-byte SBO signal payload; empty when slot is free.
92    #[debug(skip)]
93    #[get(pub(crate))]
94    #[get_mut(pub(crate))]
95    #[set(pub(crate))]
96    pub(crate) value: SignalSlot,
97    /// Head edge index of this signal's intrusive subscriber list (`EDGE_NONE` when empty).
98    #[get(pub(crate), type(copy))]
99    #[get_mut(pub(crate))]
100    #[set(pub(crate))]
101    pub(crate) sub_head: u32,
102    /// Tail edge index of this signal's intrusive subscriber list (`EDGE_NONE` when empty).
103    #[get(pub(crate), type(copy))]
104    #[get_mut(pub(crate))]
105    #[set(pub(crate))]
106    pub(crate) sub_tail: u32,
107    /// Active subscriber count for O(1) degree checks.
108    #[get(pub(crate), type(copy))]
109    #[get_mut(pub(crate))]
110    #[set(pub(crate))]
111    pub(crate) sub_count: u32,
112}
113
114/// Storage entry for a lazy memoized derived computation inside the reactive arena.
115#[derive(CustomDebug, Data)]
116pub(crate) struct MemoEntry {
117    /// Generation counter for this slot.
118    #[get(pub(crate), type(copy))]
119    #[get_mut(pub(crate))]
120    #[set(pub(crate))]
121    pub(crate) generation: u32,
122    /// Monotonic version counter incremented when computed value changes.
123    #[get(pub(crate), type(copy))]
124    #[get_mut(pub(crate))]
125    #[set(pub(crate))]
126    pub(crate) version: u64,
127    /// Current push-pull evaluation state.
128    #[get(pub(crate), type(copy))]
129    #[get_mut(pub(crate))]
130    #[set(pub(crate))]
131    pub(crate) state: NodeState,
132    /// Cached 24-byte SBO computed value; empty when uninitialized or freed.
133    #[debug(skip)]
134    #[get(pub(crate))]
135    #[get_mut(pub(crate))]
136    #[set(pub(crate))]
137    pub(crate) value: SignalSlot,
138    /// Type-erased recomputation closure returning `changed`.
139    #[debug(skip)]
140    #[get(skip)]
141    #[get_mut(pub(crate))]
142    #[set(pub(crate))]
143    pub(crate) compute: Option<MemoComputeFn>,
144    /// Head edge index of this memo's intrusive dependency list (`EDGE_NONE` when empty).
145    #[get(pub(crate), type(copy))]
146    #[get_mut(pub(crate))]
147    #[set(pub(crate))]
148    pub(crate) dep_head: u32,
149    /// Tail edge index of this memo's intrusive dependency list (`EDGE_NONE` when empty).
150    #[get(pub(crate), type(copy))]
151    #[get_mut(pub(crate))]
152    #[set(pub(crate))]
153    pub(crate) dep_tail: u32,
154    /// Active upstream dependency count.
155    #[get(pub(crate), type(copy))]
156    #[get_mut(pub(crate))]
157    #[set(pub(crate))]
158    pub(crate) dep_count: u32,
159    /// Monotonic dependency-tracking epoch incremented on every recomputation.
160    #[get(pub(crate), type(copy))]
161    #[get_mut(pub(crate))]
162    #[set(pub(crate))]
163    pub(crate) dep_epoch: u32,
164    /// Whether this memo's dependencies are mirrored in `ReactiveRuntime::edge_index`.
165    #[get(pub(crate), type(copy))]
166    #[get_mut(pub(crate))]
167    #[set(pub(crate))]
168    pub(crate) dep_indexed: bool,
169    /// Head edge index of this memo's intrusive subscriber list (`EDGE_NONE` when empty).
170    #[get(pub(crate), type(copy))]
171    #[get_mut(pub(crate))]
172    #[set(pub(crate))]
173    pub(crate) sub_head: u32,
174    /// Tail edge index of this memo's intrusive subscriber list (`EDGE_NONE` when empty).
175    #[get(pub(crate), type(copy))]
176    #[get_mut(pub(crate))]
177    #[set(pub(crate))]
178    pub(crate) sub_tail: u32,
179    /// Active downstream subscriber count.
180    #[get(pub(crate), type(copy))]
181    #[get_mut(pub(crate))]
182    #[set(pub(crate))]
183    pub(crate) sub_count: u32,
184    /// Topological depth level used to evaluate memos before effects in order.
185    #[get(pub(crate), type(copy))]
186    #[get_mut(pub(crate))]
187    #[set(pub(crate))]
188    pub(crate) depth: u32,
189}
190
191/// Storage entry for a reactive side effect inside the reactive arena.
192#[derive(CustomDebug, Data)]
193pub(crate) struct EffectEntry {
194    /// Generation counter for this slot.
195    #[get(pub(crate), type(copy))]
196    #[get_mut(pub(crate))]
197    #[set(pub(crate))]
198    pub(crate) generation: u32,
199    /// Whether this effect is currently alive.
200    #[get(pub(crate), type(copy))]
201    #[get_mut(pub(crate))]
202    #[set(pub(crate))]
203    pub(crate) alive: bool,
204    /// Whether this effect is queued for execution.
205    #[get(pub(crate), type(copy))]
206    #[get_mut(pub(crate))]
207    #[set(pub(crate))]
208    pub(crate) scheduled: bool,
209    /// Type-erased side-effect closure.
210    #[debug(skip)]
211    #[get(skip)]
212    #[get_mut(pub(crate))]
213    #[set(pub(crate))]
214    pub(crate) callback: Option<Rc<RefCell<dyn FnMut()>>>,
215    /// Head edge index of this effect's intrusive dependency list (`EDGE_NONE` when empty).
216    #[get(pub(crate), type(copy))]
217    #[get_mut(pub(crate))]
218    #[set(pub(crate))]
219    pub(crate) dep_head: u32,
220    /// Tail edge index of this effect's intrusive dependency list (`EDGE_NONE` when empty).
221    #[get(pub(crate), type(copy))]
222    #[get_mut(pub(crate))]
223    #[set(pub(crate))]
224    pub(crate) dep_tail: u32,
225    /// Active upstream dependency count.
226    #[get(pub(crate), type(copy))]
227    #[get_mut(pub(crate))]
228    #[set(pub(crate))]
229    pub(crate) dep_count: u32,
230    /// Monotonic dependency-tracking epoch incremented on every effect run.
231    #[get(pub(crate), type(copy))]
232    #[get_mut(pub(crate))]
233    #[set(pub(crate))]
234    pub(crate) dep_epoch: u32,
235    /// Whether this effect's dependencies are mirrored in `ReactiveRuntime::edge_index`.
236    #[get(pub(crate), type(copy))]
237    #[get_mut(pub(crate))]
238    #[set(pub(crate))]
239    pub(crate) dep_indexed: bool,
240    /// Execution priority depth so parent effects run before nested ones.
241    #[get(pub(crate), type(copy))]
242    #[get_mut(pub(crate))]
243    #[set(pub(crate))]
244    pub(crate) depth: u32,
245    /// Active freeze nesting depth (`> 0` suspends execution while tracking dirty notifications).
246    #[get(pub(crate), type(copy))]
247    #[get_mut(pub(crate))]
248    #[set(pub(crate))]
249    pub(crate) frozen_depth: u32,
250    /// Whether upstream signals or memos dirtied this effect while frozen.
251    #[get(pub(crate), type(copy))]
252    #[get_mut(pub(crate))]
253    #[set(pub(crate))]
254    pub(crate) pending_while_frozen: bool,
255}
256
257/// Ownership scope entry that disposes all owned reactive resources together.
258#[derive(CustomDebug, Data)]
259pub(crate) struct ScopeEntry {
260    /// Generation counter for this scope slot.
261    #[get(pub(crate), type(copy))]
262    #[get_mut(pub(crate))]
263    #[set(pub(crate))]
264    pub(crate) generation: u32,
265    /// Whether this scope is alive.
266    #[get(pub(crate), type(copy))]
267    #[get_mut(pub(crate))]
268    #[set(pub(crate))]
269    pub(crate) alive: bool,
270    /// Whether this scope is currently frozen (`PageRetention::Freeze`).
271    #[get(pub(crate), type(copy))]
272    #[get_mut(pub(crate))]
273    #[set(pub(crate))]
274    pub(crate) frozen: bool,
275    /// Optional parent scope identifier for O(depth) zero-allocation ancestry and freeze queries.
276    #[get(pub(crate), type(copy))]
277    #[get_mut(pub(crate))]
278    #[set(pub(crate))]
279    pub(crate) parent: Option<ScopeId>,
280    /// Signals owned by this scope.
281    #[get(pub(crate))]
282    #[get_mut(pub(crate))]
283    #[set(pub(crate))]
284    pub(crate) signals: Vec<SignalId>,
285    /// Memos owned by this scope.
286    #[get(pub(crate))]
287    #[get_mut(pub(crate))]
288    #[set(pub(crate))]
289    pub(crate) memos: Vec<MemoId>,
290    /// Effects owned by this scope.
291    #[get(pub(crate))]
292    #[get_mut(pub(crate))]
293    #[set(pub(crate))]
294    pub(crate) effects: Vec<EffectId>,
295    /// Child scopes owned by this scope.
296    #[get(pub(crate))]
297    #[get_mut(pub(crate))]
298    #[set(pub(crate))]
299    pub(crate) children: Vec<ScopeId>,
300    /// Cleanup callbacks invoked when this scope is disposed.
301    #[debug(skip)]
302    #[get(skip)]
303    #[get_mut(pub(crate))]
304    #[set(pub(crate))]
305    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
306}
307
308/// Thread-local arena storing all reactive signals, memos, effects, scopes, and O(1) intrusive dependency edges.
309#[derive(CustomDebug, Data)]
310pub struct ReactiveRuntime {
311    /// Signal slot arena.
312    #[get(pub(crate))]
313    #[get_mut(pub(crate))]
314    #[set(pub(crate))]
315    pub(crate) signals: Vec<SignalEntry>,
316    /// Free-list indices for recycled signal slots.
317    #[get(pub(crate))]
318    #[get_mut(pub(crate))]
319    #[set(pub(crate))]
320    pub(crate) free_signals: Vec<u32>,
321    /// Memo slot arena.
322    #[get(pub(crate))]
323    #[get_mut(pub(crate))]
324    #[set(pub(crate))]
325    pub(crate) memos: Vec<MemoEntry>,
326    /// Free-list indices for recycled memo slots.
327    #[get(pub(crate))]
328    #[get_mut(pub(crate))]
329    #[set(pub(crate))]
330    pub(crate) free_memos: Vec<u32>,
331    /// Effect slot arena.
332    #[get(pub(crate))]
333    #[get_mut(pub(crate))]
334    #[set(pub(crate))]
335    pub(crate) effects: Vec<EffectEntry>,
336    /// Free-list indices for recycled effect slots.
337    #[get(pub(crate))]
338    #[get_mut(pub(crate))]
339    #[set(pub(crate))]
340    pub(crate) free_effects: Vec<u32>,
341    /// Scope slot arena.
342    #[get(pub(crate))]
343    #[get_mut(pub(crate))]
344    #[set(pub(crate))]
345    pub(crate) scopes: Vec<ScopeEntry>,
346    /// Free-list indices for recycled scope slots.
347    #[get(pub(crate))]
348    #[get_mut(pub(crate))]
349    #[set(pub(crate))]
350    pub(crate) free_scopes: Vec<u32>,
351    /// Intrusive doubly-linked dependency edge slab enabling O(1) subscribe and unsubscribe.
352    #[get(pub(crate))]
353    #[get_mut(pub(crate))]
354    #[set(pub(crate))]
355    pub(crate) edges: Vec<ReactiveEdge>,
356    /// Free-list indices for recycled edge slab slots.
357    #[get(pub(crate))]
358    #[get_mut(pub(crate))]
359    #[set(pub(crate))]
360    pub(crate) free_edges: Vec<u32>,
361    /// Lazy O(1) edge lookup index populated only for subscribers exceeding `EDGE_INDEX_THRESHOLD` dependencies.
362    #[get(pub(crate))]
363    #[get_mut(pub(crate))]
364    #[set(pub(crate))]
365    pub(crate) edge_index: HashMap<u64, u32>,
366    /// Monotonic counter of total edges linked (diagnostic metric for verifying zero edge thrash).
367    #[get(pub(crate), type(copy))]
368    #[get_mut(pub(crate))]
369    #[set(pub(crate))]
370    pub(crate) edge_alloc_count: u64,
371    /// Stack of currently executing observers and their epoch-tracking frames.
372    #[get(pub(crate))]
373    #[get_mut(pub(crate))]
374    #[set(pub(crate))]
375    pub(crate) observer_stack: Vec<Option<ObserverFrame>>,
376    /// Stack of active ownership scopes.
377    #[get(pub(crate))]
378    #[get_mut(pub(crate))]
379    #[set(pub(crate))]
380    pub(crate) scope_stack: Vec<ScopeId>,
381    /// Pending effects awaiting flush.
382    #[get(pub(crate))]
383    #[get_mut(pub(crate))]
384    #[set(pub(crate))]
385    pub(crate) pending_effects: Vec<EffectId>,
386    /// Reusable scratch queue storing `(packed_sub_tag, target_state)` for zero-allocation dirty propagation.
387    #[get(pub(crate))]
388    #[get_mut(pub(crate))]
389    #[set(pub(crate))]
390    pub(crate) dirty_queue: Vec<(u32, NodeState)>,
391    /// Reusable cursor-sliced scratch stack for zero-allocation `NodeState::Check` verification of >4 memo deps.
392    #[get(pub(crate))]
393    #[get_mut(pub(crate))]
394    #[set(pub(crate))]
395    pub(crate) check_scratch: Vec<MemoId>,
396    /// Reusable scratch buffer holding `(depth, EffectId)` batches across `flush_effects` runs.
397    #[get(pub(crate))]
398    #[get_mut(pub(crate))]
399    #[set(pub(crate))]
400    pub(crate) flush_batch_scratch: Vec<(u32, EffectId)>,
401    /// Reusable DFS stack for zero-allocation scope disposal and freeze/resume traversals.
402    #[get(pub(crate))]
403    #[get_mut(pub(crate))]
404    #[set(pub(crate))]
405    pub(crate) scope_stack_scratch: Vec<ScopeId>,
406    /// Reusable scratch list of effects collected during `dispose_scope_collect`.
407    #[get(pub(crate))]
408    #[get_mut(pub(crate))]
409    #[set(pub(crate))]
410    pub(crate) dispose_effects_scratch: Vec<EffectId>,
411    /// Reusable scratch list of memos collected during `dispose_scope_collect`.
412    #[get(pub(crate))]
413    #[get_mut(pub(crate))]
414    #[set(pub(crate))]
415    pub(crate) dispose_memos_scratch: Vec<MemoId>,
416    /// Reusable scratch list of signals collected during `dispose_scope_collect`.
417    #[get(pub(crate))]
418    #[get_mut(pub(crate))]
419    #[set(pub(crate))]
420    pub(crate) dispose_signals_scratch: Vec<SignalId>,
421    /// Active batch nesting depth; flush executes only when returning to zero.
422    #[get(pub(crate), type(copy))]
423    #[get_mut(pub(crate))]
424    #[set(pub(crate))]
425    pub(crate) batch_depth: u32,
426    /// Guard flag preventing recursive flush loops.
427    #[get(pub(crate), type(copy))]
428    #[get_mut(pub(crate))]
429    #[set(pub(crate))]
430    pub(crate) flushing: bool,
431    /// Global version clock incremented on every reactive write.
432    #[get(pub(crate), type(copy))]
433    #[get_mut(pub(crate))]
434    #[set(pub(crate))]
435    pub(crate) clock: u64,
436    /// Type-keyed root-lifetime global state signal registry.
437    #[get(pub(crate))]
438    #[get_mut(pub(crate))]
439    #[set(pub(crate))]
440    pub(crate) global_states: HashMap<TypeId, SignalId>,
441    /// String-keyed root-lifetime named global state signal registry.
442    #[get(pub(crate))]
443    #[get_mut(pub(crate))]
444    #[set(pub(crate))]
445    pub(crate) named_global_states: HashMap<String, SignalId>,
446    /// Type-keyed cross-component custom event channel signals.
447    #[get(pub(crate))]
448    #[get_mut(pub(crate))]
449    #[set(pub(crate))]
450    pub(crate) event_channels: HashMap<TypeId, SignalId>,
451}
452
453/// Snapshot of runtime diagnostic statistics for telemetry and benchmarking.
454#[derive(Clone, Copy, Debug, Default, Eq, Getter, Hash, New, Ord, PartialEq, PartialOrd)]
455pub struct ReactiveStats {
456    /// Total live signals in the current thread runtime.
457    #[get(type(copy))]
458    pub(crate) live_signals: usize,
459    /// Total live memos in the current thread runtime.
460    #[get(type(copy))]
461    pub(crate) live_memos: usize,
462    /// Total live effects in the current thread runtime.
463    #[get(type(copy))]
464    pub(crate) live_effects: usize,
465    /// Total live ownership scopes in the current thread runtime.
466    #[get(type(copy))]
467    pub(crate) live_scopes: usize,
468    /// Global reactive write clock.
469    #[get(type(copy))]
470    pub(crate) clock: u64,
471}
472
473/// Snapshot of intrusive edge slab and arena capacity metrics for reactive graph diagnostics.
474#[derive(Clone, Copy, Debug, Default, Eq, Getter, Hash, New, Ord, PartialEq, PartialOrd)]
475pub struct ReactiveGraphMetrics {
476    /// Number of currently linked dependency edges in the intrusive slab.
477    #[get(type(copy))]
478    pub(crate) live_edges: usize,
479    /// Monotonic count of edge allocations since runtime initialization.
480    #[get(type(copy))]
481    pub(crate) edge_alloc_count: u64,
482    /// Allocated capacity of the signal arena vector.
483    #[get(type(copy))]
484    pub(crate) signals_capacity: usize,
485    /// Allocated capacity of the memo arena vector.
486    #[get(type(copy))]
487    pub(crate) memos_capacity: usize,
488    /// Allocated capacity of the effect arena vector.
489    #[get(type(copy))]
490    pub(crate) effects_capacity: usize,
491    /// Allocated capacity of the scope arena vector.
492    #[get(type(copy))]
493    pub(crate) scopes_capacity: usize,
494}
495
496/// Panic-safe RAII guard that pops an active tracking frame from `observer_stack` on unwind.
497#[derive(Debug)]
498pub(crate) struct TrackingGuard {
499    /// Remains `true` until normal completion disarms the guard before committing.
500    pub(crate) active: bool,
501}
502
503/// Panic-safe RAII guard tracking nested shared `RefCell::try_borrow()` depth and flushing deferred reads on exit.
504#[derive(Debug)]
505pub(crate) struct RefBorrowGuard {
506    /// Nesting depth prior to entering this shared read borrow.
507    pub(crate) prev_depth: u32,
508}
509
510/// Panic-safe RAII guard temporarily suspending dependency tracking inside `untracked(...)`.
511#[derive(Debug)]
512pub(crate) struct UntrackedGuard {
513    /// Previous `TRACKING_ACTIVE` flag restored on drop.
514    pub(crate) prev_active: bool,
515    /// Whether a `None` sentinel frame was pushed onto `observer_stack`.
516    pub(crate) pushed_frame: bool,
517}