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