Skip to main content

cranpose_core/
lib.rs

1#![doc = r"Core runtime pieces for the Cranpose experiment."]
2#![deny(unsafe_code)]
3
4pub extern crate self as cranpose_core;
5
6mod callbacks;
7mod composer;
8pub mod composer_context;
9mod composition;
10mod composition_locals;
11mod debug_trace;
12mod emit;
13#[cfg(any(feature = "internal", test))]
14mod frame_clock;
15mod hooks;
16mod launched_effect;
17pub mod owned;
18pub mod platform;
19mod recompose;
20mod retention;
21pub mod runtime;
22mod slot;
23pub mod snapshot_double_index_heap;
24pub mod snapshot_id_set;
25pub mod snapshot_pinning;
26pub mod snapshot_state_observer;
27pub mod snapshot_v2;
28mod snapshot_weak_set;
29mod state;
30pub mod subcompose;
31
32#[cfg(feature = "internal")]
33#[doc(hidden)]
34pub mod internal {
35    pub use crate::frame_clock::{FrameCallbackRegistration, FrameClock};
36}
37pub use callbacks::{CallbackHolder, CallbackHolder1, ParamSlot, ParamState, ReturnSlot};
38pub use composer::{Composer, ValueSlotHandle};
39pub(crate) use composer::{ComposerCore, EmittedNode, ParentAttachMode, ParentFrame};
40pub use composition::{Composition, ROOT_RENDER_REPLAY_LIMIT};
41pub use composition_locals::{
42    compositionLocalOf, compositionLocalOfWithPolicy, staticCompositionLocalOf, CompositionLocal,
43    CompositionLocalProvider, ProvidedValue, StaticCompositionLocal,
44};
45pub(crate) use composition_locals::{LocalStateEntry, StaticLocalEntry};
46#[doc(hidden)]
47pub use debug_trace::{
48    debug_label_current_scope, debug_live_recompose_scope_count,
49    debug_recompose_scope_registry_stats, debug_scope_invalidation_sources, debug_scope_label,
50};
51pub use hooks::{
52    derivedStateOf, mutableStateList, mutableStateListOf, mutableStateMap, mutableStateMapOf,
53    mutableStateOf, ownedMutableStateOf, remember, rememberUpdatedState, try_mutableStateOf,
54    useState,
55};
56#[cfg(feature = "internal")]
57#[doc(hidden)]
58pub use hooks::{withFrameMillis, withFrameNanos};
59pub use launched_effect::{
60    __launched_effect_async_impl, __launched_effect_impl, CancelToken, LaunchedEffectScope,
61};
62pub use owned::Owned;
63pub use platform::{Clock, RuntimeScheduler};
64pub use retention::{RetentionBudget, RetentionEvictionPolicy, RetentionMode, RetentionPolicy};
65#[doc(hidden)]
66pub use runtime::{
67    current_runtime_handle, schedule_frame, schedule_node_update, DefaultScheduler, Runtime,
68    RuntimeHandle, StateId, TaskHandle,
69};
70pub use slot::{
71    SlotDebugAnchor, SlotDebugEntry, SlotDebugEntryKind, SlotDebugGroup, SlotDebugScope,
72    SlotDebugSnapshot, SlotRetentionDebugStats, SlotTable, SlotTableDebugStats,
73    SlotTableLocalDebugStats, SlotTableMutationDebugStats,
74};
75#[doc(hidden)]
76pub use snapshot_state_observer::SnapshotStateObserver;
77
78/// Runs the provided closure inside a mutable snapshot and applies the result.
79///
80/// Use this function when you need to update `MutableState` from outside the
81/// composition or layout phase, typically in event handlers or async tasks.
82///
83/// # Why is this needed?
84/// Cranpose uses a snapshot system (MVCC) to isolate state changes. Modifications
85/// made to `MutableState` are only visible to the current thread's active snapshot.
86/// To make changes visible to the rest of the system (and trigger recomposition),
87/// they must be "applied" by committing the snapshot. This helper handles that
88/// lifecycle for you.
89///
90/// # Example
91///
92/// ```ignore
93/// // Inside a button click handler
94/// run_in_mutable_snapshot(|| {
95///     count.set(count.value() + 1);
96/// });
97/// ```
98///
99/// # Important
100/// ALL UI event handlers (keyboard, mouse, touch, animations) that modify state
101/// MUST use this function or [`dispatch_ui_event`].
102pub fn run_in_mutable_snapshot<T>(block: impl FnOnce() -> T) -> Result<T, &'static str> {
103    let snapshot = snapshot_v2::take_mutable_snapshot(None, None);
104
105    let _applied_guard = AppliedSnapshotFlagGuard::enter();
106    let value = snapshot.enter(block);
107
108    match snapshot.apply() {
109        snapshot_v2::SnapshotApplyResult::Success => Ok(value),
110        snapshot_v2::SnapshotApplyResult::Failure => Err("Snapshot apply failed"),
111    }
112}
113
114struct AppliedSnapshotFlagGuard {
115    previous: bool,
116}
117
118impl AppliedSnapshotFlagGuard {
119    fn enter() -> Self {
120        let previous = IN_APPLIED_SNAPSHOT.with(|flag| {
121            let previous = flag.get();
122            flag.set(true);
123            previous
124        });
125        Self { previous }
126    }
127}
128
129impl Drop for AppliedSnapshotFlagGuard {
130    fn drop(&mut self) {
131        IN_APPLIED_SNAPSHOT.with(|flag| flag.set(self.previous));
132    }
133}
134
135/// Dispatches a UI event in a proper snapshot context.
136///
137/// This is a convenience wrapper around [`run_in_mutable_snapshot`] that returns
138/// `Option<T>` instead of `Result<T, &str>`.
139///
140/// # Example
141/// ```ignore
142/// // In a keyboard event handler:
143/// dispatch_ui_event(|| {
144///     text_field_state.edit(|buffer| {
145///         buffer.insert("a");
146///     });
147/// });
148/// ```
149pub fn dispatch_ui_event<T>(block: impl FnOnce() -> T) -> Option<T> {
150    run_in_mutable_snapshot(block).ok()
151}
152
153// ─── Event Handler Context Tracking ─────────────────────────────────────────
154//
155// These thread-locals track whether code is running in an event handler context
156// and whether it's properly wrapped in run_in_mutable_snapshot. This allows
157// debug-mode warnings when state is modified without proper snapshot handling.
158
159thread_local! {
160    /// Tracks if we're in a UI event handler context (keyboard, mouse, etc.)
161    pub(crate) static IN_EVENT_HANDLER: Cell<bool> = const { Cell::new(false) };
162    /// Tracks if we're in a properly-applied mutable snapshot
163    pub(crate) static IN_APPLIED_SNAPSHOT: Cell<bool> = const { Cell::new(false) };
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
167pub struct CompositionPassDebugStats {
168    pub commands_len: usize,
169    pub commands_cap: usize,
170    pub command_payload_len_bytes: usize,
171    pub command_payload_cap_bytes: usize,
172    pub sync_children_len: usize,
173    pub sync_children_cap: usize,
174    pub sync_child_ids_len: usize,
175    pub sync_child_ids_cap: usize,
176    pub side_effects_len: usize,
177    pub side_effects_cap: usize,
178}
179
180#[must_use]
181pub struct EventHandlerScopeGuard {
182    previous: bool,
183}
184
185impl Drop for EventHandlerScopeGuard {
186    fn drop(&mut self) {
187        IN_EVENT_HANDLER.with(|flag| flag.set(self.previous));
188    }
189}
190
191pub fn enter_event_handler_scope() -> EventHandlerScopeGuard {
192    let previous = IN_EVENT_HANDLER.with(|flag| {
193        let previous = flag.get();
194        flag.set(true);
195        previous
196    });
197    EventHandlerScopeGuard { previous }
198}
199
200/// Returns true if currently in an event handler context.
201pub fn in_event_handler() -> bool {
202    IN_EVENT_HANDLER.with(|c| c.get())
203}
204
205/// Returns true if currently in an applied snapshot context.
206pub fn in_applied_snapshot() -> bool {
207    IN_APPLIED_SNAPSHOT.with(|c| c.get())
208}
209
210#[cfg(test)]
211pub use runtime::{TestRuntime, TestScheduler};
212
213use crate::collections::map::{HashMap, HashSet};
214use smallvec::SmallVec;
215use std::any::{Any, TypeId};
216use std::cell::{Cell, Ref, RefCell, RefMut};
217use std::cmp::Reverse;
218use std::collections::BinaryHeap;
219use std::hash::{Hash, Hasher};
220use std::ops::{Deref, DerefMut};
221use std::rc::{Rc, Weak};
222
223pub type Key = u64;
224pub type NodeId = usize;
225
226#[cfg(any(test, debug_assertions))]
227#[derive(Clone, Debug, PartialEq, Eq)]
228struct LocationKeyDebugInfo {
229    file: String,
230    line: u32,
231    column: u32,
232}
233
234#[cfg(any(test, debug_assertions))]
235thread_local! {
236    static LOCATION_KEY_REGISTRY: RefCell<HashMap<Key, LocationKeyDebugInfo>> =
237        RefCell::new(HashMap::default());
238    static LOCATION_KEY_COLLISION_COUNT: Cell<usize> = const { Cell::new(0) };
239}
240
241#[cfg(any(test, debug_assertions))]
242fn register_location_key_debug_info(key: Key, file: &str, line: u32, column: u32) {
243    let info = LocationKeyDebugInfo {
244        file: file.to_owned(),
245        line,
246        column,
247    };
248    let collision = LOCATION_KEY_REGISTRY.with(|registry| {
249        let mut registry = registry.borrow_mut();
250        match registry.entry(key) {
251            std::collections::hash_map::Entry::Vacant(entry) => {
252                entry.insert(info);
253                None
254            }
255            std::collections::hash_map::Entry::Occupied(entry) => {
256                let existing = entry.get();
257                (existing != &info).then(|| (existing.clone(), info))
258            }
259        }
260    });
261    if let Some((existing, incoming)) = collision {
262        LOCATION_KEY_COLLISION_COUNT.with(|count| {
263            count.set(count.get().saturating_add(1));
264        });
265        log::error!("location key collision: key={key} first={existing:?} second={incoming:?}");
266    }
267}
268
269#[cfg(all(debug_assertions, not(test)))]
270fn location_key_diagnostics_enabled() -> bool {
271    std::env::var_os("CRANPOSE_LOCATION_KEY_DIAGNOSTICS").is_some()
272}
273
274#[cfg(test)]
275pub(crate) fn register_location_key_debug_info_for_test(
276    key: Key,
277    file: &str,
278    line: u32,
279    column: u32,
280) {
281    register_location_key_debug_info(key, file, line, column);
282}
283
284#[cfg(test)]
285pub(crate) fn location_key_debug_collision_count_for_test() -> usize {
286    LOCATION_KEY_COLLISION_COUNT.with(Cell::get)
287}
288
289#[cfg(test)]
290pub(crate) fn location_key_debug_info_for_test(key: Key) -> Option<LocationKeyDebugInfo> {
291    LOCATION_KEY_REGISTRY.with(|registry| registry.borrow().get(&key).cloned())
292}
293
294#[cfg(test)]
295pub(crate) fn slot_validation_diagnostics_enabled() -> bool {
296    true
297}
298
299#[cfg(all(debug_assertions, not(test)))]
300pub(crate) fn slot_validation_diagnostics_enabled() -> bool {
301    std::env::var_os("CRANPOSE_VALIDATE_SLOTS").is_some()
302}
303
304fn source_location_key(file: &str, line: u32, column: u32) -> Key {
305    let mut hash = 0xcbf2_9ce4_8422_2325u64;
306    hash = fnv1a_location_key_bytes(hash, file.as_bytes());
307    hash = fnv1a_location_key_bytes(hash, &[0xff]);
308    hash = fnv1a_location_key_bytes(hash, &line.to_le_bytes());
309    hash = fnv1a_location_key_bytes(hash, &[0xfe]);
310    hash = fnv1a_location_key_bytes(hash, &column.to_le_bytes());
311    avalanche_location_key(hash)
312}
313
314fn fnv1a_location_key_bytes(mut hash: u64, bytes: &[u8]) -> u64 {
315    for byte in bytes {
316        hash ^= u64::from(*byte);
317        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
318    }
319    hash
320}
321
322fn avalanche_location_key(mut value: u64) -> u64 {
323    value ^= value >> 33;
324    value = value.wrapping_mul(0xff51_afd7_ed55_8ccd);
325    value ^= value >> 33;
326    value = value.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
327    value ^ (value >> 33)
328}
329
330pub fn location_key(file: &str, line: u32, column: u32) -> Key {
331    let key = source_location_key(file, line, column);
332    #[cfg(test)]
333    register_location_key_debug_info(key, file, line, column);
334    #[cfg(all(debug_assertions, not(test)))]
335    if location_key_diagnostics_enabled() {
336        register_location_key_debug_info(key, file, line, column);
337    }
338    key
339}
340
341/// Stable identifier for a slot in the slot table.
342///
343/// Anchors provide positional stability: they maintain their identity even when
344/// the slot table is reorganized (e.g., during conditional rendering or group moves).
345/// This prevents effect states from being prematurely removed during recomposition.
346#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default)]
347pub struct AnchorId {
348    id: u32,
349    generation: u32,
350}
351
352impl AnchorId {
353    /// Invalid anchor that represents no anchor.
354    pub(crate) const INVALID: AnchorId = AnchorId {
355        id: 0,
356        generation: 0,
357    };
358
359    pub(crate) fn new(id: usize) -> Self {
360        Self {
361            id: crate::slot::checked_usize_to_u32(id, "anchor id"),
362            generation: 1,
363        }
364    }
365
366    /// Check if this anchor is valid (non-zero).
367    pub fn is_valid(&self) -> bool {
368        self.id != 0
369    }
370}
371
372pub(crate) type ScopeId = usize;
373pub(crate) type FrameCallbackId = u64;
374type LocalStackSnapshot = Rc<Vec<composer::LocalContext>>;
375
376#[derive(Clone)]
377pub(crate) struct LocalKey(Rc<()>);
378
379impl LocalKey {
380    fn new() -> Self {
381        Self(Rc::new(()))
382    }
383}
384
385impl std::fmt::Debug for LocalKey {
386    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
387        f.debug_tuple("LocalKey")
388            .field(&(Rc::as_ptr(&self.0) as usize))
389            .finish()
390    }
391}
392
393impl std::fmt::Display for LocalKey {
394    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
395        write!(f, "{:p}", Rc::as_ptr(&self.0))
396    }
397}
398
399impl PartialEq for LocalKey {
400    fn eq(&self, other: &Self) -> bool {
401        Rc::ptr_eq(&self.0, &other.0)
402    }
403}
404
405impl Eq for LocalKey {}
406
407impl Hash for LocalKey {
408    fn hash<H: Hasher>(&self, state: &mut H) {
409        Rc::as_ptr(&self.0).hash(state);
410    }
411}
412
413thread_local! {
414    static EMPTY_LOCAL_STACK: LocalStackSnapshot = Rc::new(Vec::new());
415    #[cfg(debug_assertions)]
416    static DEBUG_SCOPE_LABELS: RefCell<HashMap<usize, &'static str>> = RefCell::new(HashMap::default());
417    #[cfg(debug_assertions)]
418    static DEBUG_SCOPE_INVALIDATION_SOURCES: RefCell<HashMap<usize, HashSet<String>>> =
419        RefCell::new(HashMap::default());
420    #[cfg(all(test, debug_assertions))]
421    static DEBUG_SCOPE_TRACKING_OVERRIDE: Cell<Option<bool>> = const { Cell::new(None) };
422}
423
424fn empty_local_stack() -> LocalStackSnapshot {
425    EMPTY_LOCAL_STACK.with(Rc::clone)
426}
427
428enum RecomposeCallback {
429    Static(fn(&Composer)),
430    Dynamic(Box<dyn FnMut(&Composer) + 'static>),
431}
432
433pub(crate) struct RecomposeScopeInner {
434    runtime: RuntimeHandle,
435    invalid: Cell<bool>,
436    enqueued: Cell<bool>,
437    active: Cell<bool>,
438    composed_once: Cell<bool>,
439    pending_recompose: Cell<bool>,
440    force_reuse: Cell<bool>,
441    force_recompose: Cell<bool>,
442    retention_mode: Cell<RetentionMode>,
443    parent_hint: Cell<Option<NodeId>>,
444    recompose: RefCell<Option<RecomposeCallback>>,
445    parent_scope: RefCell<Option<Weak<RecomposeScopeInner>>>,
446    local_stack: RefCell<LocalStackSnapshot>,
447    slots_storage_key: Cell<usize>,
448    slots_runtime_state: RefCell<Option<std::rc::Weak<crate::composer::ComposerRuntimeState>>>,
449    state_subscriptions: RefCell<HashSet<StateId>>,
450    invalidation_sources: RefCell<Option<HashSet<StateId>>>,
451}
452
453impl RecomposeScopeInner {
454    fn new(runtime: RuntimeHandle) -> Self {
455        runtime.increment_live_recompose_scope_count();
456        Self {
457            runtime,
458            invalid: Cell::new(false),
459            enqueued: Cell::new(false),
460            active: Cell::new(true),
461            composed_once: Cell::new(false),
462            pending_recompose: Cell::new(false),
463            force_reuse: Cell::new(false),
464            force_recompose: Cell::new(false),
465            retention_mode: Cell::new(RetentionMode::DisposeWhenInactive),
466            parent_hint: Cell::new(None),
467            recompose: RefCell::new(None),
468            parent_scope: RefCell::new(None),
469            local_stack: RefCell::new(empty_local_stack()),
470            slots_storage_key: Cell::new(0),
471            slots_runtime_state: RefCell::new(None),
472            state_subscriptions: RefCell::new(HashSet::default()),
473            invalidation_sources: RefCell::new(Some(HashSet::default())),
474        }
475    }
476
477    fn id(&self) -> ScopeId {
478        std::ptr::from_ref(self).addr()
479    }
480}
481
482impl Drop for RecomposeScopeInner {
483    fn drop(&mut self) {
484        let id = self.id();
485        self.runtime.decrement_live_recompose_scope_count();
486        let subscriptions = std::mem::take(self.state_subscriptions.get_mut());
487        for state_id in subscriptions {
488            self.runtime.unregister_state_scope(state_id, id);
489        }
490        #[cfg(debug_assertions)]
491        {
492            let _ = DEBUG_SCOPE_LABELS.try_with(|labels| {
493                labels.borrow_mut().remove(&id);
494            });
495            let _ = DEBUG_SCOPE_INVALIDATION_SOURCES.try_with(|sources| {
496                sources.borrow_mut().remove(&id);
497            });
498        }
499        if self.enqueued.replace(false) {
500            self.runtime.mark_scope_recomposed(id);
501        }
502    }
503}
504
505#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
506pub struct RecomposeScopeRegistryDebugStats {
507    pub len: usize,
508    pub capacity: usize,
509}
510
511#[derive(Clone)]
512pub struct RecomposeScope {
513    inner: Rc<RecomposeScopeInner>,
514}
515
516impl PartialEq for RecomposeScope {
517    fn eq(&self, other: &Self) -> bool {
518        Rc::ptr_eq(&self.inner, &other.inner)
519    }
520}
521
522impl Eq for RecomposeScope {}
523
524impl Hash for RecomposeScope {
525    fn hash<H: Hasher>(&self, state: &mut H) {
526        self.id().hash(state);
527    }
528}
529
530impl RecomposeScope {
531    fn new(runtime: RuntimeHandle) -> Self {
532        Self {
533            inner: Rc::new(RecomposeScopeInner::new(runtime)),
534        }
535    }
536
537    pub(crate) fn downgrade(&self) -> Weak<RecomposeScopeInner> {
538        Rc::downgrade(&self.inner)
539    }
540
541    pub fn id(&self) -> ScopeId {
542        self.inner.id()
543    }
544
545    pub fn is_invalid(&self) -> bool {
546        self.inner.invalid.get()
547    }
548
549    pub fn is_active(&self) -> bool {
550        self.inner.active.get()
551    }
552
553    fn record_state_subscription(&self, state_id: StateId) {
554        self.inner.state_subscriptions.borrow_mut().insert(state_id);
555    }
556
557    fn record_unknown_invalidation_source(&self) {
558        *self.inner.invalidation_sources.borrow_mut() = None;
559    }
560
561    fn record_state_invalidation_source(&self, state_id: StateId) {
562        let mut sources = self.inner.invalidation_sources.borrow_mut();
563        if let Some(source_set) = sources.as_mut() {
564            source_set.insert(state_id);
565        }
566    }
567
568    fn enqueue_invalidation(&self) {
569        self.inner.invalid.set(true);
570        if !self.inner.active.get() {
571            return;
572        }
573        if !self.inner.enqueued.replace(true) {
574            self.inner
575                .runtime
576                .register_invalid_scope(self.id(), self.downgrade());
577        }
578    }
579
580    fn invalidate(&self) {
581        self.record_unknown_invalidation_source();
582        self.enqueue_invalidation();
583    }
584
585    pub(crate) fn invalidate_from_state(&self, state_id: StateId) {
586        self.record_state_invalidation_source(state_id);
587        self.enqueue_invalidation();
588    }
589
590    fn mark_recomposed(&self) {
591        self.inner.invalid.set(false);
592        self.inner.force_reuse.set(false);
593        self.inner.force_recompose.set(false);
594        self.inner
595            .invalidation_sources
596            .borrow_mut()
597            .replace(HashSet::default());
598        if self.inner.enqueued.replace(false) {
599            self.inner.runtime.mark_scope_recomposed(self.id());
600        }
601        let pending = self.inner.pending_recompose.replace(false);
602        if pending {
603            if self.inner.active.get() {
604                self.invalidate();
605            } else {
606                self.inner.invalid.set(true);
607            }
608        }
609    }
610
611    fn set_recompose(&self, callback: Box<dyn FnMut(&Composer) + 'static>) {
612        *self.inner.recompose.borrow_mut() = Some(RecomposeCallback::Dynamic(callback));
613    }
614
615    fn set_recompose_fn(&self, callback: fn(&Composer)) {
616        *self.inner.recompose.borrow_mut() = Some(RecomposeCallback::Static(callback));
617    }
618
619    /// Returns true if a callback was found and executed, false if no callback was registered.
620    fn run_recompose(&self, composer: &Composer) -> bool {
621        let callback = self.inner.recompose.borrow_mut().take();
622        if let Some(callback) = callback {
623            match callback {
624                RecomposeCallback::Static(callback) => callback(composer),
625                RecomposeCallback::Dynamic(mut callback) => callback(composer),
626            }
627            true
628        } else {
629            false
630        }
631    }
632
633    fn has_recompose_callback(&self) -> bool {
634        self.inner.recompose.borrow().is_some()
635    }
636
637    fn snapshot_locals(&self, stack: LocalStackSnapshot) {
638        *self.inner.local_stack.borrow_mut() = stack;
639    }
640
641    fn local_stack(&self) -> LocalStackSnapshot {
642        self.inner.local_stack.borrow().clone()
643    }
644
645    fn set_parent_hint(&self, parent: Option<NodeId>) {
646        self.inner.parent_hint.set(parent);
647    }
648
649    fn set_parent_scope(&self, parent: Option<RecomposeScope>) {
650        *self.inner.parent_scope.borrow_mut() = parent.map(|scope| scope.downgrade());
651    }
652
653    fn parent_scope(&self) -> Option<RecomposeScope> {
654        self.inner
655            .parent_scope
656            .borrow()
657            .as_ref()
658            .and_then(Weak::upgrade)
659            .map(|inner| RecomposeScope { inner })
660    }
661
662    fn callback_promotion_target(&self) -> Option<RecomposeScope> {
663        let mut current = self.parent_scope();
664        while let Some(scope) = current {
665            if scope.has_recompose_callback() {
666                return Some(scope);
667            }
668            current = scope.parent_scope();
669        }
670        None
671    }
672
673    fn parent_hint(&self) -> Option<NodeId> {
674        self.inner.parent_hint.get()
675    }
676
677    fn set_slots_host(&self, host: &Rc<SlotsHost>) {
678        self.inner.slots_storage_key.set(host.storage_key());
679        *self.inner.slots_runtime_state.borrow_mut() =
680            host.runtime_state().map(|state| Rc::downgrade(&state));
681    }
682
683    pub(crate) fn slots_storage_key(&self) -> Option<usize> {
684        let key = self.inner.slots_storage_key.get();
685        (key != 0).then_some(key)
686    }
687
688    pub(crate) fn slots_runtime_state(&self) -> Option<Rc<crate::composer::ComposerRuntimeState>> {
689        self.inner
690            .slots_runtime_state
691            .borrow()
692            .as_ref()
693            .and_then(std::rc::Weak::upgrade)
694    }
695
696    pub fn deactivate(&self) {
697        if !self.inner.active.replace(false) {
698            return;
699        }
700        if self.inner.enqueued.replace(false) {
701            self.inner.runtime.mark_scope_recomposed(self.id());
702        }
703    }
704
705    pub fn reactivate(&self) {
706        if self.inner.active.replace(true) {
707            return;
708        }
709        if self.inner.invalid.get() && !self.inner.enqueued.replace(true) {
710            self.inner
711                .runtime
712                .register_invalid_scope(self.id(), self.downgrade());
713        }
714    }
715
716    pub fn force_reuse(&self) {
717        self.inner.force_reuse.set(true);
718        self.inner.force_recompose.set(false);
719        self.inner.pending_recompose.set(true);
720    }
721
722    pub fn force_recompose(&self) {
723        self.inner.force_recompose.set(true);
724        self.inner.force_reuse.set(false);
725        self.inner.pending_recompose.set(false);
726    }
727
728    pub(crate) fn set_retention_mode(&self, mode: RetentionMode) {
729        self.inner.retention_mode.set(mode);
730    }
731
732    pub(crate) fn retention_mode(&self) -> RetentionMode {
733        self.inner.retention_mode.get()
734    }
735
736    pub fn should_recompose(&self) -> bool {
737        if self.inner.force_recompose.replace(false) {
738            self.inner.force_reuse.set(false);
739            return true;
740        }
741        if self.inner.force_reuse.replace(false) {
742            return false;
743        }
744        self.is_invalid()
745    }
746
747    pub fn has_composed_once(&self) -> bool {
748        self.inner.composed_once.get()
749    }
750
751    fn mark_composed_once(&self) {
752        self.inner.composed_once.set(true);
753    }
754
755    fn invalidated_only_by(&self, allowed_sources: &HashSet<StateId>) -> Option<bool> {
756        let sources = self.inner.invalidation_sources.borrow();
757        let sources = sources.as_ref()?;
758        if sources.is_empty() {
759            return None;
760        }
761        Some(
762            sources
763                .iter()
764                .all(|source| allowed_sources.contains(source)),
765        )
766    }
767
768    fn has_unknown_invalidation_source(&self) -> bool {
769        self.inner.invalidation_sources.borrow().is_none()
770    }
771}
772
773#[cfg(test)]
774impl RecomposeScope {
775    pub(crate) fn new_for_test(runtime: RuntimeHandle) -> Self {
776        Self::new(runtime)
777    }
778}
779
780#[derive(Debug, Clone, Copy, Default)]
781pub struct RecomposeOptions {
782    pub force_reuse: bool,
783    pub force_recompose: bool,
784    pub retention: RetentionMode,
785}
786
787#[derive(Debug, Clone, PartialEq, Eq)]
788pub enum NodeError {
789    Missing {
790        id: NodeId,
791    },
792    TypeMismatch {
793        id: NodeId,
794        expected: &'static str,
795    },
796    MissingContext {
797        id: NodeId,
798        reason: &'static str,
799    },
800    AlreadyExists {
801        id: NodeId,
802    },
803    MalformedCommandPayload {
804        tag: &'static str,
805    },
806    SlotHostUnavailable {
807        operation: &'static str,
808        reason: &'static str,
809    },
810    RecompositionLimitExceeded {
811        operation: &'static str,
812        limit: usize,
813    },
814}
815
816impl std::fmt::Display for NodeError {
817    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
818        match self {
819            NodeError::Missing { id } => write!(f, "node {id} missing"),
820            NodeError::TypeMismatch { id, expected } => {
821                write!(f, "node {id} type mismatch; expected {expected}")
822            }
823            NodeError::MissingContext { id, reason } => {
824                write!(f, "missing context for node {id}: {reason}")
825            }
826            NodeError::AlreadyExists { id } => {
827                write!(f, "node {id} already exists")
828            }
829            NodeError::MalformedCommandPayload { tag } => {
830                write!(f, "command queue missing or invalid {tag} payload")
831            }
832            NodeError::SlotHostUnavailable { operation, reason } => {
833                write!(f, "{operation} cannot access slot host: {reason}")
834            }
835            NodeError::RecompositionLimitExceeded { operation, limit } => {
836                write!(
837                    f,
838                    "{operation} exceeded {limit} iterations while reconciling composition"
839                )
840            }
841        }
842    }
843}
844
845impl std::error::Error for NodeError {}
846
847pub use subcompose::{
848    ContentTypeReusePolicy, DefaultSlotReusePolicy, SlotId, SlotReusePolicy, SubcomposeState,
849};
850
851#[derive(Copy, Clone, Debug, PartialEq, Eq)]
852pub enum Phase {
853    Compose,
854    Measure,
855    Layout,
856}
857
858pub use composer_context::with_composer as with_current_composer;
859
860#[allow(non_snake_case)]
861pub fn withCurrentComposer<R>(f: impl FnOnce(&Composer) -> R) -> R {
862    composer_context::with_composer(f)
863}
864
865fn with_current_composer_opt<R>(f: impl FnOnce(&Composer) -> R) -> Option<R> {
866    composer_context::try_with_composer(f)
867}
868
869#[doc(hidden)]
870pub fn current_recompose_scope_invalidated_only_by(
871    allowed_sources: impl IntoIterator<Item = StateId>,
872) -> Option<bool> {
873    with_current_composer_opt(|composer| {
874        let allowed_sources = allowed_sources.into_iter().collect();
875        let mut scope = composer.current_recranpose_scope();
876        let mut saw_unknown_source = false;
877        while let Some(current) = scope {
878            if current.has_unknown_invalidation_source() {
879                saw_unknown_source = true;
880                scope = current.parent_scope();
881                continue;
882            }
883            if let Some(matches) = current.invalidated_only_by(&allowed_sources) {
884                return Some(matches);
885            }
886            scope = current.parent_scope();
887        }
888        saw_unknown_source.then_some(false)
889    })
890    .flatten()
891}
892
893#[track_caller]
894pub fn with_key<K: Hash>(key: &K, content: impl FnOnce()) {
895    let seed = explicit_group_key_seed(key, std::panic::Location::caller());
896    with_current_composer(|composer| composer.with_group_seed(seed, |_| content()));
897}
898
899#[allow(non_snake_case)]
900pub fn withKey<K: Hash>(key: &K, content: impl FnOnce()) {
901    with_key(key, content)
902}
903
904#[derive(Default)]
905struct DisposableEffectState {
906    key: Option<Key>,
907    cleanup: Option<Box<dyn FnOnce()>>,
908}
909
910impl DisposableEffectState {
911    fn should_run(&self, key: Key) -> bool {
912        match self.key {
913            Some(current) => current != key,
914            None => true,
915        }
916    }
917
918    fn set_key(&mut self, key: Key) {
919        self.key = Some(key);
920    }
921
922    fn set_cleanup(&mut self, cleanup: Option<Box<dyn FnOnce()>>) {
923        self.cleanup = cleanup;
924    }
925
926    fn run_cleanup(&mut self) {
927        if let Some(cleanup) = self.cleanup.take() {
928            cleanup();
929        }
930    }
931}
932
933impl Drop for DisposableEffectState {
934    fn drop(&mut self) {
935        self.run_cleanup();
936    }
937}
938
939#[derive(Clone, Copy, Debug, Default)]
940pub struct DisposableEffectScope;
941
942#[derive(Default)]
943pub struct DisposableEffectResult {
944    cleanup: Option<Box<dyn FnOnce()>>,
945}
946
947impl DisposableEffectScope {
948    pub fn on_dispose(&self, cleanup: impl FnOnce() + 'static) -> DisposableEffectResult {
949        DisposableEffectResult::new(cleanup)
950    }
951}
952
953impl DisposableEffectResult {
954    pub fn new(cleanup: impl FnOnce() + 'static) -> Self {
955        Self {
956            cleanup: Some(Box::new(cleanup)),
957        }
958    }
959
960    fn into_cleanup(self) -> Option<Box<dyn FnOnce()>> {
961        self.cleanup
962    }
963}
964
965#[allow(non_snake_case)]
966pub fn SideEffect(effect: impl FnOnce() + 'static) {
967    with_current_composer(|composer| composer.register_side_effect(effect));
968}
969
970pub fn __disposable_effect_impl<K, F>(group_key: Key, keys: K, effect: F)
971where
972    K: Hash,
973    F: FnOnce(DisposableEffectScope) -> DisposableEffectResult + 'static,
974{
975    // Create a group using the caller's location to ensure each DisposableEffect
976    // gets its own slot table entry, even in conditional branches
977    with_current_composer(|composer| {
978        composer.with_group(group_key, |composer| {
979            let key_hash = hash_key(&keys);
980            let state = composer.remember_effect(DisposableEffectState::default);
981            if state.with(|state| state.should_run(key_hash)) {
982                state.update(|state| {
983                    state.run_cleanup();
984                    state.set_key(key_hash);
985                });
986                let state_for_effect = state.clone();
987                let mut effect_opt = Some(effect);
988                composer.register_side_effect(move || {
989                    if let Some(effect) = effect_opt.take() {
990                        let result = effect(DisposableEffectScope);
991                        state_for_effect.update(|state| state.set_cleanup(result.into_cleanup()));
992                    }
993                });
994            }
995        });
996    });
997}
998
999#[macro_export]
1000macro_rules! DisposableEffect {
1001    ($keys:expr, $effect:expr) => {
1002        $crate::__disposable_effect_impl(
1003            $crate::location_key(file!(), line!(), column!()),
1004            $keys,
1005            $effect,
1006        )
1007    };
1008}
1009
1010#[macro_export]
1011macro_rules! clone_captures {
1012    ($($alias:ident $(= $value:expr)?),+ $(,)?; $body:expr) => {{
1013        $(let $alias = $crate::clone_captures!(@clone $alias $(= $value)?);)+
1014        $body
1015    }};
1016    (@clone $alias:ident = $value:expr) => {
1017        ($value).clone()
1018    };
1019    (@clone $alias:ident) => {
1020        $alias.clone()
1021    };
1022}
1023
1024pub fn with_node_mut<N: Node + 'static, R>(
1025    id: NodeId,
1026    f: impl FnOnce(&mut N) -> R,
1027) -> Result<R, NodeError> {
1028    with_current_composer(|composer| composer.with_node_mut(id, f))
1029}
1030
1031pub fn push_parent(id: NodeId) {
1032    with_current_composer(|composer| composer.push_parent(id));
1033}
1034
1035pub fn pop_parent() {
1036    with_current_composer(|composer| composer.pop_parent());
1037}
1038
1039// ═══════════════════════════════════════════════════════════════════════════
1040// Public slot storage helper types
1041// ═══════════════════════════════════════════════════════════════════════════
1042
1043pub trait Node: Any {
1044    fn mount(&mut self) {}
1045    fn update(&mut self) {}
1046    fn unmount(&mut self) {}
1047    fn insert_child(&mut self, _child: NodeId) {}
1048    fn remove_child(&mut self, _child: NodeId) {}
1049    fn move_child(&mut self, _from: usize, _to: usize) {}
1050    fn update_children(&mut self, _children: &[NodeId]) {}
1051    fn children(&self) -> Vec<NodeId> {
1052        Vec::new()
1053    }
1054    /// Copies child IDs into the provided scratch buffer without allocating a
1055    /// fresh container for every traversal.
1056    fn collect_children_into(&self, out: &mut SmallVec<[NodeId; 8]>) {
1057        out.clear();
1058        out.extend(self.children());
1059    }
1060    /// Called after the node is created to record its own ID.
1061    /// Useful for nodes that need to store their ID for later operations.
1062    fn set_node_id(&mut self, _id: NodeId) {}
1063    /// Called when this node is attached to a parent.
1064    /// Nodes with parent tracking should set their parent reference here.
1065    fn on_attached_to_parent(&mut self, _parent: NodeId) {}
1066    /// Called when this node is removed from its parent.
1067    /// Nodes with parent tracking should clear their parent reference here.
1068    fn on_removed_from_parent(&mut self) {}
1069    /// Get this node's parent ID (for nodes that track parents).
1070    /// Returns None if node has no parent or doesn't track parents.
1071    fn parent(&self) -> Option<NodeId> {
1072        None
1073    }
1074    /// Mark this node as needing layout (for nodes with dirty flags).
1075    /// Called during bubbling to propagate dirtiness up the tree.
1076    fn mark_needs_layout(&self) {}
1077    /// Check if this node needs layout (for nodes with dirty flags).
1078    fn needs_layout(&self) -> bool {
1079        false
1080    }
1081    /// Mark this node as needing measure (size may have changed).
1082    /// Called during bubbling when children are added/removed.
1083    fn mark_needs_measure(&self) {}
1084    /// Check if this node needs measure (for nodes with dirty flags).
1085    fn needs_measure(&self) -> bool {
1086        false
1087    }
1088    /// Mark this node as needing semantics recomputation.
1089    fn mark_needs_semantics(&self) {}
1090    /// Check if this node needs semantics recomputation.
1091    fn needs_semantics(&self) -> bool {
1092        false
1093    }
1094    /// Set parent reference for dirty flag bubbling ONLY.
1095    /// This is a minimal version of on_attached_to_parent that doesn't trigger
1096    /// registry updates or other side effects. Used during measurement when we
1097    /// need to establish parent connections for bubble_measure_dirty without
1098    /// causing the full attachment lifecycle.
1099    ///
1100    /// Default implementation uses the normal parent-attachment hook.
1101    fn set_parent_for_bubbling(&mut self, parent: NodeId) {
1102        self.on_attached_to_parent(parent);
1103    }
1104
1105    /// Returns a recycle pool key when this node supports shell reuse.
1106    fn recycle_key(&self) -> Option<TypeId> {
1107        None
1108    }
1109
1110    /// Bounds how many recyclable shells of this node type should be retained.
1111    fn recycle_pool_limit(&self) -> Option<usize> {
1112        None
1113    }
1114
1115    /// Clears live attachments before the node shell enters a recycle pool.
1116    fn prepare_for_recycle(&mut self) {}
1117
1118    /// Optionally provides a compact replacement box for this recycled shell.
1119    ///
1120    /// Returning `Some` lets nodes move pooled survivors onto fresh compact
1121    /// storage so the recycle pool does not pin large spike-era allocations.
1122    fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
1123        None
1124    }
1125
1126    /// Optionally moves a live node onto a fresh box during applier compaction.
1127    ///
1128    /// This is used after large-majority teardowns where a small surviving live
1129    /// tree can otherwise pin allocator pages from a much larger spike-era node
1130    /// population. Implementations must preserve the node's live state.
1131    fn rehouse_for_live_compaction(&mut self) -> Option<Box<dyn Node>> {
1132        None
1133    }
1134
1135    /// Returns the node-owned heap retained beyond the node's own box allocation.
1136    fn debug_heap_bytes(&self) -> usize {
1137        0
1138    }
1139}
1140
1141/// Unified API for bubbling layout dirty flags from a node to the root (Applier context).
1142///
1143/// This is the canonical function for dirty bubbling during the apply phase (structural changes).
1144/// Call this after mutations like insert/remove/move that happen during apply.
1145///
1146/// # Behavior
1147/// 1. Marks the starting node as needing layout
1148/// 2. Walks up the parent chain, marking each ancestor
1149/// 3. Stops when it reaches a node that's already dirty (O(1) optimization)
1150/// 4. Stops at the root (node with no parent)
1151///
1152/// # Performance
1153/// This function is O(height) in the worst case, but typically O(1) due to early exit
1154/// when encountering an already-dirty ancestor.
1155///
1156/// # Usage
1157/// - Call from composer mutations (insert/remove/move) during apply phase
1158/// - Call from applier-level operations that modify the tree structure
1159pub fn bubble_layout_dirty(applier: &mut dyn Applier, node_id: NodeId) {
1160    bubble_layout_dirty_applier(applier, node_id);
1161}
1162
1163/// Unified API for bubbling measure dirty flags from a node to the root (Applier context).
1164///
1165/// Call this when a node's size may have changed (children added/removed, modifier changed).
1166/// This ensures that measure_layout will increment the cache epoch and re-measure the subtree.
1167///
1168/// # Behavior
1169/// 1. Marks the starting node as needing measure
1170/// 2. Walks up the parent chain, marking each ancestor
1171/// 3. Stops when it reaches a node that's already dirty (O(1) optimization)
1172/// 4. Stops at the root (node with no parent)
1173pub fn bubble_measure_dirty(applier: &mut dyn Applier, node_id: NodeId) {
1174    bubble_measure_dirty_applier(applier, node_id);
1175}
1176
1177/// Unified API for bubbling semantics dirty flags from a node to the root (Applier context).
1178///
1179/// This mirrors [`bubble_layout_dirty`] but toggles semantics-specific dirty
1180/// flags instead of layout ones, allowing semantics updates to propagate during
1181/// the apply phase without forcing layout work.
1182pub fn bubble_semantics_dirty(applier: &mut dyn Applier, node_id: NodeId) {
1183    bubble_semantics_dirty_applier(applier, node_id);
1184}
1185
1186/// Schedules semantics bubbling for a node using the active composer if present.
1187///
1188/// This defers the work to the apply phase where we can safely mutate the
1189/// applier tree without re-entrantly borrowing the composer during composition.
1190pub fn queue_semantics_invalidation(node_id: NodeId) {
1191    let _ = composer_context::try_with_composer(|composer| {
1192        composer.enqueue_semantics_invalidation(node_id);
1193    });
1194}
1195
1196/// Unified API for bubbling layout dirty flags from a node to the root (Composer context).
1197///
1198/// This is the canonical function for dirty bubbling during composition (property changes).
1199/// Call this after property changes that happen during composition via with_node_mut.
1200///
1201/// # Behavior
1202/// 1. Marks the starting node as needing layout
1203/// 2. Walks up the parent chain, marking each ancestor
1204/// 3. Stops when it reaches a node that's already dirty (O(1) optimization)
1205/// 4. Stops at the root (node with no parent)
1206///
1207/// # Performance
1208/// This function is O(height) in the worst case, but typically O(1) due to early exit
1209/// when encountering an already-dirty ancestor.
1210///
1211/// # Type Requirements
1212/// The node type N must implement Node (which includes mark_needs_layout, parent, etc.).
1213/// Typically this will be LayoutNode or similar layout-aware node types.
1214///
1215/// # Usage
1216/// - Call from property setters during composition (e.g., set_modifier, set_measure_policy)
1217/// - Call from widget composition when layout-affecting state changes
1218pub fn bubble_layout_dirty_in_composer<N: Node + 'static>(node_id: NodeId) {
1219    bubble_layout_dirty_composer::<N>(node_id);
1220}
1221
1222/// Unified API for bubbling measure dirty flags from a node to the root during composition.
1223///
1224/// This queues a dirty-bubble command on the active composer so measure invalidation
1225/// runs during the apply phase, avoiding re-entrant applier borrows while widgets are
1226/// mutating nodes via `with_node_mut`.
1227pub fn bubble_measure_dirty_in_composer(node_id: NodeId) {
1228    with_current_composer(|composer| {
1229        composer.commands_mut().push(Command::BubbleDirty {
1230            node_id,
1231            bubble: DirtyBubble {
1232                layout: false,
1233                measure: true,
1234                semantics: false,
1235            },
1236        });
1237    });
1238}
1239
1240/// Unified API for bubbling semantics dirty flags from a node to the root (Composer context).
1241///
1242/// This mirrors [`bubble_layout_dirty_in_composer`] but routes through the semantics
1243/// dirty flag instead of the layout one. Modifier nodes can request semantics
1244/// invalidations without triggering measure/layout work, and the runtime can
1245/// query the root to determine whether the semantics tree needs rebuilding.
1246pub fn bubble_semantics_dirty_in_composer<N: Node + 'static>(node_id: NodeId) {
1247    bubble_semantics_dirty_composer::<N>(node_id);
1248}
1249
1250/// Internal implementation for applier-based bubbling.
1251fn bubble_layout_dirty_applier(applier: &mut dyn Applier, mut node_id: NodeId) {
1252    // First, mark the starting node dirty (critical!)
1253    // This ensures root gets marked even if it has no parent
1254    if let Ok(node) = applier.get_mut(node_id) {
1255        node.mark_needs_layout();
1256    }
1257
1258    // Then bubble up to ancestors
1259    loop {
1260        // Get parent of current node
1261        let parent_id = match applier.get_mut(node_id) {
1262            Ok(node) => node.parent(),
1263            Err(_) => None,
1264        };
1265
1266        match parent_id {
1267            Some(pid) => {
1268                // Mark parent as needing layout
1269                if let Ok(parent) = applier.get_mut(pid) {
1270                    let parent_already_dirty = parent.needs_layout();
1271                    if !parent_already_dirty {
1272                        parent.mark_needs_layout();
1273                    }
1274                    node_id = pid;
1275                } else {
1276                    break;
1277                }
1278            }
1279            None => break, // No parent, stop
1280        }
1281    }
1282}
1283
1284/// Internal implementation for applier-based bubbling of measure dirtiness.
1285fn bubble_measure_dirty_applier(applier: &mut dyn Applier, mut node_id: NodeId) {
1286    // First, mark the starting node as needing measure
1287    if let Ok(node) = applier.get_mut(node_id) {
1288        node.mark_needs_measure();
1289    }
1290
1291    // Then bubble up to ancestors
1292    loop {
1293        // Get parent of current node
1294        let parent_id = match applier.get_mut(node_id) {
1295            Ok(node) => node.parent(),
1296            Err(_) => None,
1297        };
1298
1299        match parent_id {
1300            Some(pid) => {
1301                // Mark parent as needing measure
1302                if let Ok(parent) = applier.get_mut(pid) {
1303                    if !parent.needs_measure() {
1304                        parent.mark_needs_measure();
1305                    }
1306                    node_id = pid;
1307                } else {
1308                    break;
1309                }
1310            }
1311            None => {
1312                break; // No parent, stop
1313            }
1314        }
1315    }
1316}
1317
1318/// Internal implementation for applier-based bubbling of semantics dirtiness.
1319fn bubble_semantics_dirty_applier(applier: &mut dyn Applier, mut node_id: NodeId) {
1320    if let Ok(node) = applier.get_mut(node_id) {
1321        node.mark_needs_semantics();
1322    }
1323
1324    loop {
1325        let parent_id = match applier.get_mut(node_id) {
1326            Ok(node) => node.parent(),
1327            Err(_) => None,
1328        };
1329
1330        match parent_id {
1331            Some(pid) => {
1332                if let Ok(parent) = applier.get_mut(pid) {
1333                    if !parent.needs_semantics() {
1334                        parent.mark_needs_semantics();
1335                    }
1336                    node_id = pid;
1337                } else {
1338                    break;
1339                }
1340            }
1341            None => break,
1342        }
1343    }
1344}
1345
1346/// Internal implementation for composer-based bubbling.
1347/// This uses with_node_mut and works during composition with a concrete node type.
1348/// The node type N must implement Node (which includes mark_needs_layout, parent, etc.).
1349fn bubble_layout_dirty_composer<N: Node + 'static>(mut node_id: NodeId) {
1350    // Mark the starting node dirty
1351    let _ = with_node_mut(node_id, |node: &mut N| {
1352        node.mark_needs_layout();
1353    });
1354
1355    // Then bubble up to ancestors
1356    while let Ok(Some(pid)) = with_node_mut(node_id, |node: &mut N| node.parent()) {
1357        let parent_id = pid;
1358
1359        // Mark parent as needing layout
1360        let advanced = with_node_mut(parent_id, |node: &mut N| {
1361            if !node.needs_layout() {
1362                node.mark_needs_layout();
1363            }
1364            true
1365        })
1366        .unwrap_or(false);
1367
1368        if advanced {
1369            node_id = parent_id;
1370        } else {
1371            break;
1372        }
1373    }
1374}
1375
1376/// Internal implementation for composer-based bubbling of semantics dirtiness.
1377fn bubble_semantics_dirty_composer<N: Node + 'static>(mut node_id: NodeId) {
1378    // Mark the starting node semantics-dirty.
1379    let _ = with_node_mut(node_id, |node: &mut N| {
1380        node.mark_needs_semantics();
1381    });
1382
1383    while let Ok(Some(pid)) = with_node_mut(node_id, |node: &mut N| node.parent()) {
1384        let parent_id = pid;
1385
1386        let advanced = with_node_mut(parent_id, |node: &mut N| {
1387            if !node.needs_semantics() {
1388                node.mark_needs_semantics();
1389            }
1390            true
1391        })
1392        .unwrap_or(false);
1393
1394        if advanced {
1395            node_id = parent_id;
1396        } else {
1397            break;
1398        }
1399    }
1400}
1401
1402impl dyn Node {
1403    pub fn as_any_mut(&mut self) -> &mut dyn Any {
1404        self
1405    }
1406}
1407
1408pub struct RecycledNode {
1409    stable_id: NodeId,
1410    node: Box<dyn Node>,
1411    warm_origin: bool,
1412}
1413
1414impl RecycledNode {
1415    fn new(stable_id: NodeId, node: Box<dyn Node>, warm_origin: bool) -> Self {
1416        let node = node.rehouse_for_recycle().unwrap_or(node);
1417        Self {
1418            stable_id,
1419            node,
1420            warm_origin,
1421        }
1422    }
1423
1424    fn from_shell(stable_id: NodeId, node: Box<dyn Node>, warm_origin: bool) -> Self {
1425        Self {
1426            stable_id,
1427            node,
1428            warm_origin,
1429        }
1430    }
1431
1432    pub fn stable_id(&self) -> NodeId {
1433        self.stable_id
1434    }
1435
1436    fn warm_origin(&self) -> bool {
1437        self.warm_origin
1438    }
1439
1440    fn set_warm_origin(&mut self, warm_origin: bool) {
1441        self.warm_origin = warm_origin;
1442    }
1443
1444    pub fn node_mut(&mut self) -> &mut dyn Node {
1445        self.node.as_mut()
1446    }
1447
1448    pub fn into_parts(self) -> (NodeId, Box<dyn Node>, bool) {
1449        (self.stable_id, self.node, self.warm_origin)
1450    }
1451}
1452
1453#[derive(Debug, Clone, PartialEq, Eq)]
1454pub struct RecycledNodeInsertion {
1455    pub id: NodeId,
1456    pub stable_id_reused: bool,
1457    pub fallback_error: Option<NodeError>,
1458}
1459
1460impl RecycledNodeInsertion {
1461    fn reused(stable_id: NodeId) -> Self {
1462        Self {
1463            id: stable_id,
1464            stable_id_reused: true,
1465            fallback_error: None,
1466        }
1467    }
1468
1469    fn fresh(id: NodeId, fallback_error: Option<NodeError>) -> Self {
1470        Self {
1471            id,
1472            stable_id_reused: false,
1473            fallback_error,
1474        }
1475    }
1476}
1477
1478pub trait Applier: Any {
1479    fn create(&mut self, node: Box<dyn Node>) -> NodeId;
1480    fn get_mut(&mut self, id: NodeId) -> Result<&mut dyn Node, NodeError>;
1481    fn remove(&mut self, id: NodeId) -> Result<(), NodeError>;
1482
1483    /// Returns the current generation for a node index.
1484    /// Generation is incremented when an index is reused from the freelist,
1485    /// preventing stale slot entries from matching recycled nodes.
1486    fn node_generation(&self, id: NodeId) -> u32;
1487
1488    /// Inserts a node with a pre-assigned ID.
1489    ///
1490    /// This is used for virtual nodes whose IDs are allocated separately
1491    /// (e.g., via allocate_virtual_node_id()). Unlike `create()` which assigns
1492    /// a new ID, this method uses the provided ID.
1493    ///
1494    /// Returns Ok(()) if successful, or an error if the ID is already in use.
1495    fn insert_with_id(&mut self, id: NodeId, node: Box<dyn Node>) -> Result<(), NodeError>;
1496
1497    /// Reinserts a recycled node at its retained stable ID, or creates a fresh ID if that
1498    /// retained ID is no longer available.
1499    fn insert_recycled_node_or_create(
1500        &mut self,
1501        stable_id: NodeId,
1502        node: Box<dyn Node>,
1503    ) -> RecycledNodeInsertion {
1504        let id = self.create(node);
1505        RecycledNodeInsertion::fresh(id, Some(NodeError::AlreadyExists { id: stable_id }))
1506    }
1507
1508    fn as_any(&self) -> &dyn Any
1509    where
1510        Self: Sized,
1511    {
1512        self
1513    }
1514
1515    fn as_any_mut(&mut self) -> &mut dyn Any
1516    where
1517        Self: Sized,
1518    {
1519        self
1520    }
1521
1522    /// Trim trailing tombstones/unused capacity after structural changes.
1523    fn compact(&mut self) {}
1524
1525    /// Returns a previously recycled node shell and its stable ID for the requested concrete type.
1526    fn take_recycled_node(&mut self, _key: TypeId) -> Option<RecycledNode> {
1527        None
1528    }
1529
1530    /// Marks whether a reinserted recycled node originated from the warm recycle path.
1531    fn set_recycled_node_origin(&mut self, _id: NodeId, _warm_origin: bool) {}
1532
1533    /// Seeds a warm recyclable shell for future reuse without requiring a prior removal.
1534    fn seed_recycled_node_shell(
1535        &mut self,
1536        _key: TypeId,
1537        _recycle_pool_limit: Option<usize>,
1538        _shell: Box<dyn Node>,
1539    ) {
1540    }
1541
1542    /// Records that the current apply pass had to allocate a fresh recyclable shell for this type.
1543    fn record_fresh_recyclable_creation(&mut self, _key: TypeId) {}
1544
1545    /// Drops any recyclable shells that should not survive beyond the current apply pass.
1546    fn clear_recycled_nodes(&mut self) {}
1547}
1548
1549type TypedNodeUpdate = fn(&mut dyn Node, NodeId) -> Result<(), NodeError>;
1550type CommandCallback = Box<dyn FnOnce(&mut dyn Applier) -> Result<(), NodeError> + 'static>;
1551
1552#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1553pub(crate) struct DirtyBubble {
1554    layout: bool,
1555    measure: bool,
1556    semantics: bool,
1557}
1558
1559impl DirtyBubble {
1560    pub(crate) const LAYOUT_AND_MEASURE: Self = Self {
1561        layout: true,
1562        measure: true,
1563        semantics: false,
1564    };
1565
1566    pub(crate) const SEMANTICS: Self = Self {
1567        layout: false,
1568        measure: false,
1569        semantics: true,
1570    };
1571
1572    fn apply(self, applier: &mut dyn Applier, node_id: NodeId) {
1573        if self.layout {
1574            bubble_layout_dirty(applier, node_id);
1575        }
1576        if self.measure {
1577            bubble_measure_dirty(applier, node_id);
1578        }
1579        if self.semantics {
1580            bubble_semantics_dirty(applier, node_id);
1581        }
1582    }
1583}
1584
1585pub(crate) enum Command {
1586    BubbleDirty {
1587        node_id: NodeId,
1588        bubble: DirtyBubble,
1589    },
1590    UpdateTypedNode {
1591        id: NodeId,
1592        updater: TypedNodeUpdate,
1593    },
1594    RemoveNode {
1595        id: NodeId,
1596    },
1597    MountNode {
1598        id: NodeId,
1599    },
1600    AttachChild {
1601        parent_id: NodeId,
1602        child_id: NodeId,
1603        bubble: DirtyBubble,
1604    },
1605    InsertChild {
1606        parent_id: NodeId,
1607        child_id: NodeId,
1608        appended_index: usize,
1609        insert_index: usize,
1610        bubble: DirtyBubble,
1611    },
1612    MoveChild {
1613        parent_id: NodeId,
1614        from_index: usize,
1615        to_index: usize,
1616        bubble: DirtyBubble,
1617    },
1618    RemoveChild {
1619        parent_id: NodeId,
1620        child_id: NodeId,
1621    },
1622    DetachChild {
1623        parent_id: NodeId,
1624        child_id: NodeId,
1625    },
1626    SyncChildren {
1627        parent_id: NodeId,
1628        expected_children: ChildList,
1629    },
1630    Callback(CommandCallback),
1631}
1632
1633#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1634struct DeferredChildCleanup {
1635    child_id: NodeId,
1636    generation: u32,
1637    removed_from_parent: bool,
1638}
1639
1640#[derive(Default)]
1641struct DeferredChildCleanupQueue {
1642    pending: Vec<DeferredChildCleanup>,
1643    preserved: Vec<(NodeId, u32)>,
1644}
1645
1646impl DeferredChildCleanupQueue {
1647    fn push(&mut self, child_id: NodeId, generation: u32, removed_from_parent: bool) {
1648        if self
1649            .preserved
1650            .iter()
1651            .any(|&(preserved_id, preserved_generation)| {
1652                preserved_id == child_id && preserved_generation == generation
1653            })
1654        {
1655            return;
1656        }
1657        self.pending.push(DeferredChildCleanup {
1658            child_id,
1659            generation,
1660            removed_from_parent,
1661        });
1662    }
1663
1664    fn preserve(&mut self, child_id: NodeId, generation: u32) {
1665        if !self
1666            .preserved
1667            .iter()
1668            .any(|&(preserved_id, preserved_generation)| {
1669                preserved_id == child_id && preserved_generation == generation
1670            })
1671        {
1672            self.preserved.push((child_id, generation));
1673        }
1674        self.pending
1675            .retain(|cleanup| cleanup.child_id != child_id || cleanup.generation != generation);
1676    }
1677
1678    fn flush(self, applier: &mut dyn Applier) -> Result<(), NodeError> {
1679        for cleanup in self.pending {
1680            cleanup_detached_child(applier, cleanup)?;
1681        }
1682        Ok(())
1683    }
1684}
1685
1686impl Command {
1687    pub(crate) fn update_node<N: Node + 'static>(id: NodeId) -> Self {
1688        Self::UpdateTypedNode {
1689            id,
1690            updater: update_typed_node::<N>,
1691        }
1692    }
1693
1694    pub(crate) fn callback(
1695        callback: impl FnOnce(&mut dyn Applier) -> Result<(), NodeError> + 'static,
1696    ) -> Self {
1697        Self::Callback(Box::new(callback))
1698    }
1699
1700    pub(crate) fn apply(self, applier: &mut dyn Applier) -> Result<(), NodeError> {
1701        let mut deferred_cleanup = DeferredChildCleanupQueue::default();
1702        self.apply_with_cleanup(applier, &mut deferred_cleanup)?;
1703        deferred_cleanup.flush(applier)
1704    }
1705
1706    fn apply_with_cleanup(
1707        self,
1708        applier: &mut dyn Applier,
1709        deferred_cleanup: &mut DeferredChildCleanupQueue,
1710    ) -> Result<(), NodeError> {
1711        match self {
1712            Self::BubbleDirty { node_id, bubble } => {
1713                bubble.apply(applier, node_id);
1714                Ok(())
1715            }
1716            Self::UpdateTypedNode { id, updater } => {
1717                let node = match applier.get_mut(id) {
1718                    Ok(node) => node,
1719                    Err(NodeError::Missing { .. }) => return Ok(()),
1720                    Err(err) => return Err(err),
1721                };
1722                updater(node, id)
1723            }
1724            Self::RemoveNode { id } => {
1725                if let Ok(node) = applier.get_mut(id) {
1726                    node.unmount();
1727                }
1728                match applier.remove(id) {
1729                    Ok(()) | Err(NodeError::Missing { .. }) => Ok(()),
1730                    Err(err) => Err(err),
1731                }
1732            }
1733            Self::MountNode { id } => {
1734                let node = match applier.get_mut(id) {
1735                    Ok(node) => node,
1736                    Err(NodeError::Missing { .. }) => return Ok(()),
1737                    Err(err) => return Err(err),
1738                };
1739                node.set_node_id(id);
1740                node.mount();
1741                Ok(())
1742            }
1743            Self::AttachChild {
1744                parent_id,
1745                child_id,
1746                bubble,
1747            } => {
1748                insert_child_with_reparenting(applier, parent_id, child_id);
1749                bubble.apply(applier, parent_id);
1750                Ok(())
1751            }
1752            Self::InsertChild {
1753                parent_id,
1754                child_id,
1755                appended_index,
1756                insert_index,
1757                bubble,
1758            } => {
1759                insert_child_with_reparenting(applier, parent_id, child_id);
1760                bubble.apply(applier, parent_id);
1761                if insert_index != appended_index {
1762                    if let Ok(parent_node) = applier.get_mut(parent_id) {
1763                        parent_node.move_child(appended_index, insert_index);
1764                    }
1765                }
1766                Ok(())
1767            }
1768            Self::MoveChild {
1769                parent_id,
1770                from_index,
1771                to_index,
1772                bubble,
1773            } => {
1774                if let Ok(parent_node) = applier.get_mut(parent_id) {
1775                    parent_node.move_child(from_index, to_index);
1776                }
1777                bubble.apply(applier, parent_id);
1778                Ok(())
1779            }
1780            Self::RemoveChild {
1781                parent_id,
1782                child_id,
1783            } => apply_remove_child(applier, parent_id, child_id, deferred_cleanup),
1784            Self::DetachChild {
1785                parent_id,
1786                child_id,
1787            } => {
1788                let generation = applier.node_generation(child_id);
1789                detach_child_from_parent(applier, parent_id, child_id)?;
1790                deferred_cleanup.preserve(child_id, generation);
1791                Ok(())
1792            }
1793            Self::SyncChildren {
1794                parent_id,
1795                expected_children,
1796            } => sync_children(applier, parent_id, &expected_children, deferred_cleanup),
1797            Self::Callback(callback) => callback(applier),
1798        }
1799    }
1800}
1801
1802const COMMAND_CHUNK_CAPACITY: usize = 1024;
1803const COMMAND_FLUSH_THRESHOLD: usize = COMMAND_CHUNK_CAPACITY * 4;
1804type ChildList = SmallVec<[NodeId; 4]>;
1805const SMALL_CHILD_SYNC_LINEAR_THRESHOLD: usize = 8;
1806
1807#[derive(Copy, Clone)]
1808enum CommandTag {
1809    BubbleDirty,
1810    UpdateTypedNode,
1811    RemoveNode,
1812    MountNode,
1813    AttachChild,
1814    InsertChild,
1815    MoveChild,
1816    RemoveChild,
1817    DetachChild,
1818    SyncChildren,
1819    Callback,
1820}
1821
1822impl CommandTag {
1823    fn label(self) -> &'static str {
1824        match self {
1825            Self::BubbleDirty => "BubbleDirty",
1826            Self::UpdateTypedNode => "UpdateTypedNode",
1827            Self::RemoveNode => "RemoveNode",
1828            Self::MountNode => "MountNode",
1829            Self::AttachChild => "AttachChild",
1830            Self::InsertChild => "InsertChild",
1831            Self::MoveChild => "MoveChild",
1832            Self::RemoveChild => "RemoveChild",
1833            Self::DetachChild => "DetachChild",
1834            Self::SyncChildren => "SyncChildren",
1835            Self::Callback => "Callback",
1836        }
1837    }
1838}
1839
1840#[derive(Copy, Clone)]
1841struct BubbleDirtyCommand {
1842    node_id: NodeId,
1843    bubble: DirtyBubble,
1844}
1845
1846#[derive(Copy, Clone)]
1847struct UpdateTypedNodeCommand {
1848    id: NodeId,
1849    updater: TypedNodeUpdate,
1850}
1851
1852#[derive(Copy, Clone)]
1853struct AttachChildCommand {
1854    parent_id: NodeId,
1855    child_id: NodeId,
1856    bubble: DirtyBubble,
1857}
1858
1859#[derive(Copy, Clone)]
1860struct InsertChildCommand {
1861    parent_id: NodeId,
1862    child_id: NodeId,
1863    appended_index: usize,
1864    insert_index: usize,
1865    bubble: DirtyBubble,
1866}
1867
1868#[derive(Copy, Clone)]
1869struct MoveChildCommand {
1870    parent_id: NodeId,
1871    from_index: usize,
1872    to_index: usize,
1873    bubble: DirtyBubble,
1874}
1875
1876#[derive(Copy, Clone)]
1877struct RemoveChildCommand {
1878    parent_id: NodeId,
1879    child_id: NodeId,
1880}
1881
1882#[derive(Copy, Clone)]
1883struct DetachChildCommand {
1884    parent_id: NodeId,
1885    child_id: NodeId,
1886}
1887
1888struct SyncChildrenCommand {
1889    parent_id: NodeId,
1890    child_start: usize,
1891    child_len: usize,
1892}
1893
1894#[derive(Default)]
1895struct CommandQueue {
1896    chunks: Vec<Vec<CommandTag>>,
1897    len: usize,
1898    bubble_dirty: Vec<BubbleDirtyCommand>,
1899    update_typed_nodes: Vec<UpdateTypedNodeCommand>,
1900    remove_nodes: Vec<NodeId>,
1901    mount_nodes: Vec<NodeId>,
1902    attach_children: Vec<AttachChildCommand>,
1903    insert_children: Vec<InsertChildCommand>,
1904    move_children: Vec<MoveChildCommand>,
1905    remove_children: Vec<RemoveChildCommand>,
1906    detach_children: Vec<DetachChildCommand>,
1907    sync_children: Vec<SyncChildrenCommand>,
1908    sync_child_ids: Vec<NodeId>,
1909    callbacks: Vec<CommandCallback>,
1910}
1911
1912impl CommandQueue {
1913    fn push_tag(&mut self, tag: CommandTag) {
1914        let needs_chunk = self
1915            .chunks
1916            .last()
1917            .map(|chunk| chunk.len() == chunk.capacity())
1918            .unwrap_or(true);
1919        if needs_chunk {
1920            self.chunks.push(Vec::with_capacity(COMMAND_CHUNK_CAPACITY));
1921        }
1922        if let Some(chunk) = self.chunks.last_mut() {
1923            chunk.push(tag);
1924            self.len += 1;
1925        }
1926    }
1927
1928    fn push(&mut self, command: Command) {
1929        match command {
1930            Command::BubbleDirty { node_id, bubble } => {
1931                self.bubble_dirty
1932                    .push(BubbleDirtyCommand { node_id, bubble });
1933                self.push_tag(CommandTag::BubbleDirty);
1934            }
1935            Command::UpdateTypedNode { id, updater } => {
1936                self.update_typed_nodes
1937                    .push(UpdateTypedNodeCommand { id, updater });
1938                self.push_tag(CommandTag::UpdateTypedNode);
1939            }
1940            Command::RemoveNode { id } => {
1941                self.remove_nodes.push(id);
1942                self.push_tag(CommandTag::RemoveNode);
1943            }
1944            Command::MountNode { id } => {
1945                self.mount_nodes.push(id);
1946                self.push_tag(CommandTag::MountNode);
1947            }
1948            Command::AttachChild {
1949                parent_id,
1950                child_id,
1951                bubble,
1952            } => {
1953                self.attach_children.push(AttachChildCommand {
1954                    parent_id,
1955                    child_id,
1956                    bubble,
1957                });
1958                self.push_tag(CommandTag::AttachChild);
1959            }
1960            Command::InsertChild {
1961                parent_id,
1962                child_id,
1963                appended_index,
1964                insert_index,
1965                bubble,
1966            } => {
1967                self.insert_children.push(InsertChildCommand {
1968                    parent_id,
1969                    child_id,
1970                    appended_index,
1971                    insert_index,
1972                    bubble,
1973                });
1974                self.push_tag(CommandTag::InsertChild);
1975            }
1976            Command::MoveChild {
1977                parent_id,
1978                from_index,
1979                to_index,
1980                bubble,
1981            } => {
1982                self.move_children.push(MoveChildCommand {
1983                    parent_id,
1984                    from_index,
1985                    to_index,
1986                    bubble,
1987                });
1988                self.push_tag(CommandTag::MoveChild);
1989            }
1990            Command::RemoveChild {
1991                parent_id,
1992                child_id,
1993            } => {
1994                self.remove_children.push(RemoveChildCommand {
1995                    parent_id,
1996                    child_id,
1997                });
1998                self.push_tag(CommandTag::RemoveChild);
1999            }
2000            Command::DetachChild {
2001                parent_id,
2002                child_id,
2003            } => {
2004                self.detach_children.push(DetachChildCommand {
2005                    parent_id,
2006                    child_id,
2007                });
2008                self.push_tag(CommandTag::DetachChild);
2009            }
2010            Command::SyncChildren {
2011                parent_id,
2012                expected_children,
2013            } => {
2014                let child_start = self.sync_child_ids.len();
2015                let child_len = expected_children.len();
2016                self.sync_child_ids.extend(expected_children);
2017                self.sync_children.push(SyncChildrenCommand {
2018                    parent_id,
2019                    child_start,
2020                    child_len,
2021                });
2022                self.push_tag(CommandTag::SyncChildren);
2023            }
2024            Command::Callback(callback) => {
2025                self.callbacks.push(callback);
2026                self.push_tag(CommandTag::Callback);
2027            }
2028        }
2029    }
2030
2031    fn len(&self) -> usize {
2032        self.len
2033    }
2034
2035    fn capacity(&self) -> usize {
2036        self.chunks.iter().map(Vec::capacity).sum()
2037    }
2038
2039    fn payload_len_bytes(&self) -> usize {
2040        self.bubble_dirty
2041            .len()
2042            .saturating_mul(std::mem::size_of::<BubbleDirtyCommand>())
2043            .saturating_add(
2044                self.update_typed_nodes
2045                    .len()
2046                    .saturating_mul(std::mem::size_of::<UpdateTypedNodeCommand>()),
2047            )
2048            .saturating_add(
2049                self.remove_nodes
2050                    .len()
2051                    .saturating_mul(std::mem::size_of::<NodeId>()),
2052            )
2053            .saturating_add(
2054                self.mount_nodes
2055                    .len()
2056                    .saturating_mul(std::mem::size_of::<NodeId>()),
2057            )
2058            .saturating_add(
2059                self.attach_children
2060                    .len()
2061                    .saturating_mul(std::mem::size_of::<AttachChildCommand>()),
2062            )
2063            .saturating_add(
2064                self.insert_children
2065                    .len()
2066                    .saturating_mul(std::mem::size_of::<InsertChildCommand>()),
2067            )
2068            .saturating_add(
2069                self.move_children
2070                    .len()
2071                    .saturating_mul(std::mem::size_of::<MoveChildCommand>()),
2072            )
2073            .saturating_add(
2074                self.remove_children
2075                    .len()
2076                    .saturating_mul(std::mem::size_of::<RemoveChildCommand>()),
2077            )
2078            .saturating_add(
2079                self.detach_children
2080                    .len()
2081                    .saturating_mul(std::mem::size_of::<DetachChildCommand>()),
2082            )
2083            .saturating_add(
2084                self.sync_children
2085                    .len()
2086                    .saturating_mul(std::mem::size_of::<SyncChildrenCommand>()),
2087            )
2088            .saturating_add(
2089                self.sync_child_ids
2090                    .len()
2091                    .saturating_mul(std::mem::size_of::<NodeId>()),
2092            )
2093            .saturating_add(
2094                self.callbacks
2095                    .len()
2096                    .saturating_mul(std::mem::size_of::<CommandCallback>()),
2097            )
2098    }
2099
2100    fn payload_capacity_bytes(&self) -> usize {
2101        self.bubble_dirty
2102            .capacity()
2103            .saturating_mul(std::mem::size_of::<BubbleDirtyCommand>())
2104            .saturating_add(
2105                self.update_typed_nodes
2106                    .capacity()
2107                    .saturating_mul(std::mem::size_of::<UpdateTypedNodeCommand>()),
2108            )
2109            .saturating_add(
2110                self.remove_nodes
2111                    .capacity()
2112                    .saturating_mul(std::mem::size_of::<NodeId>()),
2113            )
2114            .saturating_add(
2115                self.mount_nodes
2116                    .capacity()
2117                    .saturating_mul(std::mem::size_of::<NodeId>()),
2118            )
2119            .saturating_add(
2120                self.attach_children
2121                    .capacity()
2122                    .saturating_mul(std::mem::size_of::<AttachChildCommand>()),
2123            )
2124            .saturating_add(
2125                self.insert_children
2126                    .capacity()
2127                    .saturating_mul(std::mem::size_of::<InsertChildCommand>()),
2128            )
2129            .saturating_add(
2130                self.move_children
2131                    .capacity()
2132                    .saturating_mul(std::mem::size_of::<MoveChildCommand>()),
2133            )
2134            .saturating_add(
2135                self.remove_children
2136                    .capacity()
2137                    .saturating_mul(std::mem::size_of::<RemoveChildCommand>()),
2138            )
2139            .saturating_add(
2140                self.detach_children
2141                    .capacity()
2142                    .saturating_mul(std::mem::size_of::<DetachChildCommand>()),
2143            )
2144            .saturating_add(
2145                self.sync_children
2146                    .capacity()
2147                    .saturating_mul(std::mem::size_of::<SyncChildrenCommand>()),
2148            )
2149            .saturating_add(
2150                self.sync_child_ids
2151                    .capacity()
2152                    .saturating_mul(std::mem::size_of::<NodeId>()),
2153            )
2154            .saturating_add(
2155                self.callbacks
2156                    .capacity()
2157                    .saturating_mul(std::mem::size_of::<CommandCallback>()),
2158            )
2159    }
2160
2161    fn apply(self, applier: &mut dyn Applier) -> Result<(), NodeError> {
2162        let mut bubble_dirty = self.bubble_dirty.into_iter();
2163        let mut update_typed_nodes = self.update_typed_nodes.into_iter();
2164        let mut remove_nodes = self.remove_nodes.into_iter();
2165        let mut mount_nodes = self.mount_nodes.into_iter();
2166        let mut attach_children = self.attach_children.into_iter();
2167        let mut insert_children = self.insert_children.into_iter();
2168        let mut move_children = self.move_children.into_iter();
2169        let mut remove_children = self.remove_children.into_iter();
2170        let mut detach_children = self.detach_children.into_iter();
2171        let mut sync_children_commands = self.sync_children.into_iter();
2172        let sync_child_ids = self.sync_child_ids;
2173        let mut callbacks = self.callbacks.into_iter();
2174        let mut deferred_cleanup = DeferredChildCleanupQueue::default();
2175
2176        for chunk in self.chunks {
2177            for tag in chunk {
2178                match tag {
2179                    CommandTag::BubbleDirty => {
2180                        let BubbleDirtyCommand { node_id, bubble } =
2181                            next_command_payload(&mut bubble_dirty, tag)?;
2182                        Command::BubbleDirty { node_id, bubble }
2183                            .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2184                    }
2185                    CommandTag::UpdateTypedNode => {
2186                        let UpdateTypedNodeCommand { id, updater } =
2187                            next_command_payload(&mut update_typed_nodes, tag)?;
2188                        Command::UpdateTypedNode { id, updater }
2189                            .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2190                    }
2191                    CommandTag::RemoveNode => {
2192                        let id = next_command_payload(&mut remove_nodes, tag)?;
2193                        Command::RemoveNode { id }
2194                            .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2195                    }
2196                    CommandTag::MountNode => {
2197                        let id = next_command_payload(&mut mount_nodes, tag)?;
2198                        Command::MountNode { id }
2199                            .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2200                    }
2201                    CommandTag::AttachChild => {
2202                        let AttachChildCommand {
2203                            parent_id,
2204                            child_id,
2205                            bubble,
2206                        } = next_command_payload(&mut attach_children, tag)?;
2207                        Command::AttachChild {
2208                            parent_id,
2209                            child_id,
2210                            bubble,
2211                        }
2212                        .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2213                    }
2214                    CommandTag::InsertChild => {
2215                        let InsertChildCommand {
2216                            parent_id,
2217                            child_id,
2218                            appended_index,
2219                            insert_index,
2220                            bubble,
2221                        } = next_command_payload(&mut insert_children, tag)?;
2222                        Command::InsertChild {
2223                            parent_id,
2224                            child_id,
2225                            appended_index,
2226                            insert_index,
2227                            bubble,
2228                        }
2229                        .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2230                    }
2231                    CommandTag::MoveChild => {
2232                        let MoveChildCommand {
2233                            parent_id,
2234                            from_index,
2235                            to_index,
2236                            bubble,
2237                        } = next_command_payload(&mut move_children, tag)?;
2238                        Command::MoveChild {
2239                            parent_id,
2240                            from_index,
2241                            to_index,
2242                            bubble,
2243                        }
2244                        .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2245                    }
2246                    CommandTag::RemoveChild => {
2247                        let RemoveChildCommand {
2248                            parent_id,
2249                            child_id,
2250                        } = next_command_payload(&mut remove_children, tag)?;
2251                        Command::RemoveChild {
2252                            parent_id,
2253                            child_id,
2254                        }
2255                        .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2256                    }
2257                    CommandTag::DetachChild => {
2258                        let DetachChildCommand {
2259                            parent_id,
2260                            child_id,
2261                        } = next_command_payload(&mut detach_children, tag)?;
2262                        Command::DetachChild {
2263                            parent_id,
2264                            child_id,
2265                        }
2266                        .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2267                    }
2268                    CommandTag::SyncChildren => {
2269                        let SyncChildrenCommand {
2270                            parent_id,
2271                            child_start,
2272                            child_len,
2273                        } = next_command_payload(&mut sync_children_commands, tag)?;
2274                        let child_end = child_start
2275                            .checked_add(child_len)
2276                            .ok_or_else(|| command_payload_error(tag))?;
2277                        let expected_children = sync_child_ids
2278                            .get(child_start..child_end)
2279                            .ok_or_else(|| command_payload_error(tag))?;
2280                        sync_children(
2281                            applier,
2282                            parent_id,
2283                            expected_children,
2284                            &mut deferred_cleanup,
2285                        )?;
2286                    }
2287                    CommandTag::Callback => {
2288                        let callback = next_command_payload(&mut callbacks, tag)?;
2289                        Command::Callback(callback)
2290                            .apply_with_cleanup(applier, &mut deferred_cleanup)?;
2291                    }
2292                }
2293            }
2294        }
2295
2296        debug_assert!(bubble_dirty.next().is_none());
2297        debug_assert!(update_typed_nodes.next().is_none());
2298        debug_assert!(remove_nodes.next().is_none());
2299        debug_assert!(mount_nodes.next().is_none());
2300        debug_assert!(attach_children.next().is_none());
2301        debug_assert!(insert_children.next().is_none());
2302        debug_assert!(move_children.next().is_none());
2303        debug_assert!(remove_children.next().is_none());
2304        debug_assert!(detach_children.next().is_none());
2305        debug_assert!(sync_children_commands.next().is_none());
2306        debug_assert!(callbacks.next().is_none());
2307        deferred_cleanup.flush(applier)
2308    }
2309}
2310
2311fn command_payload_error(tag: CommandTag) -> NodeError {
2312    NodeError::MalformedCommandPayload { tag: tag.label() }
2313}
2314
2315fn next_command_payload<T>(
2316    payloads: &mut impl Iterator<Item = T>,
2317    tag: CommandTag,
2318) -> Result<T, NodeError> {
2319    payloads.next().ok_or_else(|| command_payload_error(tag))
2320}
2321
2322fn update_typed_node<N: Node + 'static>(node: &mut dyn Node, id: NodeId) -> Result<(), NodeError> {
2323    let typed = node
2324        .as_any_mut()
2325        .downcast_mut::<N>()
2326        .ok_or(NodeError::TypeMismatch {
2327            id,
2328            expected: std::any::type_name::<N>(),
2329        })?;
2330    typed.update();
2331    Ok(())
2332}
2333
2334fn insert_child_with_reparenting(applier: &mut dyn Applier, parent_id: NodeId, child_id: NodeId) {
2335    if parent_id == child_id {
2336        debug_assert_ne!(
2337            parent_id, child_id,
2338            "a node cannot be attached as its own child"
2339        );
2340        return;
2341    }
2342
2343    let old_parent = applier
2344        .get_mut(child_id)
2345        .ok()
2346        .and_then(|node| node.parent());
2347    if let Some(old_parent_id) = old_parent {
2348        if old_parent_id != parent_id {
2349            if let Ok(old_parent_node) = applier.get_mut(old_parent_id) {
2350                old_parent_node.remove_child(child_id);
2351            }
2352            if let Ok(child_node) = applier.get_mut(child_id) {
2353                child_node.on_removed_from_parent();
2354            }
2355            bubble_layout_dirty(applier, old_parent_id);
2356            bubble_measure_dirty(applier, old_parent_id);
2357        }
2358    }
2359
2360    if let Ok(parent_node) = applier.get_mut(parent_id) {
2361        parent_node.insert_child(child_id);
2362    }
2363    if let Ok(child_node) = applier.get_mut(child_id) {
2364        child_node.on_attached_to_parent(parent_id);
2365    }
2366}
2367
2368fn apply_remove_child(
2369    applier: &mut dyn Applier,
2370    parent_id: NodeId,
2371    child_id: NodeId,
2372    deferred_cleanup: &mut DeferredChildCleanupQueue,
2373) -> Result<(), NodeError> {
2374    detach_child_from_parent(applier, parent_id, child_id)?;
2375
2376    let generation = applier.node_generation(child_id);
2377    let removed_from_parent = if let Ok(node) = applier.get_mut(child_id) {
2378        node.parent().is_none()
2379    } else {
2380        return Ok(());
2381    };
2382    deferred_cleanup.push(child_id, generation, removed_from_parent);
2383    Ok(())
2384}
2385
2386fn detach_child_from_parent(
2387    applier: &mut dyn Applier,
2388    parent_id: NodeId,
2389    child_id: NodeId,
2390) -> Result<(), NodeError> {
2391    if let Ok(parent_node) = applier.get_mut(parent_id) {
2392        parent_node.remove_child(child_id);
2393    }
2394    bubble_layout_dirty(applier, parent_id);
2395    bubble_measure_dirty(applier, parent_id);
2396
2397    if let Ok(node) = applier.get_mut(child_id) {
2398        match node.parent() {
2399            Some(existing_parent_id) if existing_parent_id == parent_id => {
2400                node.on_removed_from_parent();
2401            }
2402            None => {}
2403            Some(_) => return Ok(()),
2404        }
2405    } else {
2406        return Ok(());
2407    }
2408
2409    Ok(())
2410}
2411
2412fn cleanup_detached_child(
2413    applier: &mut dyn Applier,
2414    cleanup: DeferredChildCleanup,
2415) -> Result<(), NodeError> {
2416    if applier.node_generation(cleanup.child_id) != cleanup.generation {
2417        return Ok(());
2418    }
2419
2420    let parent_id = match applier.get_mut(cleanup.child_id) {
2421        Ok(node) => node.parent(),
2422        Err(NodeError::Missing { .. }) => return Ok(()),
2423        Err(err) => return Err(err),
2424    };
2425    if parent_id.is_some() {
2426        return Ok(());
2427    }
2428
2429    if let Ok(node) = applier.get_mut(cleanup.child_id) {
2430        if !cleanup.removed_from_parent {
2431            node.on_removed_from_parent();
2432        }
2433        node.unmount();
2434    }
2435    match applier.remove(cleanup.child_id) {
2436        Ok(()) | Err(NodeError::Missing { .. }) => Ok(()),
2437        Err(err) => Err(err),
2438    }
2439}
2440
2441fn remove_child_and_cleanup_now(
2442    applier: &mut dyn Applier,
2443    parent_id: NodeId,
2444    child_id: NodeId,
2445) -> Result<(), NodeError> {
2446    let mut deferred_cleanup = DeferredChildCleanupQueue::default();
2447    apply_remove_child(applier, parent_id, child_id, &mut deferred_cleanup)?;
2448    deferred_cleanup.flush(applier)
2449}
2450
2451fn collect_current_children(applier: &mut dyn Applier, parent_id: NodeId) -> ChildList {
2452    let mut scratch = SmallVec::<[NodeId; 8]>::new();
2453    if let Ok(node) = applier.get_mut(parent_id) {
2454        node.collect_children_into(&mut scratch);
2455    }
2456    let mut current = ChildList::new();
2457    current.extend(scratch);
2458    current
2459}
2460
2461fn sync_children(
2462    applier: &mut dyn Applier,
2463    parent_id: NodeId,
2464    expected_children: &[NodeId],
2465    deferred_cleanup: &mut DeferredChildCleanupQueue,
2466) -> Result<(), NodeError> {
2467    let mut current = collect_current_children(applier, parent_id);
2468    let children_changed = current.as_slice() != expected_children;
2469
2470    if children_changed {
2471        if current.len().max(expected_children.len()) <= SMALL_CHILD_SYNC_LINEAR_THRESHOLD {
2472            sync_children_small(
2473                applier,
2474                parent_id,
2475                &mut current,
2476                expected_children,
2477                deferred_cleanup,
2478            )?;
2479        } else {
2480            let mut target_positions: HashMap<NodeId, usize> = HashMap::default();
2481            target_positions.reserve(expected_children.len());
2482            for (index, &child) in expected_children.iter().enumerate() {
2483                target_positions.insert(child, index);
2484            }
2485
2486            for index in (0..current.len()).rev() {
2487                let child = current[index];
2488                if !target_positions.contains_key(&child) {
2489                    current.remove(index);
2490                    apply_remove_child(applier, parent_id, child, deferred_cleanup)?;
2491                }
2492            }
2493
2494            let mut current_positions = build_child_positions(&current);
2495            for (target_index, &child) in expected_children.iter().enumerate() {
2496                if let Some(current_index) = current_positions.get(&child).copied() {
2497                    if current_index != target_index {
2498                        let from_index = current_index;
2499                        let to_index = move_child_in_diff_state(
2500                            &mut current,
2501                            &mut current_positions,
2502                            from_index,
2503                            target_index,
2504                        );
2505                        Command::MoveChild {
2506                            parent_id,
2507                            from_index,
2508                            to_index,
2509                            bubble: DirtyBubble::LAYOUT_AND_MEASURE,
2510                        }
2511                        .apply(applier)?;
2512                    }
2513                } else {
2514                    let insert_index = target_index.min(current.len());
2515                    let appended_index = current.len();
2516                    insert_child_into_diff_state(
2517                        &mut current,
2518                        &mut current_positions,
2519                        insert_index,
2520                        child,
2521                    );
2522                    Command::InsertChild {
2523                        parent_id,
2524                        child_id: child,
2525                        appended_index,
2526                        insert_index,
2527                        bubble: DirtyBubble::LAYOUT_AND_MEASURE,
2528                    }
2529                    .apply(applier)?;
2530                }
2531            }
2532        }
2533    }
2534
2535    reconcile_children(applier, parent_id, expected_children, !children_changed)
2536}
2537
2538fn sync_children_small(
2539    applier: &mut dyn Applier,
2540    parent_id: NodeId,
2541    current: &mut ChildList,
2542    expected_children: &[NodeId],
2543    deferred_cleanup: &mut DeferredChildCleanupQueue,
2544) -> Result<(), NodeError> {
2545    for index in (0..current.len()).rev() {
2546        let child = current[index];
2547        if !expected_children.contains(&child) {
2548            current.remove(index);
2549            apply_remove_child(applier, parent_id, child, deferred_cleanup)?;
2550        }
2551    }
2552
2553    for (target_index, &child) in expected_children.iter().enumerate() {
2554        if let Some(current_index) = current
2555            .iter()
2556            .position(|&current_child| current_child == child)
2557        {
2558            if current_index != target_index {
2559                let child = current.remove(current_index);
2560                let to_index = target_index.min(current.len());
2561                current.insert(to_index, child);
2562                Command::MoveChild {
2563                    parent_id,
2564                    from_index: current_index,
2565                    to_index,
2566                    bubble: DirtyBubble::LAYOUT_AND_MEASURE,
2567                }
2568                .apply(applier)?;
2569            }
2570        } else {
2571            let insert_index = target_index.min(current.len());
2572            let appended_index = current.len();
2573            current.insert(insert_index, child);
2574            Command::InsertChild {
2575                parent_id,
2576                child_id: child,
2577                appended_index,
2578                insert_index,
2579                bubble: DirtyBubble::LAYOUT_AND_MEASURE,
2580            }
2581            .apply(applier)?;
2582        }
2583    }
2584
2585    Ok(())
2586}
2587
2588fn reconcile_children(
2589    applier: &mut dyn Applier,
2590    parent_id: NodeId,
2591    expected_children: &[NodeId],
2592    needs_dirty_check: bool,
2593) -> Result<(), NodeError> {
2594    let mut repaired = false;
2595    for &child_id in expected_children {
2596        let needs_attach = if let Ok(node) = applier.get_mut(child_id) {
2597            node.parent() != Some(parent_id)
2598        } else {
2599            false
2600        };
2601
2602        if needs_attach {
2603            insert_child_with_reparenting(applier, parent_id, child_id);
2604            repaired = true;
2605        }
2606    }
2607
2608    let is_dirty = if needs_dirty_check {
2609        if let Ok(node) = applier.get_mut(parent_id) {
2610            node.needs_layout()
2611        } else {
2612            false
2613        }
2614    } else {
2615        false
2616    };
2617
2618    if repaired {
2619        bubble_layout_dirty(applier, parent_id);
2620        bubble_measure_dirty(applier, parent_id);
2621    } else if is_dirty {
2622        bubble_layout_dirty(applier, parent_id);
2623    }
2624
2625    Ok(())
2626}
2627
2628#[derive(Default)]
2629pub struct MemoryApplier {
2630    nodes: Vec<Option<Box<dyn Node>>>,
2631    /// Stable node ID occupying each physical slot.
2632    physical_stable_ids: Vec<u32>,
2633    physical_warm_recycled_origins: Vec<bool>,
2634    /// Physical storage location for each live stable node ID.
2635    stable_to_physical: HashMap<NodeId, usize>,
2636    /// Generation counter per stable node ID.
2637    stable_generations: HashMap<NodeId, u32>,
2638    /// Lowest free physical slots are reused first so the dense storage stays compact.
2639    free_ids: BinaryHeap<Reverse<usize>>,
2640    /// Storage for high-ID nodes (like virtual nodes with IDs starting at 0xFFFFFFFF00000000)
2641    /// that can't be stored in the Vec without causing capacity overflow.
2642    high_id_nodes: HashMap<NodeId, Box<dyn Node>>,
2643    high_id_warm_recycled_origins: HashMap<NodeId, bool>,
2644    high_id_generations: HashMap<NodeId, u32>,
2645    next_stable_id: NodeId,
2646    layout_runtime: Option<RuntimeHandle>,
2647    slots: SlotTable,
2648    recycled_nodes: HashMap<TypeId, Vec<RecycledNode>>,
2649    returning_recycled_nodes: HashMap<TypeId, Vec<RecycledNode>>,
2650    cold_recycled_nodes: HashMap<TypeId, Vec<RecycledNode>>,
2651    recycled_node_limits: HashMap<TypeId, usize>,
2652    warm_recycled_node_targets: HashMap<TypeId, usize>,
2653    fresh_recyclable_creations: HashMap<TypeId, usize>,
2654    recycled_node_prototypes: HashMap<TypeId, Box<dyn Node>>,
2655}
2656
2657struct RemovalFrame {
2658    node_id: NodeId,
2659    children: SmallVec<[NodeId; 8]>,
2660    next_child: usize,
2661}
2662
2663#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
2664pub struct MemoryApplierDebugStats {
2665    pub next_stable_id: NodeId,
2666    pub nodes_len: usize,
2667    pub nodes_cap: usize,
2668    pub physical_stable_ids_len: usize,
2669    pub physical_stable_ids_cap: usize,
2670    pub stable_to_physical_len: usize,
2671    pub stable_to_physical_cap: usize,
2672    pub stable_generations_len: usize,
2673    pub stable_generations_cap: usize,
2674    pub free_ids_len: usize,
2675    pub free_ids_cap: usize,
2676    pub high_id_nodes_len: usize,
2677    pub high_id_nodes_cap: usize,
2678    pub high_id_generations_len: usize,
2679    pub high_id_generations_cap: usize,
2680    pub recycled_type_count: usize,
2681    pub recycled_type_cap: usize,
2682    pub recycled_node_count: usize,
2683    pub recycled_node_capacity: usize,
2684    pub warm_recycled_node_id_count: usize,
2685    pub warm_recycled_node_id_capacity: usize,
2686}
2687
2688impl MemoryApplier {
2689    const EAGER_COMPACT_NODE_LEN: usize = 1_024;
2690    const HIGH_ID_THRESHOLD: NodeId = 1_000_000_000;
2691    const INVALID_STABLE_ID: u32 = u32::MAX;
2692    const INITIAL_DENSE_NODE_CAP: usize = 32;
2693    const LARGE_DENSE_NODE_GROWTH_THRESHOLD: usize = 32 * 1024;
2694    const LARGE_DENSE_NODE_GROWTH_DIVISOR: usize = 4;
2695
2696    fn pack_stable_id(stable_id: NodeId) -> u32 {
2697        u32::try_from(stable_id).expect("stable id overflow")
2698    }
2699
2700    fn unpack_stable_id(stable_id: u32) -> NodeId {
2701        stable_id as NodeId
2702    }
2703
2704    fn next_dense_node_target_len(old_len: usize) -> usize {
2705        if old_len < Self::INITIAL_DENSE_NODE_CAP {
2706            return Self::INITIAL_DENSE_NODE_CAP;
2707        }
2708        if old_len < Self::LARGE_DENSE_NODE_GROWTH_THRESHOLD {
2709            return old_len.saturating_mul(2);
2710        }
2711
2712        let incremental_growth =
2713            (old_len / Self::LARGE_DENSE_NODE_GROWTH_DIVISOR).max(Self::INITIAL_DENSE_NODE_CAP);
2714        old_len.saturating_add(incremental_growth)
2715    }
2716
2717    fn ensure_dense_node_storage_capacity(&mut self) {
2718        let len = self
2719            .nodes
2720            .len()
2721            .max(self.physical_stable_ids.len())
2722            .max(self.physical_warm_recycled_origins.len());
2723        if len < self.nodes.capacity()
2724            && len < self.physical_stable_ids.capacity()
2725            && len < self.physical_warm_recycled_origins.capacity()
2726        {
2727            return;
2728        }
2729
2730        let target = Self::next_dense_node_target_len(len);
2731        if self.nodes.capacity() < target {
2732            self.nodes
2733                .reserve_exact(target.saturating_sub(self.nodes.len()));
2734        }
2735        if self.physical_stable_ids.capacity() < target {
2736            self.physical_stable_ids
2737                .reserve_exact(target.saturating_sub(self.physical_stable_ids.len()));
2738        }
2739        if self.physical_warm_recycled_origins.capacity() < target {
2740            self.physical_warm_recycled_origins
2741                .reserve_exact(target.saturating_sub(self.physical_warm_recycled_origins.len()));
2742        }
2743    }
2744
2745    fn ensure_stable_index_capacity(&mut self) {
2746        let len = self
2747            .stable_to_physical
2748            .len()
2749            .max(self.stable_generations.len());
2750        if len < self.stable_to_physical.capacity() && len < self.stable_generations.capacity() {
2751            return;
2752        }
2753
2754        let target = Self::next_dense_node_target_len(len);
2755        let additional = target.saturating_sub(len);
2756        if self.stable_to_physical.capacity() < target {
2757            self.stable_to_physical.reserve(additional);
2758        }
2759        if self.stable_generations.capacity() < target {
2760            self.stable_generations.reserve(additional);
2761        }
2762    }
2763
2764    pub fn new() -> Self {
2765        Self {
2766            nodes: Vec::new(),
2767            physical_stable_ids: Vec::new(),
2768            physical_warm_recycled_origins: Vec::new(),
2769            stable_to_physical: HashMap::default(),
2770            stable_generations: HashMap::default(),
2771            free_ids: BinaryHeap::new(),
2772            high_id_nodes: HashMap::default(),
2773            high_id_warm_recycled_origins: HashMap::default(),
2774            high_id_generations: HashMap::default(),
2775            next_stable_id: 0,
2776            layout_runtime: None,
2777            slots: SlotTable::default(),
2778            recycled_nodes: HashMap::default(),
2779            returning_recycled_nodes: HashMap::default(),
2780            cold_recycled_nodes: HashMap::default(),
2781            recycled_node_limits: HashMap::default(),
2782            warm_recycled_node_targets: HashMap::default(),
2783            fresh_recyclable_creations: HashMap::default(),
2784            recycled_node_prototypes: HashMap::default(),
2785        }
2786    }
2787
2788    pub fn slots(&mut self) -> &mut SlotTable {
2789        &mut self.slots
2790    }
2791
2792    pub fn with_node<N: Node + 'static, R>(
2793        &mut self,
2794        id: NodeId,
2795        f: impl FnOnce(&mut N) -> R,
2796    ) -> Result<R, NodeError> {
2797        let physical_id = self
2798            .resolve_node_index(id)
2799            .ok_or(NodeError::Missing { id })?;
2800        let slot = self
2801            .nodes
2802            .get_mut(physical_id)
2803            .ok_or(NodeError::Missing { id })?
2804            .as_deref_mut()
2805            .ok_or(NodeError::Missing { id })?;
2806        let typed = slot
2807            .as_any_mut()
2808            .downcast_mut::<N>()
2809            .ok_or(NodeError::TypeMismatch {
2810                id,
2811                expected: std::any::type_name::<N>(),
2812            })?;
2813        Ok(f(typed))
2814    }
2815
2816    pub fn len(&self) -> usize {
2817        self.nodes.iter().filter(|n| n.is_some()).count()
2818    }
2819
2820    pub fn capacity(&self) -> usize {
2821        self.nodes.len()
2822    }
2823
2824    pub fn tombstone_count(&self) -> usize {
2825        self.nodes.iter().filter(|n| n.is_none()).count()
2826    }
2827
2828    pub fn freelist_len(&self) -> usize {
2829        self.free_ids.len()
2830    }
2831
2832    pub fn debug_recycled_node_count(&self) -> usize {
2833        self.total_recycled_node_count()
2834    }
2835
2836    pub fn debug_recycled_node_count_for<N: Node + 'static>(&self) -> usize {
2837        let key = TypeId::of::<N>();
2838        self.recycled_nodes.get(&key).map(Vec::len).unwrap_or(0)
2839            + self
2840                .returning_recycled_nodes
2841                .get(&key)
2842                .map(Vec::len)
2843                .unwrap_or(0)
2844            + self
2845                .cold_recycled_nodes
2846                .get(&key)
2847                .map(Vec::len)
2848                .unwrap_or(0)
2849    }
2850
2851    pub fn debug_stats(&self) -> MemoryApplierDebugStats {
2852        let mut recycled_keys: HashSet<TypeId> = HashSet::default();
2853        recycled_keys.extend(self.recycled_nodes.keys().copied());
2854        recycled_keys.extend(self.returning_recycled_nodes.keys().copied());
2855        recycled_keys.extend(self.cold_recycled_nodes.keys().copied());
2856
2857        MemoryApplierDebugStats {
2858            next_stable_id: self.next_stable_id,
2859            nodes_len: self.len(),
2860            nodes_cap: self.nodes.len(),
2861            physical_stable_ids_len: self.physical_stable_ids.len(),
2862            physical_stable_ids_cap: self.physical_stable_ids.capacity(),
2863            stable_to_physical_len: self.stable_to_physical.len(),
2864            stable_to_physical_cap: self.stable_to_physical.capacity(),
2865            stable_generations_len: self.stable_generations.len(),
2866            stable_generations_cap: self.stable_generations.capacity(),
2867            free_ids_len: self.free_ids.len(),
2868            free_ids_cap: self.free_ids.capacity(),
2869            high_id_nodes_len: self.high_id_nodes.len(),
2870            high_id_nodes_cap: self.high_id_nodes.capacity(),
2871            high_id_generations_len: self.high_id_generations.len(),
2872            high_id_generations_cap: self.high_id_generations.capacity(),
2873            recycled_type_count: recycled_keys.len(),
2874            recycled_type_cap: self.recycled_nodes.capacity()
2875                + self.returning_recycled_nodes.capacity()
2876                + self.cold_recycled_nodes.capacity(),
2877            recycled_node_count: self.total_recycled_node_count(),
2878            recycled_node_capacity: self.total_recycled_node_capacity(),
2879            warm_recycled_node_id_count: self.total_warm_recycled_node_id_count(),
2880            warm_recycled_node_id_capacity: self.total_warm_recycled_node_id_capacity(),
2881        }
2882    }
2883
2884    pub fn is_empty(&self) -> bool {
2885        self.len() == 0
2886    }
2887
2888    pub fn debug_live_node_heap_bytes(&self) -> usize {
2889        let dense_nodes = self
2890            .nodes
2891            .iter()
2892            .flatten()
2893            .map(|node| std::mem::size_of_val(&**node) + node.debug_heap_bytes())
2894            .sum::<usize>();
2895        let high_id_nodes = self
2896            .high_id_nodes
2897            .values()
2898            .map(|node| std::mem::size_of_val(&**node) + node.debug_heap_bytes())
2899            .sum::<usize>();
2900        dense_nodes + high_id_nodes
2901    }
2902
2903    pub fn debug_recycled_node_heap_bytes(&self) -> usize {
2904        let pool_bytes = |pools: &HashMap<TypeId, Vec<RecycledNode>>| {
2905            pools
2906                .values()
2907                .flat_map(|nodes| nodes.iter())
2908                .map(|node| std::mem::size_of_val(&*node.node) + node.node.debug_heap_bytes())
2909                .sum::<usize>()
2910        };
2911
2912        pool_bytes(&self.recycled_nodes)
2913            + pool_bytes(&self.returning_recycled_nodes)
2914            + pool_bytes(&self.cold_recycled_nodes)
2915    }
2916
2917    pub fn set_runtime_handle(&mut self, handle: RuntimeHandle) {
2918        self.layout_runtime = Some(handle);
2919    }
2920
2921    pub fn clear_runtime_handle(&mut self) {
2922        self.layout_runtime = None;
2923    }
2924
2925    pub fn runtime_handle(&self) -> Option<RuntimeHandle> {
2926        self.layout_runtime.clone()
2927    }
2928
2929    fn pool_node_count(pools: &HashMap<TypeId, Vec<RecycledNode>>) -> usize {
2930        pools.values().map(Vec::len).sum()
2931    }
2932
2933    fn pool_node_capacity(pools: &HashMap<TypeId, Vec<RecycledNode>>) -> usize {
2934        pools.values().map(Vec::capacity).sum()
2935    }
2936
2937    fn total_recycled_node_count(&self) -> usize {
2938        Self::pool_node_count(&self.recycled_nodes)
2939            + Self::pool_node_count(&self.returning_recycled_nodes)
2940            + Self::pool_node_count(&self.cold_recycled_nodes)
2941    }
2942
2943    fn total_recycled_node_capacity(&self) -> usize {
2944        Self::pool_node_capacity(&self.recycled_nodes)
2945            + Self::pool_node_capacity(&self.returning_recycled_nodes)
2946            + Self::pool_node_capacity(&self.cold_recycled_nodes)
2947    }
2948
2949    fn total_warm_recycled_node_id_count(&self) -> usize {
2950        self.live_warm_recycled_origin_count()
2951            + Self::pool_node_count(&self.recycled_nodes)
2952            + Self::pool_node_count(&self.returning_recycled_nodes)
2953    }
2954
2955    fn total_warm_recycled_node_id_capacity(&self) -> usize {
2956        self.live_warm_recycled_origin_capacity()
2957            + Self::pool_node_capacity(&self.recycled_nodes)
2958            + Self::pool_node_capacity(&self.returning_recycled_nodes)
2959    }
2960
2961    fn remember_recycle_pool_limit(&mut self, key: TypeId, recycle_pool_limit: Option<usize>) {
2962        if let Some(limit) = recycle_pool_limit {
2963            self.recycled_node_limits.insert(key, limit);
2964        } else {
2965            self.recycled_node_limits.remove(&key);
2966        }
2967    }
2968
2969    fn recycle_pool_limit_for(&self, key: TypeId) -> Option<usize> {
2970        self.recycled_node_limits.get(&key).copied()
2971    }
2972
2973    fn warm_recycled_pool_len(&self, key: TypeId) -> usize {
2974        self.recycled_nodes.get(&key).map(Vec::len).unwrap_or(0)
2975    }
2976
2977    fn warm_recycled_node_target(&self, key: TypeId) -> usize {
2978        self.warm_recycled_node_targets
2979            .get(&key)
2980            .copied()
2981            .unwrap_or(0)
2982    }
2983
2984    fn warm_recycled_node_target_limit(&self, key: TypeId) -> usize {
2985        let Some(limit) = self.recycle_pool_limit_for(key) else {
2986            return usize::MAX;
2987        };
2988        if limit <= 8 {
2989            limit
2990        } else {
2991            limit / 4
2992        }
2993    }
2994
2995    fn update_warm_recycled_node_target(&mut self, key: TypeId, observed_demand: usize) -> usize {
2996        let target_limit = self.warm_recycled_node_target_limit(key);
2997        let existing = self.warm_recycled_node_target(key).min(target_limit);
2998        if observed_demand == 0 {
2999            return existing;
3000        }
3001
3002        let target = match self.recycle_pool_limit_for(key) {
3003            Some(limit) if limit > 8 => target_limit,
3004            Some(_) => observed_demand.min(target_limit),
3005            None => observed_demand,
3006        };
3007        self.warm_recycled_node_targets.insert(key, target);
3008        target
3009    }
3010
3011    fn remember_recycled_node_prototype(&mut self, key: TypeId, shell: &dyn Node) {
3012        if self.recycled_node_prototypes.contains_key(&key) {
3013            return;
3014        }
3015        if let Some(prototype) = shell.rehouse_for_recycle() {
3016            self.recycled_node_prototypes.insert(key, prototype);
3017        }
3018    }
3019
3020    fn live_warm_recycled_origin_count(&self) -> usize {
3021        self.physical_warm_recycled_origins
3022            .iter()
3023            .zip(self.nodes.iter())
3024            .filter(|(warm_origin, node)| **warm_origin && node.is_some())
3025            .count()
3026            + self
3027                .high_id_warm_recycled_origins
3028                .values()
3029                .filter(|warm_origin| **warm_origin)
3030                .count()
3031    }
3032
3033    fn live_warm_recycled_origin_capacity(&self) -> usize {
3034        self.physical_warm_recycled_origins.capacity()
3035            + self.high_id_warm_recycled_origins.capacity()
3036    }
3037
3038    fn push_recycled_node(
3039        &mut self,
3040        key: TypeId,
3041        recycle_pool_limit: Option<usize>,
3042        recycled: RecycledNode,
3043    ) {
3044        self.remember_recycle_pool_limit(key, recycle_pool_limit);
3045        self.remember_recycled_node_prototype(key, recycled.node.as_ref());
3046
3047        let warm_origin = recycled.warm_origin();
3048        let pool = if warm_origin {
3049            self.returning_recycled_nodes.entry(key).or_default()
3050        } else {
3051            self.cold_recycled_nodes.entry(key).or_default()
3052        };
3053        pool.push(recycled);
3054        if let Some(limit) = recycle_pool_limit {
3055            if pool.len() > limit {
3056                let excess = pool.len() - limit;
3057                let dropped: Vec<_> = pool.drain(0..excess).collect();
3058                drop(dropped);
3059            }
3060        }
3061    }
3062
3063    fn push_warm_recycled_node(
3064        &mut self,
3065        key: TypeId,
3066        recycle_pool_limit: Option<usize>,
3067        mut recycled: RecycledNode,
3068    ) {
3069        self.remember_recycle_pool_limit(key, recycle_pool_limit);
3070
3071        recycled.set_warm_origin(true);
3072        let mut dropped = Vec::new();
3073        let mut remove_pool_entry = false;
3074        {
3075            let pool = self.recycled_nodes.entry(key).or_default();
3076            pool.push(recycled);
3077            if let Some(limit) = recycle_pool_limit {
3078                if pool.len() > limit {
3079                    let excess = pool.len() - limit;
3080                    dropped = pool.drain(0..excess).collect();
3081                    remove_pool_entry = pool.is_empty();
3082                }
3083            }
3084        }
3085        if remove_pool_entry {
3086            self.recycled_nodes.remove(&key);
3087        }
3088        drop(dropped);
3089    }
3090
3091    fn seed_recycled_node_shell_impl(
3092        &mut self,
3093        key: TypeId,
3094        recycle_pool_limit: Option<usize>,
3095        shell: Box<dyn Node>,
3096    ) {
3097        let limit = recycle_pool_limit.unwrap_or(usize::MAX);
3098        if self.warm_recycled_pool_len(key) >= limit {
3099            return;
3100        }
3101
3102        self.remember_recycled_node_prototype(key, shell.as_ref());
3103        let stable_id = self.next_stable_id;
3104        self.next_stable_id = self.next_stable_id.saturating_add(1);
3105        self.push_warm_recycled_node(
3106            key,
3107            recycle_pool_limit,
3108            RecycledNode::from_shell(stable_id, shell, true),
3109        );
3110    }
3111
3112    fn take_recycled_node_from_pool(
3113        pools: &mut HashMap<TypeId, Vec<RecycledNode>>,
3114        key: TypeId,
3115    ) -> Option<RecycledNode> {
3116        let pool = pools.get_mut(&key)?;
3117        let node = pool.pop();
3118        if pool.is_empty() {
3119            pools.remove(&key);
3120        }
3121        node
3122    }
3123
3124    fn compact_idle_warm_pool(&mut self, key: TypeId) {
3125        let Some(pool) = self.recycled_nodes.get_mut(&key) else {
3126            return;
3127        };
3128        if pool.capacity() <= pool.len().saturating_mul(4).max(64) {
3129            return;
3130        }
3131
3132        let retained = pool.len();
3133        let mut compacted = Vec::with_capacity(retained);
3134        compacted.append(pool);
3135        let remove_pool_entry = compacted.is_empty();
3136        *pool = compacted;
3137        let _ = pool;
3138
3139        if remove_pool_entry {
3140            self.recycled_nodes.remove(&key);
3141        }
3142    }
3143
3144    fn trim_idle_warm_pool_to_target(&mut self, key: TypeId, target: usize) {
3145        let pool_len = self.warm_recycled_pool_len(key);
3146        if pool_len <= target {
3147            return;
3148        }
3149
3150        let Some(pool) = self.recycled_nodes.get_mut(&key) else {
3151            return;
3152        };
3153        let removable = (pool_len - target).min(pool.len());
3154        let dropped: Vec<_> = pool.drain(0..removable).collect();
3155        let remove_pool_entry = pool.is_empty();
3156        let _ = pool;
3157
3158        if remove_pool_entry {
3159            self.recycled_nodes.remove(&key);
3160        }
3161        drop(dropped);
3162    }
3163
3164    fn replenish_warm_pool_to_target(&mut self, key: TypeId, target: usize) {
3165        let missing = target.saturating_sub(self.warm_recycled_pool_len(key));
3166        if missing == 0 {
3167            return;
3168        }
3169
3170        let recycle_pool_limit = self.recycle_pool_limit_for(key);
3171        let mut shells = Vec::with_capacity(missing);
3172        if let Some(prototype) = self.recycled_node_prototypes.get(&key) {
3173            for _ in 0..missing {
3174                let Some(shell) = prototype.rehouse_for_recycle() else {
3175                    break;
3176                };
3177                shells.push(shell);
3178            }
3179        }
3180
3181        for shell in shells {
3182            self.seed_recycled_node_shell_impl(key, recycle_pool_limit, shell);
3183        }
3184    }
3185
3186    fn prune_stable_generations(&mut self) {
3187        let retained_len = self.stable_to_physical.len() + self.total_recycled_node_count();
3188        if retained_len == self.stable_generations.len() {
3189            return;
3190        }
3191
3192        let mut retained = HashMap::default();
3193        retained.reserve(retained_len);
3194        for stable_id in self.stable_to_physical.keys().copied() {
3195            if let Some(generation) = self.stable_generations.get(&stable_id).copied() {
3196                retained.insert(stable_id, generation);
3197            }
3198        }
3199        for stable_id in self
3200            .recycled_nodes
3201            .values()
3202            .flat_map(|nodes| nodes.iter().map(RecycledNode::stable_id))
3203        {
3204            if let Some(generation) = self.stable_generations.get(&stable_id).copied() {
3205                retained.insert(stable_id, generation);
3206            }
3207        }
3208        for stable_id in self
3209            .returning_recycled_nodes
3210            .values()
3211            .flat_map(|nodes| nodes.iter().map(RecycledNode::stable_id))
3212        {
3213            if let Some(generation) = self.stable_generations.get(&stable_id).copied() {
3214                retained.insert(stable_id, generation);
3215            }
3216        }
3217        for stable_id in self
3218            .cold_recycled_nodes
3219            .values()
3220            .flat_map(|nodes| nodes.iter().map(RecycledNode::stable_id))
3221        {
3222            if let Some(generation) = self.stable_generations.get(&stable_id).copied() {
3223                retained.insert(stable_id, generation);
3224            }
3225        }
3226        self.stable_generations = retained;
3227    }
3228
3229    pub fn dump_tree(&self, root: Option<NodeId>) -> String {
3230        let mut output = String::new();
3231        if let Some(root_id) = root {
3232            self.dump_node(&mut output, root_id, 0);
3233        } else {
3234            output.push_str("(no root)\n");
3235        }
3236        output
3237    }
3238
3239    fn dump_node(&self, output: &mut String, id: NodeId, depth: usize) {
3240        let indent = "  ".repeat(depth);
3241        if let Some(physical_id) = self.resolve_node_index(id) {
3242            if let Some(node) = self.nodes.get(physical_id).and_then(Option::as_ref) {
3243                let type_name = std::any::type_name_of_val(&**node);
3244                output.push_str(&format!("{}[{}] {}\n", indent, id, type_name));
3245
3246                let children = node.children();
3247                for child_id in children {
3248                    self.dump_node(output, child_id, depth + 1);
3249                }
3250            } else {
3251                output.push_str(&format!(
3252                    "{}[{}] (missing physical node {})\n",
3253                    indent, id, physical_id
3254                ));
3255            }
3256        } else {
3257            output.push_str(&format!("{}[{}] (missing)\n", indent, id));
3258        }
3259    }
3260
3261    fn resolve_node_index(&self, id: NodeId) -> Option<usize> {
3262        self.stable_to_physical.get(&id).copied()
3263    }
3264
3265    fn contains_node_id(&self, id: NodeId) -> bool {
3266        self.resolve_node_index(id).is_some() || self.high_id_nodes.contains_key(&id)
3267    }
3268
3269    fn insert_high_id_node(&mut self, stable_id: NodeId, node: Box<dyn Node>, warm_origin: bool) {
3270        self.high_id_nodes.insert(stable_id, node);
3271        self.high_id_warm_recycled_origins
3272            .insert(stable_id, warm_origin);
3273        self.high_id_generations.entry(stable_id).or_insert(0);
3274    }
3275
3276    fn insert_available_with_id(&mut self, stable_id: NodeId, node: Box<dyn Node>) {
3277        if stable_id >= Self::HIGH_ID_THRESHOLD {
3278            self.insert_high_id_node(stable_id, node, false);
3279            return;
3280        }
3281
3282        let physical_id = if let Some(Reverse(free_physical_id)) = self.free_ids.pop() {
3283            self.nodes[free_physical_id] = Some(node);
3284            self.physical_stable_ids[free_physical_id] = Self::pack_stable_id(stable_id);
3285            self.physical_warm_recycled_origins[free_physical_id] = false;
3286            free_physical_id
3287        } else {
3288            self.ensure_dense_node_storage_capacity();
3289            let physical_id = self.nodes.len();
3290            self.nodes.push(Some(node));
3291            self.physical_stable_ids
3292                .push(Self::pack_stable_id(stable_id));
3293            self.physical_warm_recycled_origins.push(false);
3294            physical_id
3295        };
3296
3297        self.next_stable_id = self.next_stable_id.max(stable_id.saturating_add(1));
3298        self.ensure_stable_index_capacity();
3299        self.stable_generations.entry(stable_id).or_insert(0);
3300        self.physical_stable_ids[physical_id] = Self::pack_stable_id(stable_id);
3301        self.stable_to_physical.insert(stable_id, physical_id);
3302    }
3303
3304    fn get_ref(&self, id: NodeId) -> Result<&dyn Node, NodeError> {
3305        if let Some(physical_id) = self.resolve_node_index(id) {
3306            let slot = self
3307                .nodes
3308                .get(physical_id)
3309                .ok_or(NodeError::Missing { id })?
3310                .as_deref()
3311                .ok_or(NodeError::Missing { id })?;
3312            return Ok(slot);
3313        }
3314
3315        self.high_id_nodes
3316            .get(&id)
3317            .map(|node| node.as_ref())
3318            .ok_or(NodeError::Missing { id })
3319    }
3320
3321    fn collect_node_children_into(
3322        &self,
3323        id: NodeId,
3324        out: &mut SmallVec<[NodeId; 8]>,
3325    ) -> Result<(), NodeError> {
3326        self.get_ref(id)?.collect_children_into(out);
3327        Ok(())
3328    }
3329
3330    fn node_parent(&self, id: NodeId) -> Result<Option<NodeId>, NodeError> {
3331        Ok(self.get_ref(id)?.parent())
3332    }
3333
3334    fn collect_owned_children(
3335        &self,
3336        node_id: NodeId,
3337        out: &mut SmallVec<[NodeId; 8]>,
3338    ) -> Result<(), NodeError> {
3339        self.collect_node_children_into(node_id, out)?;
3340        out.retain(|child_id| {
3341            self.node_parent(*child_id)
3342                .map(|parent| parent == Some(node_id))
3343                .unwrap_or(false)
3344        });
3345        Ok(())
3346    }
3347
3348    fn remove_node_storage(&mut self, node_id: NodeId) -> Result<(), NodeError> {
3349        if self.high_id_nodes.contains_key(&node_id) {
3350            if let Some(mut node) = self.high_id_nodes.remove(&node_id) {
3351                if let Some(key) = node.recycle_key() {
3352                    let recycle_pool_limit = node.recycle_pool_limit();
3353                    let warm_origin = self
3354                        .high_id_warm_recycled_origins
3355                        .remove(&node_id)
3356                        .unwrap_or(false);
3357                    node.prepare_for_recycle();
3358                    self.push_recycled_node(
3359                        key,
3360                        recycle_pool_limit,
3361                        RecycledNode::new(node_id, node, warm_origin),
3362                    );
3363                }
3364            }
3365            let generation = self.high_id_generations.entry(node_id).or_insert(0);
3366            *generation = generation.wrapping_add(1);
3367            return Ok(());
3368        }
3369
3370        let physical_id = self
3371            .resolve_node_index(node_id)
3372            .ok_or(NodeError::Missing { id: node_id })?;
3373        if let Some(mut node) = self.nodes[physical_id].take() {
3374            if let Some(key) = node.recycle_key() {
3375                let recycle_pool_limit = node.recycle_pool_limit();
3376                let warm_origin = self
3377                    .physical_warm_recycled_origins
3378                    .get_mut(physical_id)
3379                    .map(std::mem::take)
3380                    .unwrap_or(false);
3381                node.prepare_for_recycle();
3382                self.push_recycled_node(
3383                    key,
3384                    recycle_pool_limit,
3385                    RecycledNode::new(node_id, node, warm_origin),
3386                );
3387            }
3388        }
3389        self.physical_stable_ids[physical_id] = Self::INVALID_STABLE_ID;
3390        self.stable_to_physical.remove(&node_id);
3391        if let Some(generation) = self.stable_generations.get_mut(&node_id) {
3392            *generation = generation.wrapping_add(1);
3393        } else {
3394            self.stable_generations.insert(node_id, 1);
3395        }
3396        self.free_ids.push(Reverse(physical_id));
3397        Ok(())
3398    }
3399
3400    fn remove_subtree_postorder(&mut self, id: NodeId) -> Result<usize, NodeError> {
3401        self.get_ref(id)?;
3402
3403        let mut root_children = SmallVec::<[NodeId; 8]>::new();
3404        self.collect_owned_children(id, &mut root_children)?;
3405
3406        let mut stack = Vec::new();
3407        stack.push(RemovalFrame {
3408            node_id: id,
3409            children: root_children,
3410            next_child: 0,
3411        });
3412        let mut max_depth = stack.len();
3413
3414        while let Some(frame) = stack.last_mut() {
3415            if frame.next_child < frame.children.len() {
3416                let child_id = frame.children[frame.next_child];
3417                frame.next_child += 1;
3418
3419                if let Ok(child) = self.get_mut(child_id) {
3420                    child.on_removed_from_parent();
3421                    child.unmount();
3422                }
3423
3424                let mut child_children = SmallVec::<[NodeId; 8]>::new();
3425                self.collect_owned_children(child_id, &mut child_children)?;
3426                stack.push(RemovalFrame {
3427                    node_id: child_id,
3428                    children: child_children,
3429                    next_child: 0,
3430                });
3431                max_depth = max_depth.max(stack.len());
3432                continue;
3433            }
3434
3435            let node_id = frame.node_id;
3436            stack.pop();
3437            self.remove_node_storage(node_id)?;
3438        }
3439
3440        Ok(max_depth)
3441    }
3442
3443    #[cfg(test)]
3444    fn debug_remove_max_traversal_depth(&mut self, id: NodeId) -> Result<usize, NodeError> {
3445        self.remove_subtree_postorder(id)
3446    }
3447}
3448
3449impl Applier for MemoryApplier {
3450    fn create(&mut self, node: Box<dyn Node>) -> NodeId {
3451        let stable_id = self.next_stable_id;
3452        self.next_stable_id = self.next_stable_id.saturating_add(1);
3453        if stable_id >= Self::HIGH_ID_THRESHOLD {
3454            self.insert_high_id_node(stable_id, node, false);
3455            return stable_id;
3456        }
3457
3458        self.ensure_stable_index_capacity();
3459        self.stable_generations.insert(stable_id, 0);
3460
3461        let physical_id = if let Some(Reverse(id)) = self.free_ids.pop() {
3462            debug_assert!(self.nodes[id].is_none(), "freelist entry {id} is not None");
3463            self.nodes[id] = Some(node);
3464            self.physical_stable_ids[id] = Self::pack_stable_id(stable_id);
3465            self.physical_warm_recycled_origins[id] = false;
3466            id
3467        } else {
3468            self.ensure_dense_node_storage_capacity();
3469            let id = self.nodes.len();
3470            self.nodes.push(Some(node));
3471            self.physical_stable_ids
3472                .push(Self::pack_stable_id(stable_id));
3473            self.physical_warm_recycled_origins.push(false);
3474            id
3475        };
3476        self.stable_to_physical.insert(stable_id, physical_id);
3477        stable_id
3478    }
3479
3480    fn node_generation(&self, id: NodeId) -> u32 {
3481        self.high_id_generations
3482            .get(&id)
3483            .copied()
3484            .or_else(|| self.stable_generations.get(&id).copied())
3485            .unwrap_or(0)
3486    }
3487
3488    fn get_mut(&mut self, id: NodeId) -> Result<&mut dyn Node, NodeError> {
3489        // Try Vec first (common case for normal nodes), then HashMap for virtual nodes.
3490        if let Some(physical_id) = self.resolve_node_index(id) {
3491            let slot = self.nodes[physical_id]
3492                .as_deref_mut()
3493                .ok_or(NodeError::Missing { id })?;
3494            return Ok(slot);
3495        }
3496        self.high_id_nodes
3497            .get_mut(&id)
3498            .map(|n| n.as_mut())
3499            .ok_or(NodeError::Missing { id })
3500    }
3501
3502    fn remove(&mut self, id: NodeId) -> Result<(), NodeError> {
3503        self.remove_subtree_postorder(id).map(|_| ())
3504    }
3505
3506    fn insert_with_id(&mut self, id: NodeId, node: Box<dyn Node>) -> Result<(), NodeError> {
3507        if self.contains_node_id(id) {
3508            return Err(NodeError::AlreadyExists { id });
3509        }
3510        self.insert_available_with_id(id, node);
3511        Ok(())
3512    }
3513
3514    fn insert_recycled_node_or_create(
3515        &mut self,
3516        stable_id: NodeId,
3517        node: Box<dyn Node>,
3518    ) -> RecycledNodeInsertion {
3519        if self.contains_node_id(stable_id) {
3520            let id = self.create(node);
3521            return RecycledNodeInsertion::fresh(
3522                id,
3523                Some(NodeError::AlreadyExists { id: stable_id }),
3524            );
3525        }
3526
3527        self.insert_available_with_id(stable_id, node);
3528        RecycledNodeInsertion::reused(stable_id)
3529    }
3530
3531    fn compact(&mut self) {
3532        let live_count = self.nodes.iter().filter(|slot| slot.is_some()).count();
3533        let tombstone_count = self.nodes.len().saturating_sub(live_count);
3534        if tombstone_count == 0 {
3535            return;
3536        }
3537        if self.nodes.len() > Self::EAGER_COMPACT_NODE_LEN && tombstone_count < live_count {
3538            return;
3539        }
3540        let rehouse_live_nodes = tombstone_count >= live_count;
3541        let mut packed_nodes = Vec::with_capacity(live_count);
3542        let mut packed_physical_stable_ids = Vec::with_capacity(live_count);
3543        let mut packed_warm_recycled_origins = Vec::with_capacity(live_count);
3544        let mut stable_to_physical = HashMap::default();
3545        stable_to_physical.reserve(live_count);
3546
3547        for physical_id in 0..self.nodes.len() {
3548            let Some(mut node) = self.nodes[physical_id].take() else {
3549                continue;
3550            };
3551            if rehouse_live_nodes {
3552                if let Some(rehoused) = node.rehouse_for_live_compaction() {
3553                    node = rehoused;
3554                }
3555            }
3556            let stable_id = std::mem::replace(
3557                &mut self.physical_stable_ids[physical_id],
3558                Self::INVALID_STABLE_ID,
3559            );
3560            debug_assert_ne!(
3561                stable_id,
3562                Self::INVALID_STABLE_ID,
3563                "live physical slot must have a stable id",
3564            );
3565            let stable_id = Self::unpack_stable_id(stable_id);
3566            packed_nodes.push(Some(node));
3567            packed_physical_stable_ids.push(Self::pack_stable_id(stable_id));
3568            packed_warm_recycled_origins.push(self.physical_warm_recycled_origins[physical_id]);
3569            stable_to_physical.insert(stable_id, packed_nodes.len() - 1);
3570        }
3571
3572        self.nodes = packed_nodes;
3573        self.physical_stable_ids = packed_physical_stable_ids;
3574        self.physical_warm_recycled_origins = packed_warm_recycled_origins;
3575        self.free_ids = BinaryHeap::new();
3576        self.stable_to_physical = stable_to_physical;
3577        self.prune_stable_generations();
3578    }
3579
3580    fn take_recycled_node(&mut self, key: TypeId) -> Option<RecycledNode> {
3581        Self::take_recycled_node_from_pool(&mut self.returning_recycled_nodes, key)
3582            .or_else(|| Self::take_recycled_node_from_pool(&mut self.recycled_nodes, key))
3583    }
3584
3585    fn set_recycled_node_origin(&mut self, id: NodeId, warm_origin: bool) {
3586        if let Some(physical_id) = self.resolve_node_index(id) {
3587            self.physical_warm_recycled_origins[physical_id] = warm_origin;
3588        } else if self.high_id_nodes.contains_key(&id) {
3589            self.high_id_warm_recycled_origins.insert(id, warm_origin);
3590        }
3591    }
3592
3593    fn seed_recycled_node_shell(
3594        &mut self,
3595        key: TypeId,
3596        recycle_pool_limit: Option<usize>,
3597        shell: Box<dyn Node>,
3598    ) {
3599        self.seed_recycled_node_shell_impl(key, recycle_pool_limit, shell);
3600    }
3601
3602    fn record_fresh_recyclable_creation(&mut self, key: TypeId) {
3603        *self.fresh_recyclable_creations.entry(key).or_insert(0) += 1;
3604    }
3605
3606    fn clear_recycled_nodes(&mut self) {
3607        let returning = std::mem::take(&mut self.returning_recycled_nodes);
3608        for (key, mut nodes) in returning {
3609            let pool = self.recycled_nodes.entry(key).or_default();
3610            pool.append(&mut nodes);
3611        }
3612
3613        let fresh_recyclable_creations = std::mem::take(&mut self.fresh_recyclable_creations);
3614        let cold = std::mem::take(&mut self.cold_recycled_nodes);
3615        for (key, mut nodes) in cold {
3616            let needed = fresh_recyclable_creations.get(&key).copied().unwrap_or(0);
3617            if needed > 0 {
3618                let remaining_limit = self
3619                    .recycle_pool_limit_for(key)
3620                    .unwrap_or(usize::MAX)
3621                    .saturating_sub(self.warm_recycled_pool_len(key));
3622                let promote = nodes.len().min(needed).min(remaining_limit);
3623                let split_at = nodes.len().saturating_sub(promote);
3624                let promoted = nodes.split_off(split_at);
3625                for mut recycled in promoted {
3626                    recycled.set_warm_origin(true);
3627                    self.recycled_nodes.entry(key).or_default().push(recycled);
3628                }
3629            }
3630        }
3631
3632        let mut keys: HashSet<TypeId> = HashSet::default();
3633        keys.extend(self.recycled_nodes.keys().copied());
3634        keys.extend(self.recycled_node_limits.keys().copied());
3635        keys.extend(self.warm_recycled_node_targets.keys().copied());
3636        keys.extend(self.recycled_node_prototypes.keys().copied());
3637        for key in keys {
3638            let observed_demand = fresh_recyclable_creations.get(&key).copied().unwrap_or(0);
3639            let target = self.update_warm_recycled_node_target(key, observed_demand);
3640            self.replenish_warm_pool_to_target(key, target);
3641            self.trim_idle_warm_pool_to_target(key, target);
3642            self.compact_idle_warm_pool(key);
3643        }
3644        self.prune_stable_generations();
3645        // Compact the dense physical storage if it has grown very large relative
3646        // to the live node count (e.g. after a spike of recyclable nodes that
3647        // were all removed). This keeps warm_recycled_node_id_capacity bounded.
3648        self.compact();
3649    }
3650}
3651
3652pub trait ApplierHost {
3653    fn borrow_dyn(&self) -> RefMut<'_, dyn Applier>;
3654    /// Compact internal storage after commands have been applied.
3655    fn compact(&self) {}
3656}
3657
3658pub struct ConcreteApplierHost<A: Applier + 'static> {
3659    inner: RefCell<A>,
3660}
3661
3662impl<A: Applier + 'static> ConcreteApplierHost<A> {
3663    pub fn new(applier: A) -> Self {
3664        Self {
3665            inner: RefCell::new(applier),
3666        }
3667    }
3668
3669    pub fn borrow_typed(&self) -> RefMut<'_, A> {
3670        self.inner.borrow_mut()
3671    }
3672
3673    pub fn try_borrow_typed(&self) -> Result<RefMut<'_, A>, std::cell::BorrowMutError> {
3674        self.inner.try_borrow_mut()
3675    }
3676
3677    pub fn into_inner(self) -> A {
3678        self.inner.into_inner()
3679    }
3680}
3681
3682impl<A: Applier + 'static> ApplierHost for ConcreteApplierHost<A> {
3683    fn borrow_dyn(&self) -> RefMut<'_, dyn Applier> {
3684        RefMut::map(self.inner.borrow_mut(), |applier| {
3685            applier as &mut dyn Applier
3686        })
3687    }
3688
3689    fn compact(&self) {
3690        self.inner.borrow_mut().compact();
3691    }
3692}
3693
3694pub struct ApplierGuard<'a, A: Applier + 'static> {
3695    inner: RefMut<'a, A>,
3696}
3697
3698impl<'a, A: Applier + 'static> ApplierGuard<'a, A> {
3699    fn new(inner: RefMut<'a, A>) -> Self {
3700        Self { inner }
3701    }
3702}
3703
3704impl<'a, A: Applier + 'static> Deref for ApplierGuard<'a, A> {
3705    type Target = A;
3706
3707    fn deref(&self) -> &Self::Target {
3708        &self.inner
3709    }
3710}
3711
3712impl<'a, A: Applier + 'static> DerefMut for ApplierGuard<'a, A> {
3713    fn deref_mut(&mut self) -> &mut Self::Target {
3714        &mut self.inner
3715    }
3716}
3717
3718pub struct SlotsHost {
3719    storage_key: Cell<usize>,
3720    inner: RefCell<SlotsHostInner>,
3721}
3722
3723#[derive(Debug, Default)]
3724pub(crate) struct SlotPassOutcome {
3725    pub(crate) compacted: bool,
3726    pub(crate) compact_anchor_registry_storage: bool,
3727    pub(crate) compact_payload_storage: bool,
3728}
3729
3730#[derive(Default)]
3731pub(crate) struct FinishedSlotPass {
3732    pub(crate) outcome: SlotPassOutcome,
3733    pub(crate) detached_root_children: Vec<slot::DetachedSubtree>,
3734}
3735
3736struct ActivePassState {
3737    state: slot::SlotWriteSessionState,
3738}
3739
3740struct SlotsHostInner {
3741    table: SlotTable,
3742    lifecycle: slot::SlotLifecycleCoordinator,
3743    runtime_state: Option<Rc<crate::composer::ComposerRuntimeState>>,
3744    active_pass: Option<ActivePassState>,
3745}
3746
3747impl Drop for SlotsHost {
3748    fn drop(&mut self) {
3749        let storage_key = self.storage_key.get();
3750        let inner = self.inner.get_mut();
3751        if let Some(state) = inner.runtime_state.clone() {
3752            if let Err(err) = state.dispose_retained_subtrees_for_host(
3753                storage_key,
3754                &mut inner.table,
3755                &mut inner.lifecycle,
3756            ) {
3757                log::error!(
3758                    "retained subtree disposal failed while dropping SlotsHost {storage_key}: {err}"
3759                );
3760                state.abandon_retained_subtrees_for_host(
3761                    storage_key,
3762                    &mut inner.table,
3763                    &mut inner.lifecycle,
3764                );
3765            } else {
3766                state.clear_host_storage_key(storage_key);
3767            }
3768        }
3769        inner.lifecycle.dispose_slot_table(&mut inner.table);
3770    }
3771}
3772
3773impl SlotsHost {
3774    pub(crate) fn storage_key(&self) -> usize {
3775        self.storage_key.get()
3776    }
3777
3778    pub fn new(storage: SlotTable) -> Self {
3779        let storage_key = storage.storage_id();
3780        Self {
3781            storage_key: Cell::new(storage_key),
3782            inner: RefCell::new(SlotsHostInner {
3783                table: storage,
3784                lifecycle: slot::SlotLifecycleCoordinator::default(),
3785                runtime_state: None,
3786                active_pass: None,
3787            }),
3788        }
3789    }
3790
3791    pub(crate) fn bind_runtime_state(&self, state: &Rc<crate::composer::ComposerRuntimeState>) {
3792        let mut inner = self.inner.borrow_mut();
3793        inner.runtime_state = Some(Rc::clone(state));
3794    }
3795
3796    pub(crate) fn rebind_orphaned_runtime_state(
3797        &self,
3798        state: &Rc<crate::composer::ComposerRuntimeState>,
3799    ) -> bool {
3800        let inner = self.inner.borrow();
3801        if inner.active_pass.is_some() {
3802            log::error!("cannot rebind SlotsHost during an active pass");
3803            return false;
3804        }
3805        let Some(bound_state) = inner.runtime_state.as_ref() else {
3806            drop(inner);
3807            self.bind_runtime_state(state);
3808            return true;
3809        };
3810        if Rc::ptr_eq(bound_state, state) {
3811            return true;
3812        }
3813        if bound_state.has_live_applier_host() {
3814            return false;
3815        }
3816        drop(inner);
3817
3818        let mut inner = self.inner.borrow_mut();
3819        let Some(bound_state) = inner.runtime_state.as_ref() else {
3820            inner.runtime_state = Some(Rc::clone(state));
3821            return true;
3822        };
3823        if Rc::ptr_eq(bound_state, state) {
3824            return true;
3825        }
3826        if bound_state.has_live_applier_host() {
3827            return false;
3828        }
3829
3830        let previous_state = Rc::clone(bound_state);
3831        let mut lifecycle = std::mem::take(&mut inner.lifecycle);
3832        lifecycle.flush_pending_drops();
3833        let host_key = self.storage_key();
3834        if previous_state
3835            .dispose_retained_subtrees_for_host(host_key, &mut inner.table, &mut lifecycle)
3836            .is_err()
3837        {
3838            inner.lifecycle = lifecycle;
3839            return false;
3840        }
3841        previous_state.clear_host(self);
3842        lifecycle.flush_pending_drops();
3843        inner.runtime_state = Some(Rc::clone(state));
3844        inner.lifecycle = lifecycle;
3845        true
3846    }
3847
3848    pub(crate) fn runtime_state(&self) -> Option<Rc<crate::composer::ComposerRuntimeState>> {
3849        self.inner.borrow().runtime_state.clone()
3850    }
3851
3852    pub(crate) fn borrow(&self) -> Ref<'_, SlotTable> {
3853        Ref::map(self.inner.borrow(), |inner| &inner.table)
3854    }
3855
3856    pub(crate) fn borrow_mut(&self) -> RefMut<'_, SlotTable> {
3857        RefMut::map(self.inner.borrow_mut(), |inner| &mut inner.table)
3858    }
3859
3860    pub fn into_table(self: Rc<Self>) -> Result<SlotTable, NodeError> {
3861        if Rc::strong_count(&self) != 1 {
3862            return Err(NodeError::SlotHostUnavailable {
3863                operation: "SlotsHost::into_table",
3864                reason: "other host references are alive",
3865            });
3866        }
3867        self.take_table_for_transfer()
3868    }
3869
3870    fn take_table_for_transfer(&self) -> Result<SlotTable, NodeError> {
3871        let inner = self.inner.borrow();
3872        if inner.active_pass.is_some() {
3873            return Err(NodeError::SlotHostUnavailable {
3874                operation: "SlotsHost::into_table",
3875                reason: "slot pass is active",
3876            });
3877        }
3878        drop(inner);
3879        let mut inner = self.inner.borrow_mut();
3880        let mut lifecycle = std::mem::take(&mut inner.lifecycle);
3881        lifecycle.flush_pending_drops();
3882        if let Some(state) = inner.runtime_state.clone() {
3883            let host_key = self.storage_key();
3884            state.dispose_retained_subtrees_for_host(host_key, &mut inner.table, &mut lifecycle)?;
3885            state.clear_host(self);
3886            lifecycle.flush_pending_drops();
3887        }
3888        let taken = std::mem::take(&mut inner.table);
3889        self.storage_key.set(inner.table.storage_id());
3890        inner.runtime_state = None;
3891        inner.lifecycle = lifecycle;
3892        Ok(taken)
3893    }
3894
3895    pub fn reset(&self) -> Result<(), NodeError> {
3896        let inner = self.inner.borrow();
3897        if inner.active_pass.is_some() {
3898            return Err(NodeError::SlotHostUnavailable {
3899                operation: "SlotsHost::reset",
3900                reason: "slot pass is active",
3901            });
3902        }
3903        let runtime_state = inner.runtime_state.clone();
3904        drop(inner);
3905        let mut inner = self.inner.borrow_mut();
3906        let mut lifecycle = std::mem::take(&mut inner.lifecycle);
3907        if let Some(state) = runtime_state {
3908            let host_key = self.storage_key();
3909            state.dispose_retained_subtrees_for_host(host_key, &mut inner.table, &mut lifecycle)?;
3910            state.clear_host(self);
3911        }
3912        lifecycle.dispose_slot_table(&mut inner.table);
3913        inner.table = SlotTable::default();
3914        self.storage_key.set(inner.table.storage_id());
3915        inner.runtime_state = None;
3916        inner.lifecycle = slot::SlotLifecycleCoordinator::default();
3917        Ok(())
3918    }
3919
3920    pub(crate) fn abandon_after_apply_failure(&self) {
3921        let inner = self.inner.borrow();
3922        if inner.active_pass.is_some() {
3923            log::error!("cannot abandon SlotsHost during an active pass");
3924            return;
3925        }
3926        let runtime_state = inner.runtime_state.clone();
3927        drop(inner);
3928        let mut inner = self.inner.borrow_mut();
3929        let mut lifecycle = std::mem::take(&mut inner.lifecycle);
3930        if let Some(state) = runtime_state {
3931            let host_key = self.storage_key();
3932            state.abandon_retained_subtrees_for_host(host_key, &mut inner.table, &mut lifecycle);
3933        }
3934        lifecycle.dispose_slot_table(&mut inner.table);
3935        inner.table = SlotTable::default();
3936        self.storage_key.set(inner.table.storage_id());
3937        inner.runtime_state = None;
3938        inner.lifecycle = slot::SlotLifecycleCoordinator::default();
3939    }
3940
3941    pub(crate) fn debug_stats(&self) -> SlotTableDebugStats {
3942        let inner = self.inner.borrow();
3943        let local = inner.table.debug_stats();
3944        let lifecycle = inner.lifecycle.debug_stats();
3945        let retention = inner
3946            .runtime_state
3947            .clone()
3948            .map(|state| state.slot_retention_debug_stats(self))
3949            .unwrap_or_default();
3950        SlotTableDebugStats::from_parts(local, lifecycle, retention)
3951    }
3952
3953    pub(crate) fn debug_snapshot(&self) -> slot::SlotDebugSnapshot {
3954        let inner = self.inner.borrow();
3955        let mut snapshot = inner.table.debug_snapshot();
3956        if let Some(state) = inner.runtime_state.clone() {
3957            state.fill_slot_debug_snapshot(self, &mut snapshot);
3958        }
3959        snapshot
3960    }
3961
3962    pub(crate) fn begin_pass(&self, mode: slot::SlotPassMode) {
3963        let mut inner = self.inner.borrow_mut();
3964        if inner.active_pass.is_some() {
3965            log::error!("slot pass already active for host");
3966            return;
3967        }
3968        let mut state = slot::SlotWriteSessionState::default();
3969        state.reset_for_pass(mode);
3970        inner.active_pass = Some(ActivePassState { state });
3971    }
3972
3973    pub(crate) fn has_active_pass(&self) -> bool {
3974        self.inner.borrow().active_pass.is_some()
3975    }
3976
3977    pub(crate) fn abandon_active_pass(&self) {
3978        self.inner.borrow_mut().active_pass = None;
3979    }
3980
3981    pub(crate) fn with_write_session<R>(
3982        &self,
3983        f: impl FnOnce(&mut slot::SlotWriteSession<'_>) -> R,
3984    ) -> R {
3985        let mut inner = self.inner.borrow_mut();
3986        let SlotsHostInner {
3987            table,
3988            lifecycle,
3989            active_pass,
3990            ..
3991        } = &mut *inner;
3992        let active_pass = active_pass
3993            .as_mut()
3994            .expect("slot write session requires an active pass");
3995        let mut session = table.write_session(lifecycle, &mut active_pass.state);
3996        f(&mut session)
3997    }
3998
3999    pub(crate) fn with_table_and_lifecycle_mut<R>(
4000        &self,
4001        f: impl FnOnce(&mut SlotTable, &mut slot::SlotLifecycleCoordinator) -> R,
4002    ) -> R {
4003        let mut inner = self.inner.borrow_mut();
4004        let SlotsHostInner {
4005            table, lifecycle, ..
4006        } = &mut *inner;
4007        f(table, lifecycle)
4008    }
4009
4010    pub(crate) fn finish_pass(
4011        &self,
4012        applier: &mut dyn Applier,
4013    ) -> Result<FinishedSlotPass, NodeError> {
4014        let mut inner = self.inner.borrow_mut();
4015        let SlotsHostInner {
4016            table,
4017            lifecycle,
4018            active_pass: active_pass_slot,
4019            ..
4020        } = &mut *inner;
4021        let Some(mut active_pass) = active_pass_slot.take() else {
4022            return Ok(FinishedSlotPass::default());
4023        };
4024
4025        active_pass.state.flush_payload_location_refreshes(table);
4026
4027        #[cfg(debug_assertions)]
4028        if let Err(err) = active_pass.state.validate(table) {
4029            log::error!("slot writer invariant violation before finalize_pass: {err:?}");
4030            return Err(NodeError::SlotHostUnavailable {
4031                operation: "SlotsHost::finish_pass",
4032                reason: "slot writer invariant violation",
4033            });
4034        }
4035
4036        let detached_root_children = {
4037            let mut session = table.write_session(lifecycle, &mut active_pass.state);
4038            session.finalize_pass(applier)?
4039        };
4040
4041        Ok(FinishedSlotPass {
4042            outcome: SlotPassOutcome {
4043                compacted: active_pass.state.request_compaction,
4044                compact_anchor_registry_storage: active_pass
4045                    .state
4046                    .request_anchor_storage_compaction,
4047                compact_payload_storage: active_pass.state.request_payload_storage_compaction,
4048            },
4049            detached_root_children,
4050        })
4051    }
4052
4053    pub(crate) fn complete_pass_cleanup(&self, outcome: &SlotPassOutcome) {
4054        let mut inner = self.inner.borrow_mut();
4055        let SlotsHostInner {
4056            table,
4057            lifecycle,
4058            runtime_state,
4059            ..
4060        } = &mut *inner;
4061        lifecycle.flush_pending_drops();
4062        if outcome.compacted {
4063            table.compact_storage();
4064            lifecycle.compact_storage();
4065        }
4066        if let Some(state) = runtime_state.clone() {
4067            state.compact_table_identity_storage_for_host(
4068                self,
4069                table,
4070                outcome.compact_anchor_registry_storage,
4071                outcome.compact_payload_storage,
4072            );
4073        } else {
4074            if outcome.compact_anchor_registry_storage {
4075                table.compact_anchor_registry_storage(None);
4076            }
4077            if outcome.compact_payload_storage {
4078                table.compact_payload_anchor_registry_storage(None);
4079            }
4080        }
4081        table.assert_fast_integrity("slot pass cleanup");
4082        #[cfg(any(test, debug_assertions))]
4083        {
4084            table.debug_verify();
4085            if let Some(state) = runtime_state.clone() {
4086                state.debug_verify_host(self, table);
4087            }
4088        }
4089    }
4090}
4091
4092fn build_child_positions(children: &[NodeId]) -> HashMap<NodeId, usize> {
4093    let mut positions = HashMap::default();
4094    positions.reserve(children.len());
4095    for (index, &child) in children.iter().enumerate() {
4096        positions.insert(child, index);
4097    }
4098    positions
4099}
4100
4101fn refresh_child_positions(
4102    current: &[NodeId],
4103    positions: &mut HashMap<NodeId, usize>,
4104    start: usize,
4105    end: usize,
4106) {
4107    if current.is_empty() || start >= current.len() {
4108        return;
4109    }
4110    let end = end.min(current.len() - 1);
4111    for (offset, &child) in current[start..=end].iter().enumerate() {
4112        positions.insert(child, start + offset);
4113    }
4114}
4115
4116fn insert_child_into_diff_state(
4117    current: &mut ChildList,
4118    positions: &mut HashMap<NodeId, usize>,
4119    index: usize,
4120    child: NodeId,
4121) {
4122    let index = index.min(current.len());
4123    current.insert(index, child);
4124    refresh_child_positions(current, positions, index, current.len() - 1);
4125}
4126
4127fn move_child_in_diff_state(
4128    current: &mut ChildList,
4129    positions: &mut HashMap<NodeId, usize>,
4130    from_index: usize,
4131    target_index: usize,
4132) -> usize {
4133    let child = current.remove(from_index);
4134    let to_index = target_index.min(current.len());
4135    current.insert(to_index, child);
4136    refresh_child_positions(
4137        current,
4138        positions,
4139        from_index.min(to_index),
4140        from_index.max(to_index),
4141    );
4142    to_index
4143}
4144
4145pub(crate) use state::MutableStateInner;
4146pub use state::{MutableState, OwnedMutableState, SnapshotStateList, SnapshotStateMap, State};
4147
4148fn hash_key<K: Hash>(key: &K) -> Key {
4149    let mut hasher = hash::default::new();
4150    key.hash(&mut hasher);
4151    hasher.finish()
4152}
4153
4154pub(crate) fn explicit_group_key_seed<K: Hash>(
4155    key: &K,
4156    caller: &'static std::panic::Location<'static>,
4157) -> slot::GroupKeySeed {
4158    let source_key = location_key(caller.file(), caller.line(), caller.column());
4159    let explicit_key = hash_key(key);
4160    slot::GroupKeySeed::keyed(source_key, explicit_key)
4161}
4162
4163#[cfg(test)]
4164#[path = "tests/mod.rs"]
4165mod tests;
4166
4167#[cfg(test)]
4168#[path = "tests/recursive_decrease_increase_test.rs"]
4169mod recursive_decrease_increase_test;
4170
4171pub mod collections;
4172pub mod hash;