Skip to main content

Module cache

Module cache 

Source
Expand description

Handling Viewport Resizing and Layout Thrashing

The viewport size is a fundamental input to the entire layout process. A change in viewport size must trigger a relayout.

  1. The layout_document function takes the viewport as an argument. The LayoutCache stores the viewport from the previous frame.
  2. The reconcile_and_invalidate function detects that the viewport has changed size
  3. This single change—marking the root as a layout root—forces a full top-down pass (calculate_layout_for_subtree starting from the root). This correctly recalculates all(calculate_layout_for_subtree starting from the root). This correctly recalculates all percentage-based sizes and repositions all elements according to the new viewport dimensions.
  4. The intrinsic size calculation (bottom-up) can often be skipped, as it’s independent of the container size, which is a significant optimization.

Structs§

LayoutCache
The persistent cache that holds the layout state between frames.
ReconciliationResult
The result of a reconciliation pass.

Functions§

calculate_layout_for_subtree
Recursive, top-down pass to calculate used sizes and positions for a given subtree. This is the single, authoritative function for in-flow layout.
collect_children_dom_ids
Collects DOM child IDs from the node hierarchy into a Vec.
compute_counters
Computes CSS counter values for all nodes in the layout tree.
is_simple_flex_stack
Checks if a flex container is simple enough to be treated like a block-stack for repositioning.
reconcile_and_invalidate
Compares the new DOM against the cached tree, creating a new tree and identifying which parts need to be re-laid out.
reconcile_recursive
Recursively traverses the new DOM and old tree, building a new tree and marking dirty nodes.
reposition_block_flow_siblings
Repositions clean children within a simple block-flow layout (like a BFC or a table-row-group). It stacks children along the main axis, preserving their previously calculated cross-axis alignment.
reposition_clean_subtrees
After dirty subtrees are laid out, this repositions their clean siblings without recalculating their internal layout. This is a critical optimization.
shift_subtree_position
Helper to recursively shift the absolute position of a node and all its descendants.
style_text_align_to_fc
Convert StyleTextAlign to fc::TextAlign
to_overflow_behavior
Convert LayoutOverflow to OverflowBehavior