nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Strict `CountingAlloc` zero-allocation guards (`[INV:bench-and-alloc-guards]`) for core
//! `InstantScrollPhysics` nested scroll chaining and Layer-1 CSS scroll anchoring.

nativeview_test_support::install_counting_allocator!();

use std::hint::black_box;

use nativeview_engine::Viewport;
use nativeview_layout::{LaidOutScene, ScrollInputKind, ScrollTouchPhase, TaffyStoreLayout};
use nativeview_store::{ChangeSet, Length, NodeId, NodeStore};
use nativeview_tables::tables::{
    FlexDirection, Layout, OverscrollBehavior, Scroll, ScrollAxes, register_core_tables,
};

use nativeview_test_support::{assert_counts_only_this_thread, count_allocs};

fn build_nested_3_scrollers() -> (NodeStore, LaidOutScene, [NodeId; 3]) {
    register_core_tables();
    let mut store = NodeStore::new();
    let mut changes = ChangeSet::default();
    let ids = {
        let mut tx = store.tx();
        let outer = tx.create();
        let mid = tx.create();
        let inner = tx.create();
        let leaf = tx.create();

        tx.edit::<Layout>(NodeId::ROOT)
            .width(Length::points(400.0))
            .height(Length::points(400.0))
            .flex_direction(FlexDirection::Column);
        for &(n, h) in &[(outer, 400.0), (mid, 900.0), (inner, 1400.0)] {
            tx.edit::<Layout>(n)
                .width(Length::points(400.0))
                .height(Length::points(h))
                .flex_shrink(0.0)
                .flex_direction(FlexDirection::Column);
            tx.edit::<Scroll>(n)
                .axes(ScrollAxes::Both)
                .overscroll_behavior_y(OverscrollBehavior::Auto);
        }
        tx.edit::<Layout>(leaf)
            .width(Length::points(800.0))
            .height(Length::points(2200.0))
            .flex_shrink(0.0);

        tx.set_children(inner, &[leaf]);
        tx.set_children(mid, &[inner]);
        tx.set_children(outer, &[mid]);
        tx.set_children(NodeId::ROOT, &[outer]);
        tx.commit_into(&mut changes);
        [outer, mid, inner]
    };

    let mut scene = LaidOutScene::new(TaffyStoreLayout::new());
    scene.set_viewport(Viewport {
        width: 400.0,
        height: 400.0,
        scale_factor: 1.0,
    });
    scene.commit_frame(&store, &changes);
    let _ = scene.reconcile_scroll_offsets(&store);
    (store, scene, ids)
}

#[test]
fn instant_scroll_chain_nested_3_is_zero_alloc_steady_state() {
    assert_counts_only_this_thread();
    let (_, probe) = count_allocs(|| black_box(vec![1u8; 64]));
    assert!(probe > 0, "CountingAlloc failed positive probe");

    let (store, mut scene, [outer, mid, inner]) = build_nested_3_scrollers();
    let _ = scene.set_scroll_offset_clamped(&store, outer, 0.0, 200.0);
    let _ = scene.set_scroll_offset_clamped(&store, mid, 0.0, 200.0);
    let _ = scene.set_scroll_offset_clamped(&store, inner, 0.0, 0.0);
    let _ = scene.apply_scroll_chain(&store, inner, 0.0, -25.0, ScrollInputKind::Instant);
    let _ = scene.set_scroll_offset_clamped(&store, outer, 0.0, 200.0);
    let _ = scene.set_scroll_offset_clamped(&store, mid, 0.0, 0.0);
    let _ = scene.set_scroll_offset_clamped(&store, inner, 0.0, 0.0);

    let (_, allocs) = count_allocs(|| {
        for i in 0..64u64 {
            let phase = match i {
                0 => ScrollTouchPhase::Started,
                63 => ScrollTouchPhase::Ended,
                _ => ScrollTouchPhase::Moved,
            };
            let out = scene.apply_scroll_chain(
                &store,
                inner,
                0.0,
                if i % 2 == 0 { -4.0 } else { 4.0 },
                ScrollInputKind::SmoothPixels {
                    phase,
                    timestamp_ms: 1000 + i * 8,
                },
            );
            black_box(out);
            black_box(scene.step_scroll_physics(&store, 1.0 / 120.0));
        }
    });
    assert_eq!(
        allocs, 0,
        "steady-state InstantScrollPhysics nested chaining must perform 0 heap allocs, saw {allocs}"
    );
}

#[test]
fn scroll_anchor_capture_and_reconcile_1k_is_zero_alloc_steady_state() {
    register_core_tables();
    let mut store = NodeStore::new();
    let mut changes = ChangeSet::default();
    let (scroller, rows) = {
        let mut tx = store.tx();
        let sc = tx.create();
        tx.edit::<Layout>(NodeId::ROOT)
            .width(Length::points(320.0))
            .height(Length::points(480.0))
            .flex_direction(FlexDirection::Column);
        tx.edit::<Layout>(sc)
            .width(Length::points(320.0))
            .height(Length::points(480.0))
            .flex_direction(FlexDirection::Column);
        tx.edit::<Scroll>(sc).axes(ScrollAxes::Vertical);
        let mut kids = Vec::with_capacity(1000);
        for _ in 0..1000 {
            let r = tx.create();
            tx.edit::<Layout>(r)
                .width(Length::points(320.0))
                .height(Length::points(40.0))
                .flex_shrink(0.0);
            kids.push(r);
        }
        tx.set_children(sc, &kids);
        tx.set_children(NodeId::ROOT, &[sc]);
        tx.commit_into(&mut changes);
        (sc, kids)
    };

    let mut scene = LaidOutScene::new(TaffyStoreLayout::new());
    scene.set_viewport(Viewport {
        width: 320.0,
        height: 480.0,
        scale_factor: 1.0,
    });
    scene.commit_frame(&store, &changes);
    let _ = scene.set_scroll_offset_clamped(&store, scroller, 0.0, 2000.0);
    let _ = scene.reconcile_scroll_offsets(&store);

    {
        let mut tx = store.tx();
        tx.edit::<Layout>(rows[10]).height(Length::points(64.0));
        tx.commit_into(&mut changes);
    }
    scene.commit_frame(&store, &changes);
    let _ = scene.reconcile_scroll_offsets(&store);

    let mut allocs = 0usize;
    for i in 0..32u32 {
        let h = if i % 2 == 0 { 48.0 } else { 72.0 };
        {
            let mut tx = store.tx();
            tx.edit::<Layout>(rows[10]).height(Length::points(h));
            tx.commit_into(&mut changes);
        }
        scene.layout_engine_mut().sync_changes(&store, &changes);
        let (_, cap_allocs) = count_allocs(|| {
            scene.capture_scroll_anchors(&store);
        });
        let vp = scene.viewport();
        let ran = scene.layout_engine_mut().compute_layout_with_interaction(
            &store,
            NodeId::ROOT,
            vp,
            None,
        );
        assert!(ran);
        let (changed_len, rec_allocs) =
            count_allocs(|| scene.reconcile_scroll_offsets(&store).len());
        assert_eq!(changed_len, 1);
        allocs += cap_allocs + rec_allocs;
    }
    assert_eq!(
        allocs, 0,
        "steady-state Layer-1 scroll anchor capture + reconcile must perform 0 heap allocs, saw {allocs}"
    );
}