mirror of
https://github.com/lexmount/moli.git
synced 2026-11-24 08:02:58 +00:00
fix(layout): distinguish container feedback cycles from rebuild limits
This commit is contained in:
@@ -14,9 +14,18 @@ pub enum LayoutError {
|
||||
/// A layout demand attempted to enter while another synchronous pass was active.
|
||||
#[error("a synchronous layout pass cannot re-enter itself")]
|
||||
ReentrantLayoutPass,
|
||||
/// Container-query feedback did not settle within its layout budget.
|
||||
#[error("container query sizes did not converge after {iterations} feedback iterations")]
|
||||
ContainerQuerySizesDidNotConverge { iterations: usize },
|
||||
/// A container-query size table repeated with no new scrollbar axis to freeze.
|
||||
#[error("container query sizes repeated in a cycle of {period} feedback iterations")]
|
||||
ContainerQueryFeedbackCycle { period: usize },
|
||||
/// The page exhausted the work budget for rebuilding layout boxes during feedback.
|
||||
#[error(
|
||||
"container query feedback exceeded its layout work budget after {iterations} iterations: {boxes} boxes, limit {max_boxes}"
|
||||
)]
|
||||
ContainerQueryFeedbackBudgetExceeded {
|
||||
iterations: usize,
|
||||
boxes: usize,
|
||||
max_boxes: usize,
|
||||
},
|
||||
/// A consumer requested paint data from a geometry-only output.
|
||||
#[error("this layout pass did not request a paint projection")]
|
||||
PaintProjectionNotRequested,
|
||||
|
||||
+12
-3
@@ -314,11 +314,16 @@ impl<N: Eq + Hash> Default for AutoScrollbars<N> {
|
||||
}
|
||||
|
||||
impl<N: Copy + Eq + Hash> AutoScrollbars<N> {
|
||||
/// Record changed visibility and report whether it introduced any new axes.
|
||||
pub fn extend_changed_axes(&mut self, first: &Self, second: &Self) -> bool {
|
||||
let previous_count = self.0.len();
|
||||
/// Record every axis whose visibility differs between two measurements.
|
||||
pub fn extend_changed_axes(&mut self, first: &Self, second: &Self) {
|
||||
self.0
|
||||
.extend(first.0.symmetric_difference(&second.0).copied());
|
||||
}
|
||||
|
||||
/// Add the axes observed in a confirmed cycle to the frozen gutters.
|
||||
pub fn freeze_axes(&mut self, changed: Self) -> bool {
|
||||
let previous_count = self.0.len();
|
||||
self.0.extend(changed.0);
|
||||
self.0.len() != previous_count
|
||||
}
|
||||
}
|
||||
@@ -327,6 +332,10 @@ impl<N> MeasuredLayoutPass<N>
|
||||
where
|
||||
N: Copy + std::fmt::Debug + Eq + std::hash::Hash,
|
||||
{
|
||||
pub fn box_count(&self) -> usize {
|
||||
self.prepared.world.len()
|
||||
}
|
||||
|
||||
/// Rebuild after style feedback, keeping the cost of the discarded layout.
|
||||
pub fn remeasure(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
use moli_layout::{AutoScrollbars, LayoutError, MAX_RETAINED_LAYOUT_BOXES};
|
||||
use moli_selector::QueryContainerSizes;
|
||||
|
||||
use crate::document_runtime::DomHandle;
|
||||
|
||||
// Bound full box-tree rebuild work independently of the number of query
|
||||
// containers or scrollbar transitions. One large pass consumes more budget
|
||||
// than one small pass.
|
||||
const MAX_FEEDBACK_BOXES: usize = 16 * MAX_RETAINED_LAYOUT_BOXES;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct ContainerQueryFeedback {
|
||||
checkpoint: Option<Checkpoint>,
|
||||
pub(super) frozen_scrollbars: AutoScrollbars<DomHandle>,
|
||||
iterations: usize,
|
||||
boxes: usize,
|
||||
}
|
||||
|
||||
struct Checkpoint {
|
||||
sizes: QueryContainerSizes,
|
||||
scrollbars: AutoScrollbars<DomHandle>,
|
||||
changed_axes: AutoScrollbars<DomHandle>,
|
||||
window: usize,
|
||||
steps: usize,
|
||||
}
|
||||
|
||||
impl Checkpoint {
|
||||
fn new(
|
||||
sizes: QueryContainerSizes,
|
||||
scrollbars: AutoScrollbars<DomHandle>,
|
||||
window: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
sizes,
|
||||
scrollbars,
|
||||
changed_axes: AutoScrollbars::default(),
|
||||
window,
|
||||
steps: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContainerQueryFeedback {
|
||||
/// With frozen gutters held fixed, the measured size table determines the
|
||||
/// next pass. Brent's expanding checkpoint detects exact repeats with a
|
||||
/// single retained table. Accumulate scrollbar changes over that window
|
||||
/// so only axes that toggle in a confirmed cycle are eligible to freeze.
|
||||
pub(super) fn observe(
|
||||
&mut self,
|
||||
sizes: QueryContainerSizes,
|
||||
scrollbars: AutoScrollbars<DomHandle>,
|
||||
) -> Result<bool, LayoutError> {
|
||||
let Some(checkpoint) = self.checkpoint.as_mut() else {
|
||||
self.checkpoint = Some(Checkpoint::new(sizes, scrollbars, 1));
|
||||
return Ok(false);
|
||||
};
|
||||
checkpoint.steps += 1;
|
||||
checkpoint
|
||||
.changed_axes
|
||||
.extend_changed_axes(&checkpoint.scrollbars, &scrollbars);
|
||||
if checkpoint.sizes == sizes {
|
||||
if self
|
||||
.frozen_scrollbars
|
||||
.freeze_axes(std::mem::take(&mut checkpoint.changed_axes))
|
||||
{
|
||||
self.checkpoint = None;
|
||||
return Ok(true);
|
||||
}
|
||||
return Err(LayoutError::ContainerQueryFeedbackCycle {
|
||||
period: checkpoint.steps,
|
||||
});
|
||||
}
|
||||
if checkpoint.steps == checkpoint.window {
|
||||
*checkpoint = Checkpoint::new(sizes, scrollbars, checkpoint.window.saturating_mul(2));
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub(super) fn reserve_rebuild(&mut self, box_count: usize) -> Result<(), LayoutError> {
|
||||
self.boxes = self.boxes.saturating_add(box_count);
|
||||
if self.boxes > MAX_FEEDBACK_BOXES {
|
||||
return Err(LayoutError::ContainerQueryFeedbackBudgetExceeded {
|
||||
iterations: self.iterations,
|
||||
boxes: self.boxes,
|
||||
max_boxes: MAX_FEEDBACK_BOXES,
|
||||
});
|
||||
}
|
||||
self.iterations += 1;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use app_units::Au;
|
||||
use euclid::default::Size2D;
|
||||
|
||||
fn size_table(width: i32) -> QueryContainerSizes {
|
||||
[(
|
||||
DomHandle::new(1),
|
||||
Size2D::new(Some(Au::from_px(width)), None),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn container_feedback_checks_actual_repeats_and_rebuild_work_separately() {
|
||||
let mut feedback = ContainerQueryFeedback::default();
|
||||
for width in 0..128 {
|
||||
assert_eq!(
|
||||
feedback.observe(size_table(width), AutoScrollbars::default()),
|
||||
Ok(false)
|
||||
);
|
||||
feedback.reserve_rebuild(20).unwrap();
|
||||
}
|
||||
|
||||
let mut feedback = ContainerQueryFeedback::default();
|
||||
// The repeated tail has three states and contains no scrollbar axes.
|
||||
for width in [10, 20, 30, 40, 20, 30] {
|
||||
assert_eq!(
|
||||
feedback.observe(size_table(width), AutoScrollbars::default()),
|
||||
Ok(false)
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
feedback.observe(size_table(40), AutoScrollbars::default()),
|
||||
Err(LayoutError::ContainerQueryFeedbackCycle { period: 3 }),
|
||||
);
|
||||
|
||||
let mut feedback = ContainerQueryFeedback::default();
|
||||
feedback.reserve_rebuild(MAX_FEEDBACK_BOXES).unwrap();
|
||||
assert_eq!(
|
||||
feedback.reserve_rebuild(1),
|
||||
Err(LayoutError::ContainerQueryFeedbackBudgetExceeded {
|
||||
iterations: 1,
|
||||
boxes: MAX_FEEDBACK_BOXES + 1,
|
||||
max_boxes: MAX_FEEDBACK_BOXES,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod container_feedback;
|
||||
mod inline_svg;
|
||||
#[cfg(test)]
|
||||
pub(crate) use inline_svg::parse_count as svg_parse_count;
|
||||
@@ -8,13 +9,13 @@ mod style_resolver;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use moli_layout::{
|
||||
AutoScrollbars, DocumentLayoutServices, EmbeddedFrameRenderer, LayoutPassRequest,
|
||||
LayoutPassResult, LayoutViewport, finish_measured_layout_pass, measure_prepared_layout_pass,
|
||||
prepare_layout_pass,
|
||||
DocumentLayoutServices, EmbeddedFrameRenderer, LayoutPassRequest, LayoutPassResult,
|
||||
LayoutViewport, finish_measured_layout_pass, measure_prepared_layout_pass, prepare_layout_pass,
|
||||
};
|
||||
use moli_selector::QueryContainerSizes;
|
||||
|
||||
use crate::{document_runtime::DomHandle, native_bridge::PageScriptHost};
|
||||
use container_feedback::ContainerQueryFeedback;
|
||||
|
||||
/// Measured sizes are provisional until a Realtime screen pass publishes.
|
||||
/// Captures and failed passes restore the previous tables for CSSOM reads.
|
||||
@@ -124,14 +125,8 @@ fn build_native_layout_pass_recursive(
|
||||
};
|
||||
let finish = |services: &mut DocumentLayoutServices| {
|
||||
let mut measured = measure_prepared_layout_pass(prepared, services)?;
|
||||
let mut iterations = 0usize;
|
||||
let mut largest_container_count = 0;
|
||||
let mut feedback = ContainerQueryFeedback::default();
|
||||
let mut previous_scrollbars = None;
|
||||
let mut scrollbar_transitions =
|
||||
Vec::<(QueryContainerSizes, AutoScrollbars<DomHandle>)>::new();
|
||||
let mut frozen_scrollbars = AutoScrollbars::default();
|
||||
let mut changed_scrollbar_axes = AutoScrollbars::default();
|
||||
let mut last_scrollbar_progress = 0;
|
||||
loop {
|
||||
let sizes: QueryContainerSizes = measured
|
||||
.query_container_sizes()
|
||||
@@ -145,59 +140,25 @@ fn build_native_layout_pass_recursive(
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
largest_container_count = largest_container_count.max(sizes.len());
|
||||
let repeated = scrollbar_transitions
|
||||
.iter()
|
||||
.rposition(|(earlier, _)| earlier == &sizes);
|
||||
let Some(previous) = runtime.replace_layout_container_sizes(document, sizes) else {
|
||||
break;
|
||||
};
|
||||
largest_container_count = largest_container_count.max(previous.len());
|
||||
let scrollbars = measured.auto_scrollbars();
|
||||
if let Some((earlier, _)) = &previous_scrollbars
|
||||
&& changed_scrollbar_axes.extend_changed_axes(earlier, &scrollbars)
|
||||
{
|
||||
last_scrollbar_progress = iterations;
|
||||
}
|
||||
// Keep only the size tables measured at a change in scrollbar
|
||||
// visibility. An exact repeat identifies a feedback cycle, and
|
||||
// only the gutters that toggled within it need to be frozen.
|
||||
let changed = previous_scrollbars
|
||||
.as_ref()
|
||||
.is_none_or(|(earlier, _)| earlier != &scrollbars);
|
||||
match previous_scrollbars.replace((scrollbars, changed)) {
|
||||
Some((earlier, true)) => scrollbar_transitions.push((previous, earlier)),
|
||||
Some((_, false)) => {}
|
||||
let froze_scrollbars = match previous_scrollbars.take() {
|
||||
Some(scrollbars) => feedback.observe(previous, scrollbars)?,
|
||||
None => {
|
||||
frames
|
||||
.original_container_sizes
|
||||
.entry(document)
|
||||
.or_insert(previous);
|
||||
false
|
||||
}
|
||||
}
|
||||
if let Some(start) = repeated
|
||||
&& let Some((current, record_current)) = &mut previous_scrollbars
|
||||
{
|
||||
for (_, earlier) in scrollbar_transitions.drain(start..) {
|
||||
if frozen_scrollbars.extend_changed_axes(current, &earlier) {
|
||||
last_scrollbar_progress = iterations;
|
||||
}
|
||||
}
|
||||
scrollbar_transitions.clear();
|
||||
// The next pass starts a new history with the frozen gutters.
|
||||
*record_current = false;
|
||||
}
|
||||
// Each new scrollbar axis or frozen gutter can start another
|
||||
// propagation through the container chain. Observing the same
|
||||
// axis toggle again grants no additional rounds.
|
||||
let budget = largest_container_count.saturating_mul(2).saturating_add(1);
|
||||
if iterations.saturating_sub(last_scrollbar_progress) >= budget {
|
||||
return Err(
|
||||
moli_layout::LayoutError::ContainerQuerySizesDidNotConverge { iterations },
|
||||
);
|
||||
}
|
||||
iterations += 1;
|
||||
measured = measured.remeasure(services, &frozen_scrollbars, &mut prepare)?;
|
||||
};
|
||||
// The size table returned on the next swap describes this
|
||||
// measurement. Start a fresh checkpoint after changing gutters.
|
||||
previous_scrollbars = (!froze_scrollbars).then(|| measured.auto_scrollbars());
|
||||
feedback.reserve_rebuild(measured.box_count())?;
|
||||
measured =
|
||||
measured.remeasure(services, &feedback.frozen_scrollbars, &mut prepare)?;
|
||||
}
|
||||
finish_measured_layout_pass(measured, &source, &mut frames)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user