nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! `[INV:container-query-zero-alloc-layout]`: the `@container` machinery adds zero heap
//! allocations to layout. On a 202-node card grid (50 cards x 3 leaves) every relayout allocates
//! exactly what its unqueried twin allocates (Taffy's own per-pass flex scratch), never drifts
//! between rounds, and clean frames allocate nothing. Covered stimuli: a viewport resize crossing
//! the 480px breakpoint, a viewport resize crossing none, and a container-only resize (a store
//! edit of the column width with a fixed viewport, like a SplitPane drag).

nativeview_test_support::install_counting_allocator!();
#[path = "support/cq_harness.rs"]
mod cq_harness;

use nativeview_test_support::count_allocs;
use cq_harness::{CardGrid, Harness, Leaves, VIEWPORT, build_card_grid, resize_column};
use nativeview_engine::Viewport;

#[derive(Clone, Copy, Debug)]
enum Stimulus {
    /// Viewport widths (the column is 50% of it).
    Viewport(&'static [f32]),
    /// Column widths written through the store at a fixed viewport.
    Column(&'static [f32]),
}

/// Column 450 / 500 / 480.5 / 479.5 px: every frame crosses the breakpoint.
const CROSSING: Stimulus = Stimulus::Viewport(&[900.0, 1000.0, 961.0, 959.0]);
/// Column 500 .. 504 px: no frame crosses a breakpoint.
const NO_BREAKPOINT: Stimulus = Stimulus::Viewport(&[1002.0, 1004.0, 1006.0, 1008.0]);
const CONTAINER_ONLY: Stimulus = Stimulus::Column(&[450.0, 500.0, 480.5, 479.5]);

struct Fixture {
    h: Harness,
    grid: CardGrid,
}

impl Fixture {
    fn new(containers: bool, leaves: Leaves) -> Self {
        let mut h = Harness::default();
        let grid = build_card_grid(&mut h, 50, 3, containers, leaves);
        Self { h, grid }
    }

    fn frame(&mut self, stimulus: Stimulus, i: usize) {
        match stimulus {
            Stimulus::Viewport(widths) => {
                let width = widths[i % widths.len()];
                assert!(self.h.relayout(Viewport { width, ..VIEWPORT }), "resize to {width}");
            }
            Stimulus::Column(widths) => resize_column(&mut self.h, &self.grid, widths[i % widths.len()]),
        }
    }

    /// Allocations of each frame of one round of `stimulus`.
    fn round(&mut self, stimulus: Stimulus) -> Vec<usize> {
        let n = match stimulus {
            Stimulus::Viewport(w) | Stimulus::Column(w) => w.len(),
        };
        (0..n).map(|i| count_allocs(|| self.frame(stimulus, i)).1).collect()
    }

    /// Two measured rounds after three warm-up rounds.
    fn measure(containers: bool, leaves: Leaves, stimulus: Stimulus) -> (Vec<usize>, Vec<usize>) {
        let mut f = Self::new(containers, leaves);
        for _ in 0..3 {
            f.round(stimulus);
        }
        (f.round(stimulus), f.round(stimulus))
    }
}

fn assert_parity(stimulus: Stimulus, (c, l): (bool, Leaves), (tc, tl): (bool, Leaves)) {
    let (a1, a2) = Fixture::measure(c, l, stimulus);
    let (b1, b2) = Fixture::measure(tc, tl, stimulus);
    assert_eq!(a1, a2, "{l:?} allocations drift between rounds under {stimulus:?}");
    assert_eq!(b1, b2, "{tl:?} allocations drift between rounds under {stimulus:?}");
    assert_eq!(
        a1, b1,
        "{l:?} (containers={c}) must allocate exactly what {tl:?} (containers={tc}) allocates \
         under {stimulus:?}: the container-query machinery adds zero"
    );
}

#[test]
fn counting_alloc_probe_fires_on_this_thread_only() {
    nativeview_test_support::assert_counts_only_this_thread();
}

#[test]
fn query_machinery_overhead_is_zero_allocations() {
    // Same layout work, queries on vs off (incl. containment).
    for stimulus in [CROSSING, NO_BREAKPOINT, CONTAINER_ONLY] {
        assert_parity(stimulus, (true, Leaves::Identical), (false, Leaves::Unqueried));
    }
}

#[test]
fn flipping_rules_allocate_like_their_unqueried_twin() {
    for stimulus in [CROSSING, NO_BREAKPOINT, CONTAINER_ONLY] {
        assert_parity(stimulus, (true, Leaves::Flipping), (true, Leaves::Unqueried));
    }
}

#[test]
fn responsive_typography_allocates_like_plain_text() {
    // Scratch `ParagraphStore` compiles and committed recompiles reuse their buffers.
    for stimulus in [CROSSING, CONTAINER_ONLY] {
        assert_parity(stimulus, (true, Leaves::Typography), (true, Leaves::PlainText));
    }
}

#[test]
fn queries_really_flip_and_clean_frames_allocate_nothing() {
    let mut f = Fixture::new(true, Leaves::Flipping);
    f.frame(CROSSING, 0); // 900px viewport: 450px cards
    assert_eq!(f.h.width(f.grid.leaves[0]), 40.0);
    f.frame(CROSSING, 1); // 1000px viewport: 500px cards
    assert_eq!(f.h.width(f.grid.leaves[0]), 120.0);
    f.frame(CROSSING, 3); // 959px viewport: 479.5px cards
    assert_eq!(f.h.width(f.grid.leaves[0]), 40.0);
    resize_column(&mut f.h, &f.grid, 500.0);
    assert_eq!(f.h.width(f.grid.leaves[0]), 120.0);
    for _ in 0..3 {
        f.round(CROSSING);
    }
    f.h.relayout(Viewport { width: 959.0, ..VIEWPORT });
    let (_, clean) = count_allocs(|| {
        for _ in 0..200 {
            assert!(!f.h.relayout(Viewport { width: 959.0, ..VIEWPORT }));
        }
    });
    assert_eq!(clean, 0, "clean container-query frames allocated {clean} times");
}