Files
ltk/benches/lookup.rs
T
admin 02ab10b3ef
CI / build + test (push) Canceled after 0s
CI / cargo audit (push) Canceled after 0s
State restored on an ordinary launch for apps that ask, focus that follows its widget's id, buttons that do not take the focus when pressed, a focus ring only after the keyboard, and a browser app's surfaces copied rather than transferred, which WebKit got wrong
Restore on launch. `restore_state` follows Android's saved-instance-state contract: the saved bytes come back after a crash or a session restore, and a launch from the launcher starts fresh. Some apps keep state their users expect to find however the last run ended, as an Android browser keeps its tabs in its own storage even when closed from the app switcher. `App::restore_on_launch`, false by default, has the runtime restore on a plain launch too; `SessionRuntime::bootstrap` asks it before deciding. Saving, the file layout and the window geometry are the same either way. Its documentation explains when it is asked, that it is not when persistence is off, that `restore_state` must then merge what the launch itself asks for, such as a URL on the command line, and that what `save_state` returns now outlives every exit; it carries a compiled example. `restore_state`'s documentation, `docs/onboarding.md` and the cookbook's session state recipe mention it.
Focus on a press. `Button::focusable( false )` and `WindowButton` kept out of the Tab cycle but still took the focus when pressed, and Enter then pressed them again, which closed a browser's tab when the user pressed Enter in a page after closing another tab. `LaidOutWidget` gains `focus_on_press`, from `Element::focus_on_press`: a button's `focusable`, a `WindowButton`'s, a pressable's new `Pressable::focus_on_press`, true by default, and true for every other widget. `focus_pressed` replaces `set_focus` for pointer and touch presses and leaves the focus where it was when the widget does not take it. Enter's fallback to the widget under the pointer, which `hovered_idx` follows, skips such widgets too, and a rebuild that leaves the focus on one drops it (`drop_misplaced_focus`). The close buttons of `tab_strip`, on the desktop and on the phone's cards, are not focusable; `symbol_button` takes whether its button is.
Focus by id. The focus was held as the index of a widget in the laid-out tree, so a rebuild that inserted or removed widgets before it moved it to whatever widget took that index. `WidgetId` becomes a name and a key, `WidgetId::keyed( name, key )` telling apart widgets of the same kind, such as the tabs of a strip; `WidgetId( "name" )` still builds one, through a `const fn` of that name, so the calls in every app compile unchanged, and only code that read `.0`, the toasts, the combo and the tooltip here, moves to the derived `Hash` or `name()`. After each layout `follow_focused_id` finds the focused id and moves the focus to its new index with the field's pending text, cursor, selection and scroll; it drops the focus when the widget is gone, keeps it when a scroll only clipped it, and leaves it when the widget at the old index still has the id. `draw_surface` returns whether the focus moved, and its callers draw the surface again rather than marking it done, since the frame just drawn showed the focus in the old place. Widgets without an id keep the index. Each tab and each card of `tab_strip` carries `WidgetId::keyed` with the tab's key, so a chosen tab keeps the focus while others close.
Focus ring. A press that focused a button, a toggle or a list item left its ring drawn, which browsers have not done since `:focus-visible`. `SurfaceState::focus_visible` is set by Tab and Shift+Tab and cleared by a press that moves the focus, `show_focus_ring` redrawing the whole surface when it changes, since the partial redraw only sees indices. The draw is given the focused index only while the ring shows or the widget is a text field, which draws its cursor however it was focused; the focus itself is unchanged, so Enter and Space still press the widget.
Key repeat. A key repeated by the platform rather than by ltk's own timer reached the focused widget even while `App::claims_keyboard` was true; `key_repeated` checks it as `key_press` does.
Browser surfaces. Every surface is painted through the app's one WebGL context on one `OffscreenCanvas`, and was handed to its own canvas, a `bitmaprenderer`, with `transferToImageBitmap`. WebKit reuses the buffer of a bitmap it was handed, so in a WebKit browser each surface showed another's frame, or none, and only the last one painted was right: forge/web's background, dock and windows came and went in a WPE browser while Chromium showed them all. A test page with two canvases fed from one offscreen canvas showed their colours swapped in WPE and right with a copy. Each surface's canvas is now a `2d` one, and `present_copy` copies the offscreen canvas with `drawImage` in `copy` composition, set before every copy since resizing a canvas resets its context, so a transparent frame replaces the last rather than leaving trails behind an animation. Subsurfaces go the same way. `WebSurfaceState` loses `bitmap` and `new` its `gpu` argument, and web-sys loses the `ImageBitmap` and `ImageBitmapRenderingContext` features.
CHANGELOG records each of these.
Not done. The tests that build `LaidOutWidget` by hand set the new field, but no test covers a press on a non-focusable button, the focus following an id or the ring. For one frame after a rebuild moves the focused widget, its ring and its cursor may show in the old place.
2026-11-01 09:19:23 +01:00

156 lines
4.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Per-frame dispatch hot path benches.
//!
//! The renderer rebuilds the widget tree on every frame, then hit-tests
//! pointer events against the resulting flat `Vec<LaidOutWidget>` and
//! looks up handlers by `flat_idx`. Both calls are O(N) in the slice
//! length. Run with `cargo bench --bench lookup`.
use criterion::{ black_box, criterion_group, criterion_main, BenchmarkId, Criterion };
use ltk::test_support::{ find_handlers, find_widget, find_widget_at, LaidOutWidget, WidgetHandlers };
use ltk::{ Point, Rect };
// ── Fixture builders ──────────────────────────────────────────────────────────
/// Build N widgets laid out in a vertical strip. Each widget is 100×30 with a
/// 1px gap, so `find_widget_at(x=50, y=k*31+15)` lands inside widget `k`. Half
/// of them carry a Button handler so `find_handlers` exercises the
/// match-and-clone path on roughly every other lookup.
fn build_widgets( n: usize ) -> Vec<LaidOutWidget<()>>
{
( 0 .. n ).map( |i|
{
let rect = Rect
{
x: 0.0,
y: ( i as f32 ) * 31.0,
width: 100.0,
height: 30.0,
};
let handlers = if i % 2 == 0
{
WidgetHandlers::Button { on_press: Some( () ), on_long_press: None, on_drag_start: None, on_escape: None, repeating: false }
} else {
WidgetHandlers::None
};
LaidOutWidget
{
rect,
flat_idx: i,
id: None,
paint_rect: rect,
handlers,
keyboard_focusable: true,
focus_on_press: true,
cursor: ltk::CursorShape::Default,
tooltip: None,
accessible_label: None,
is_live_region: false,
}
} ).collect()
}
// ── Benches ───────────────────────────────────────────────────────────────────
/// Pointer hit testing on a hot path. Three sub-benches:
/// - last: hit the last widget (worst case under reverse iteration is the
/// *first* widget; the helper iterates from the end to honour
/// "topmost wins", so the last widget in slice order is the cheap
/// one and the first is the expensive one).
/// - first: hit the first widget — full-slice walk.
/// - miss: point that lies outside every rect — also a full-slice walk
/// but never returns early.
fn bench_find_widget_at( c: &mut Criterion )
{
let mut group = c.benchmark_group( "find_widget_at" );
for &n in &[ 10usize, 100, 1000 ]
{
let widgets = build_widgets( n );
// Hit on the last widget in slice order — `find_widget_at` walks in
// reverse, so this returns on the first iteration.
let last_y = ( n as f32 - 1.0 ) * 31.0 + 15.0;
group.bench_with_input( BenchmarkId::new( "hit_last", n ), &n, |b, _|
{
b.iter( ||
{
let p = Point { x: 50.0, y: last_y };
black_box( find_widget_at( black_box( &widgets ), black_box( p ) ) )
} );
} );
// Hit on the first widget — full reverse walk.
group.bench_with_input( BenchmarkId::new( "hit_first", n ), &n, |b, _|
{
b.iter( ||
{
let p = Point { x: 50.0, y: 15.0 };
black_box( find_widget_at( black_box( &widgets ), black_box( p ) ) )
} );
} );
// Miss — point sits to the right of every rect.
group.bench_with_input( BenchmarkId::new( "miss", n ), &n, |b, _|
{
b.iter( ||
{
let p = Point { x: 9_999.0, y: 9_999.0 };
black_box( find_widget_at( black_box( &widgets ), black_box( p ) ) )
} );
} );
}
group.finish();
}
/// `flat_idx` lookup. Linear scan; we measure the worst case (last entry)
/// since it's what bounds latency on real frames.
fn bench_find_widget( c: &mut Criterion )
{
let mut group = c.benchmark_group( "find_widget" );
for &n in &[ 10usize, 100, 1000 ]
{
let widgets = build_widgets( n );
let target = n - 1;
group.bench_with_input( BenchmarkId::new( "last", n ), &n, |b, _|
{
b.iter( ||
{
black_box( find_widget( black_box( &widgets ), black_box( target ) ) )
} );
} );
}
group.finish();
}
/// Same shape as `find_widget` but exercises the handler-cloning path on hit.
/// Useful as a separate measurement because the clone cost grows with the
/// handler payload, even though the scan itself does not.
fn bench_find_handlers( c: &mut Criterion )
{
let mut group = c.benchmark_group( "find_handlers" );
for &n in &[ 10usize, 100, 1000 ]
{
let widgets = build_widgets( n );
let target = n - 1;
group.bench_with_input( BenchmarkId::new( "last", n ), &n, |b, _|
{
b.iter( ||
{
black_box( find_handlers( black_box( &widgets ), black_box( target ) ) )
} );
} );
}
group.finish();
}
criterion_group!( benches, bench_find_widget_at, bench_find_widget, bench_find_handlers );
criterion_main!( benches );