Skip to main content

euv_core/layout/cache/
struct.rs

1use super::*;
2
3/// Available layout space passed down during a layout pass and cached for clean-subtree skipping.
4#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
5pub struct AvailableSpace {
6    /// Available horizontal extent in logical pixels.
7    #[get(type(copy))]
8    pub(crate) width: f32,
9    /// Available vertical extent in logical pixels.
10    #[get(type(copy))]
11    pub(crate) height: f32,
12    /// Root viewport size for `vw`/`vh` length resolution.
13    #[get(type(copy))]
14    pub(crate) viewport: Size,
15    /// Origin X coordinate where this node is placed.
16    #[get(type(copy))]
17    pub(crate) origin_x: f32,
18    /// Origin Y coordinate where this node is placed.
19    #[get(type(copy))]
20    pub(crate) origin_y: f32,
21    /// Whether the parent stretched this node's width to fill `width` when its declared width is `Length::Auto`.
22    #[get(type(copy))]
23    #[new(skip)]
24    pub(crate) stretch_width: bool,
25    /// Whether the parent stretched this node's height to fill `height` when its declared height is `Length::Auto`.
26    #[get(type(copy))]
27    #[new(skip)]
28    pub(crate) stretch_height: bool,
29}
30
31/// Internal container constraint bundle passed to child layout solvers.
32#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
33pub(crate) struct ContainerFrame {
34    /// Parent layout properties.
35    #[get(pub(crate), type(copy))]
36    #[get_mut(pub(crate))]
37    #[set(pub(crate))]
38    pub(crate) parent_layout: LayoutStyle,
39    /// Inner available dimensions inside padding and border.
40    #[get(pub(crate), type(copy))]
41    #[get_mut(pub(crate))]
42    #[set(pub(crate))]
43    pub(crate) inner_avail: Size,
44    /// Root viewport logical size.
45    #[get(pub(crate), type(copy))]
46    #[get_mut(pub(crate))]
47    #[set(pub(crate))]
48    pub(crate) viewport: Size,
49    /// Content origin in root coordinates.
50    #[get(pub(crate), type(copy))]
51    #[get_mut(pub(crate))]
52    #[set(pub(crate))]
53    pub(crate) content_origin: Point,
54    /// Whether the container's outer width is definite (explicit length, aspect ratio, or parent stretch).
55    #[get(pub(crate), type(copy))]
56    #[get_mut(pub(crate))]
57    #[set(pub(crate))]
58    pub(crate) definite_width: bool,
59    /// Whether the container's outer height is definite (explicit length, aspect ratio, or parent stretch).
60    #[get(pub(crate), type(copy))]
61    #[get_mut(pub(crate))]
62    #[set(pub(crate))]
63    pub(crate) definite_height: bool,
64}
65
66/// Telemetry counters recorded per layout pass.
67#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
68pub struct LayoutStats {
69    /// Number of nodes laid out during this pass.
70    #[get(type(copy))]
71    pub(crate) nodes_computed: usize,
72    /// Number of clean subtrees skipped in O(1) via constraint caching.
73    #[get(type(copy))]
74    pub(crate) subtrees_skipped: usize,
75    /// Number of nodes whose final layout geometry actually changed during this pass.
76    #[get(type(copy))]
77    #[new(skip)]
78    pub(crate) nodes_changed: usize,
79}
80
81/// Cumulative layout invalidation and effectiveness telemetry separating runs from actual geometric mutations.
82#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
83pub struct LayoutTelemetry {
84    /// Cumulative layout-invalidating scene mutations (`layout.invalidations`).
85    #[get(type(copy))]
86    pub(crate) invalidations: u64,
87    /// Cumulative executed layout passes (`layout.passes`).
88    #[get(type(copy))]
89    pub(crate) passes: u64,
90    /// Cumulative layout passes that produced at least one geometric change (`layout.changed_passes`).
91    #[get(type(copy))]
92    pub(crate) changed_passes: u64,
93    /// Cumulative count of nodes whose layout box actually changed (`layout.changed_nodes_total`).
94    #[get(type(copy))]
95    pub(crate) changed_nodes_total: u64,
96}
97
98/// Per-pass split layout cache and interval text cache hit/miss diagnostics.
99#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
100pub struct SplitCacheStats {
101    /// Number of O(1) measure cache hits in `SplitLayoutCache::measure_slots`.
102    pub(crate) measure_hits: u64,
103    /// Number of measure cache misses requiring computation in `ComputeSize` mode.
104    pub(crate) measure_misses: u64,
105    /// Number of O(1) final layout cache hits in `SplitLayoutCache::final_slots`.
106    pub(crate) final_hits: u64,
107    /// Number of final layout misses requiring `PerformLayout` execution.
108    pub(crate) final_misses: u64,
109    /// Number of `Display::None` subtrees skipped in O(1) while preserving descendant caches.
110    pub(crate) display_none_skips: u64,
111    /// Number of text measurements served in O(1) by interval or wrapped hits in `TextSizeCache`.
112    pub(crate) text_interval_hits: u64,
113    /// Number of text measurements that reused cached per-node text hashes without re-hashing.
114    pub(crate) text_hash_reuses: u64,
115}
116
117/// Compact value key packing all layout-relevant typography properties of `TextStyle`.
118#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
119pub(crate) struct TextStyleKey {
120    /// Normalized IEEE-754 bits of `TextStyle::font_size`.
121    pub(crate) font_size_bits: u32,
122    /// Normalized IEEE-754 bits of `TextStyle::line_height`.
123    pub(crate) line_height_bits: u32,
124    /// Normalized IEEE-754 bits of `TextStyle::letter_spacing`.
125    pub(crate) letter_spacing_bits: u32,
126    /// Packed typography flags (`family`, `weight`, `overflow`, `italic`, and occupied bit).
127    pub(crate) style_flags: u16,
128}
129
130/// Interval-aware set-associative cache slot mapping `(text_hash, style_key)` to unwrapped and wrapped metrics.
131#[derive(Clone, Copy, Debug, Default)]
132pub(crate) struct TextSizeEntry {
133    /// Primary 64-bit hash of the measured UTF-8 text bytes.
134    pub(crate) hash_a: u64,
135    /// Secondary independent 64-bit hash of the measured UTF-8 text bytes.
136    pub(crate) hash_b: u64,
137    /// Exact byte length of the measured text string.
138    pub(crate) text_len: u32,
139    /// Packed typography style key (excluding `max_width` so all wrap widths share one entry).
140    pub(crate) style_key: TextStyleKey,
141    /// Populated field bitmask (`TEXT_SIZE_ENTRY_HAS_*`).
142    pub(crate) entry_flags: u8,
143    /// Minimum content width observed under tight wrapping constraints.
144    pub(crate) min_content_w: f32,
145    /// Maximum unconstrained single/raw-line content width.
146    pub(crate) max_content_w: f32,
147    /// Unwrapped bounding height matching `max_content_w`.
148    pub(crate) single_line_h: f32,
149    /// Exact width returned for `max_width == None` when trailing/leading spaces exist.
150    pub(crate) none_w: f32,
151    /// Exact height returned for `max_width == None`.
152    pub(crate) none_h: f32,
153    /// Normalized IEEE-754 bits of the most recent wrapped/ellipsized `max_width` query.
154    pub(crate) last_max_w_bits: u32,
155    /// Cached wrapped/ellipsized width for `last_max_w_bits`.
156    pub(crate) last_wrapped_w: f32,
157    /// Cached wrapped/ellipsized height for `last_max_w_bits`.
158    pub(crate) last_wrapped_h: f32,
159}
160
161/// Measurement outcome returned by `TextSizeCache::get_or_measure_hashed` including wrap-interval metadata.
162#[derive(Clone, Copy, Debug, Default)]
163pub(crate) struct TextSizeMeasurement {
164    /// Measured bounding dimensions.
165    pub(crate) size: Size,
166    /// Whether the measurement is valid for all larger wrap widths (`avail_w >= max_content_w`).
167    pub(crate) unwrapped: bool,
168    /// Known unwrapped width threshold (`f32::INFINITY` when not yet determined).
169    pub(crate) max_content_w: f32,
170    /// Whether the measurement was served from cache without invoking the measurer callback.
171    pub(crate) cache_hit: bool,
172}
173
174/// Bounded 2-way set-associative interval-aware cache mapping `(text_hash, style_key)` to text metrics.
175#[derive(Clone, Debug, Default)]
176pub(crate) struct TextSizeCache {
177    /// Lazily allocated flat table of `TEXT_SIZE_CACHE_SLOTS` entries (`2` ways per set).
178    pub(crate) entries: Vec<TextSizeEntry>,
179}
180
181/// Single LRU measure cache slot storing an intrinsic sizing result independently of `FinalLayoutSlot`.
182#[derive(Clone, Copy, Debug, Default, PartialEq)]
183pub(crate) struct MeasureCacheSlot {
184    /// Whether this measure slot holds a populated entry.
185    pub(crate) occupied: bool,
186    /// Stretched known width (`f32::NAN` when not stretched by parent).
187    pub(crate) known_w: f32,
188    /// Stretched known height (`f32::NAN` when not stretched by parent).
189    pub(crate) known_h: f32,
190    /// Available width constraint used during measurement.
191    pub(crate) avail_w: f32,
192    /// Available height constraint used during measurement.
193    pub(crate) avail_h: f32,
194    /// Root viewport width used during measurement.
195    pub(crate) viewport_w: f32,
196    /// Root viewport height used during measurement.
197    pub(crate) viewport_h: f32,
198    /// Minimum available width for which an unwrapped measurement stays invariant.
199    pub(crate) min_avail_w: f32,
200    /// Run mode recorded when this slot was populated.
201    pub(crate) run_mode: LayoutRunMode,
202    /// Node content/style generation guarding against stale dirty entries.
203    pub(crate) content_gen: u32,
204    /// Dependency mask (`LAYOUT_DEP_*`) recording which constraint axes affected `size`.
205    pub(crate) dep_flags: u8,
206    /// Cached measured border-box dimensions.
207    pub(crate) size: Size,
208}
209
210/// Separate final-layout cache slot storing committed `PerformLayout` geometry for O(1) clean-subtree reuse.
211#[derive(Clone, Copy, Debug, Default, PartialEq)]
212pub(crate) struct FinalLayoutSlot {
213    /// Exact incoming available space (including origin and stretch flags) for this committed layout.
214    pub(crate) space: AvailableSpace,
215    /// Node subtree generation matching this committed layout snapshot.
216    pub(crate) subtree_gen: u32,
217    /// Border-box dimensions computed by `layout_node` prior to any parent post-stretch.
218    pub(crate) size: Size,
219    /// Committed border-box rectangle written by `layout_node`.
220    pub(crate) bounds: Rect,
221    /// Committed content-box rectangle written by `layout_node`.
222    pub(crate) content_bounds: Rect,
223    /// Committed intrinsic content dimensions written by `layout_node`.
224    pub(crate) content_size: Size,
225    /// Dependency mask (`LAYOUT_DEP_*`) computed during this layout pass.
226    pub(crate) dep_flags: u8,
227    /// Minimum available width for which an unwrapped measurement stays invariant.
228    pub(crate) min_avail_w: f32,
229    /// Whether the enclosing parent container post-stretched `bounds` after `layout_node` returned.
230    pub(crate) post_stretched: bool,
231    /// Whether this node was committed under `Display::None` (preserving descendant warm caches).
232    pub(crate) hidden: bool,
233}
234
235/// Cached per-node 128-bit text hash avoiding repeat UTF-8 byte hashing across layout probes and resizes.
236#[derive(Clone, Copy, Debug, Default)]
237pub(crate) struct NodeTextHashSlot {
238    /// Live `NodeId::generation` matching this cached text hash.
239    pub(crate) node_gen: u32,
240    /// Primary 64-bit hash of the node's text bytes.
241    pub(crate) hash_a: u64,
242    /// Secondary independent 64-bit hash of the node's text bytes.
243    pub(crate) hash_b: u64,
244    /// Exact byte length of the hashed text slice.
245    pub(crate) text_len: u32,
246    /// Backing UTF-8 buffer pointer address guarding against unflagged buffer replacement.
247    pub(crate) text_ptr: usize,
248}
249
250/// Flat Structure-of-Arrays (SoA) per-node layout cache, spatial subtree bounds, and generation-stamped dedup table.
251#[derive(Clone, Debug, Default)]
252pub(crate) struct SplitLayoutCache {
253    /// Live `NodeId::generation` stored per arena slot (`0` = empty/unpopulated).
254    pub(crate) node_gens: Vec<u32>,
255    /// Monotonic per-node content/subtree generation bumped on dirty recomputation.
256    pub(crate) content_gens: Vec<u32>,
257    /// Layout pass epoch when a dirty node's stale cache slots were invalidated.
258    pub(crate) invalidated_epochs: Vec<u32>,
259    /// 4-slot LRU measure cache per node, never overwritten by `final_slots` or vice-versa.
260    pub(crate) measure_slots: Vec<[MeasureCacheSlot; MEASURE_CACHE_SLOTS]>,
261    /// Separate final-layout cache slot per node.
262    pub(crate) final_slots: Vec<Option<FinalLayoutSlot>>,
263    /// Per-node cached 128-bit text hashes avoiding repeat hashing on unchanged text nodes.
264    pub(crate) text_hashes: Vec<NodeTextHashSlot>,
265    /// Per-container visual/interactive subtree bounding box in root logical coordinates for O(1) hit-test pruning.
266    pub(crate) subtree_bounds: Vec<Rect>,
267    /// Packed per-node subtree flags (`SUBTREE_FLAG_VALID | SUBTREE_FLAG_NONZERO_Z_CHILD`).
268    pub(crate) subtree_flags: Vec<u8>,
269    /// Generation-stamped snapshot dedup table (`snapshot_seen_gen[idx] == snapshot_gen` means already recorded).
270    pub(crate) snapshot_seen_gen: Vec<u32>,
271    /// Monotonic layout pass epoch incremented on every `LayoutEngine::compute_inner` call.
272    pub(crate) pass_epoch: u32,
273    /// Monotonic snapshot dedup generation incremented on every damage-tracked layout pass.
274    pub(crate) snapshot_gen: u32,
275    /// `SceneTree::revision` recorded at the end of the latest completed layout pass.
276    pub(crate) synced_tree_revision: u64,
277    /// `SceneTree::root` recorded at the end of the latest completed layout pass.
278    pub(crate) synced_root: NodeId,
279}
280
281/// Compact single-cache-line scratch record tracking a flex child across probe and placement passes.
282#[derive(Clone, Copy, Debug, Default)]
283pub(crate) struct FlexItemRecord {
284    /// Scene node handle of the flex child.
285    pub(crate) node_id: NodeId,
286    /// Current target size updated by flex grow and auto-cross re-probing.
287    pub(crate) size: Size,
288    /// Outer margin insets copied once from `LayoutStyle`.
289    pub(crate) margin: Edges,
290    /// Flex grow factor copied once from `LayoutStyle`.
291    pub(crate) flex_grow: f32,
292    /// Effective `align-self` override copied once from `LayoutStyle`.
293    pub(crate) align_self: AlignSelf,
294    /// Overflow clipping behavior copied once from `LayoutStyle`.
295    pub(crate) overflow: Overflow,
296    /// Packed boolean flags (`FLEX_ITEM_*`) describing child sizing constraints.
297    pub(crate) flags: u16,
298}
299
300/// Shared axis and container parameters passed to `LayoutEngine::layout_flex_line`.
301#[derive(Clone, Copy, Debug)]
302pub(crate) struct FlexLineContext {
303    /// Enclosing flex container constraints and origin.
304    pub(crate) frame: ContainerFrame,
305    /// `true` when main axis is horizontal (`Row`/`RowReverse`).
306    pub(crate) is_row: bool,
307    /// `true` when multi-line flex wrapping is enabled.
308    pub(crate) wrap_enabled: bool,
309    /// Main-axis gap between adjacent items in logical pixels.
310    pub(crate) main_gap: f32,
311    /// Inner available main-axis extent of the container.
312    pub(crate) main_avail: f32,
313    /// Inner available cross-axis extent of the container.
314    pub(crate) cross_avail: f32,
315    /// Aggregate `flex_grow` sum across items in the container.
316    pub(crate) total_grow: f32,
317    /// Total number of flex lines in the container.
318    pub(crate) line_count: usize,
319    /// Active layout execution mode (`ComputeSize` vs `PerformLayout`).
320    pub(crate) mode: LayoutRunMode,
321}