Tree.CacheSourceLayout computation result caching.
This module provides memoization for layout computations. During layout, parent nodes often query child sizes multiple times under different constraints. The cache prevents redundant computation and ensures earlier results are not overwritten by later ones.
The cache maintains two entry types:
Perform_layout modeCompute_size modeMeasure cache slots are determined by two factors:
Definite available space shares a cache slot with max-content because nodes are typically sized under one or the other, not both.
Slot assignments:
A cache entry matches a query when:
Hidden layouts (Perform_hidden_layout mode) are never cached.
cache_size is the number of measure cache slots (9).
type 'a cache_entry = {known_dimensions : float option Geometry.size;available_space : Available_space.t Geometry.size;content : 'a;}Cached layout result paired with its input constraints.
Fields known_dimensions and available_space record the sizing constraints that produced content.
type t = {mutable final_layout_entry : Layout_output.t cache_entry option;mutable measure_entries : float Geometry.size cache_entry option array;mutable is_empty : bool;}Cache for layout computation results.
Field final_layout_entry stores complete layout output with positions and baselines from Perform_layout mode. Field measure_entries stores preliminary size measurements from Compute_size mode. Field is_empty tracks whether any entries are populated.
Result of a clear operation.
compute_cache_slot known_dimensions available_space returns the measure cache slot index for the given constraints.
The slot is determined by which dimensions are known and the available space constraints for unknown dimensions.
is_roughly_equal av1 av2 tests approximate equality of available space values.
Definite values are equal when they differ by less than 0.0001. Min-content and max-content values are equal only to themselves.
val get :
t ->
known_dimensions:float option Geometry.size ->
available_space:Available_space.t Geometry.size ->
run_mode:Run_mode.t ->
Layout_output.t optionget t ~known_dimensions ~available_space ~run_mode retrieves a cached layout result matching the given constraints.
For Perform_layout, searches the final layout entry. For Compute_size, searches all measure entries. For Perform_hidden_layout, always returns None.
A cache entry matches when known dimensions match either the cached known dimensions or the cached result size, and unknown dimensions have roughly equal available space constraints.
val store :
t ->
known_dimensions:float option Geometry.size ->
available_space:Available_space.t Geometry.size ->
run_mode:Run_mode.t ->
Layout_output.t ->
unitstore t ~known_dimensions ~available_space ~run_mode layout_output stores a layout result in the cache.
For Perform_layout, stores complete layout_output in the final layout entry, replacing any previous entry. For Compute_size, stores only the size component in the measure entry slot determined by compute_cache_slot. For Perform_hidden_layout, performs no caching.
clear t removes all cached entries and reports the operation outcome.
Resets both final layout entry and all measure entries to None.