From 657a2b0b03cf233312043252e352d75bddf5b9fd Mon Sep 17 00:00:00 2001 From: DarkSky Date: Fri, 4 Dec 2026 07:44:01 +0900 Subject: [PATCH] perf(ui): skip paint operations on the rounded clip root scene The rounded clip compiler replays the frame into a scratch root scene. It then replaces the operation stream of that scene with the original stream. Before this change, the root scene recorded each operation again, and the compiler discarded that copy without a read. Add a private flag on `Scene` that stops the recording, and set it only on the scratch root scene. Child clip scenes keep their streams, because the empty-group check reads them. The compiler clears the flag before it returns the scene. --- packages/ui/ui/src/fast/rounded_clip_scene.rs | 80 ++++++++++++++++++- packages/ui/ui/src/scene.rs | 16 ++-- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/packages/ui/ui/src/fast/rounded_clip_scene.rs b/packages/ui/ui/src/fast/rounded_clip_scene.rs index 466e2b3028..7b50405f3e 100644 --- a/packages/ui/ui/src/fast/rounded_clip_scene.rs +++ b/packages/ui/ui/src/fast/rounded_clip_scene.rs @@ -38,7 +38,10 @@ pub(crate) fn compile(scene: &mut Scene) { scene.layers.frames = layers.clone(); scene }; - let mut stack = vec![new_scene()]; + // The root records no operations: its stream gets the original below. + let mut root = new_scene(); + root.skip_paint_operations = true; + let mut stack = vec![root]; let mut shapes = Vec::new(); let mut redundant = Vec::new(); for operation in &operations { @@ -109,6 +112,7 @@ pub(crate) fn compile(scene: &mut Scene) { "balanced rounded clips" ); let mut compiled = stack.pop().unwrap(); + compiled.skip_paint_operations = false; compiled.paint_operations = operations; compiled.fast_rounded_clip = mem::take(&mut scene.fast_rounded_clip); compiled.fast_rounded_clip.compiled = true; @@ -134,3 +138,77 @@ fn geometry(shape: Shape) -> LayerFrame { dirty_tiles: Vec::new(), } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::fast::rounded_clip::record; + use crate::{Bounds, Corners, Quad, ScaledPixels as S, Size, hsla, point}; + + /// `A`/`B` open a uniform/asymmetric clip, `)` closes it, `[`/`]` wrap a + /// layer, `t` paints a translucent quad and any other byte an opaque one. + fn paint(scene: &mut Scene, script: &str) { + for (i, op) in script.bytes().enumerate() { + let k = if op == b'A' { 0. } else { 4. }; + let r = |v: f32| Size::new(S(8. + v * k), S(4. + v * k)); + let radii = Corners { + top_left: r(-1.), + top_right: r(0.), + bottom_right: r(1.), + bottom_left: r(2.), + }; + let shape = Shape { + bounds: Bounds::new(point(S(0.), S(0.)), Size::new(S(90.), S(60.))), + radii, + }; + let at = Bounds::new(point(S(i as f32 * 7.), S(5.)), Size::new(S(30.), S(20.))); + match op { + b'A' | b'B' => record(scene, ClipOperation::Begin(shape)), + b')' => record(scene, ClipOperation::End), + b'[' => scene.push_layer(at), + b']' => scene.pop_layer(), + _ => scene.insert_primitive(Quad { + bounds: at, + content_mask: crate::scene::unbounded_scaled_content_mask(), + background: hsla(0.6, 0.5, 0.5, if op == b't' { 0.5 } else { 1. }).into(), + ..Default::default() + }), + } + } + } + + fn dump(scene: &Scene) -> String { + let mut out = format!("{:?}{:?}", scene.quads, scene.polychrome_sprites); + for frame in &scene.layers.frames { + out += &format!("{:?}{:?}{}", frame.key, frame.background, frame.tile_size); + out += &frame.content.scene().map_or(String::new(), dump); + } + out + } + + #[test] + fn compile_restores_operations_and_matches_split_replay() { + for (script, root_groups) in [("qAqAq)q)t", 1), ("AqBq))", 1), ("A)q", 0), ("tA[qt])", 1)] { + let mut scene = Scene::default(); + paint(&mut scene, script); + let (operations, len) = (scene.paint_operations.as_ptr(), scene.len()); + scene.finish(); + assert!(scene.paint_operations.as_ptr() == operations && scene.len() == len); + assert!(!scene.skip_paint_operations); + let groups = scene + .polychrome_sprites + .iter() + .filter(|s| s.tile.texture_id.index >= CLIP_TEXTURE_BASE); + assert_eq!(groups.count(), root_groups, "{script}"); + let mut next = Scene::default(); + next.replay(0..len / 2, &scene); + next.replay(len / 2..len, &scene); + next.finish(); + assert_eq!(dump(&next), dump(&scene), "{script}"); + } + let mut root = Scene::default(); + root.skip_paint_operations = true; + paint(&mut root, "[q]"); + assert_eq!((root.paint_operations.capacity(), root.quads.len()), (0, 1)); + } +} diff --git a/packages/ui/ui/src/scene.rs b/packages/ui/ui/src/scene.rs index 0ae2ee05f4..f468d79870 100644 --- a/packages/ui/ui/src/scene.rs +++ b/packages/ui/ui/src/scene.rs @@ -42,6 +42,8 @@ pub struct Scene { pub(crate) sort_scratch: crate::fast::scene::SortScratch, pub(crate) fast_rounded_clip: crate::fast::rounded_clip::SceneClips, pub(crate) layers: SceneLayers, + /// Set on a scratch scene whose operation stream the caller discards. + pub(crate) skip_paint_operations: bool, } #[expect(missing_docs)] @@ -69,13 +71,18 @@ impl Scene { pub fn push_layer(&mut self, bounds: Bounds) { let order = self.primitive_bounds.insert(bounds); self.layer_stack.push(order); - self.paint_operations - .push(PaintOperation::StartLayer(bounds)); + self.record(PaintOperation::StartLayer(bounds)); } pub fn pop_layer(&mut self) { self.layer_stack.pop(); - self.paint_operations.push(PaintOperation::EndLayer); + self.record(PaintOperation::EndLayer); + } + + fn record(&mut self, operation: PaintOperation) { + if !self.skip_paint_operations { + self.paint_operations.push(operation); + } } pub fn insert_primitive(&mut self, primitive: impl Into) { @@ -128,8 +135,7 @@ impl Scene { self.surfaces.push(surface.clone()); } } - self.paint_operations - .push(PaintOperation::Primitive(primitive)); + self.record(PaintOperation::Primitive(primitive)); } pub fn replay(&mut self, range: Range, prev_scene: &Scene) {