Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Add randomized element tree benchmarks and fix Linux benchmark leaks (#64842)
Anthony Eid3135cdf8dc8e
diff --git a/Cargo.lock b/Cargo.lockindex 07f7172ded4..0f966f61ef0 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -7712,6 +7712,7 @@ name = "gpui_platform"version = "0.1.0"dependencies = ["anyhow",+ "bench_metrics","console_error_panic_hook","criterion","gpui",diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rsindex 5711f81c516..d4da5aa34c0 100644--- a/crates/gpui/src/gpui.rs+++ b/crates/gpui/src/gpui.rs@@ -46,6 +46,9 @@ pub mod profiler;))]#[expect(missing_docs)]pub mod queue;+/// A seeded, mutable element tree for benchmarks and tests of GPUI rendering.+#[cfg(any(test, feature = "bench-support"))]+pub mod randomized_element_tree;mod scene;#[cfg(any(test, feature = "test-support", feature = "bench-support"))]mod seeds;diff --git a/crates/gpui/src/queue.rs b/crates/gpui/src/queue.rsindex 3e02b1359d9..6d921309cba 100644--- a/crates/gpui/src/queue.rs+++ b/crates/gpui/src/queue.rs@@ -83,12 +83,12 @@ impl<T> PriorityQueueState<T> {fn recv<'a>(&'a self) -> Result<MutexGuard<'a, PriorityQueues<T>>, RecvError> {let mut queues = self.queues.lock().unwrap_or_else(PoisonError::into_inner);- let sender_count = self.sender_count.load(std::sync::atomic::Ordering::Relaxed);- if queues.is_empty() && sender_count == 0 {- return Err(crate::queue::RecvError);- }-+ // Re-checked after every wake: dropping the last sender wakes waiting receivers+ // so they can end instead of blocking forever on a queue nothing can fill.while queues.is_empty() {+ if self.sender_count.load(std::sync::atomic::Ordering::Acquire) == 0 {+ return Err(crate::queue::RecvError);+ }queues = self.condvar.wait(queues)@@ -158,9 +158,24 @@ impl<T> PriorityQueueSender<T> {impl<T> Drop for PriorityQueueSender<T> {fn drop(&mut self) {- self.state+ let previous = self+ .state.sender_count.fetch_sub(1, std::sync::atomic::Ordering::AcqRel);+ if previous == 1 {+ // A receiver checks the sender count and starts waiting under the queue lock,+ // atomically. Acquiring that lock once, even with nothing to do under it,+ // means any receiver that saw a live sender is already waiting, so the+ // notification below reaches it; without this, a receiver between its check+ // and its wait would miss the wake-up and block forever.+ drop(+ self.state+ .queues+ .lock()+ .unwrap_or_else(PoisonError::into_inner),+ );+ self.state.condvar.notify_all();+ }}}@@ -414,6 +429,44 @@ mod tests {use super::*;+ #[test]+ fn blocked_receivers_end_when_the_sender_drops() {+ let (sender, receiver) = PriorityQueueReceiver::<u32>::new();+ let (done_sender, done_receiver) = std::sync::mpsc::channel();+ let workers = (0..2)+ .map(|_| {+ let receiver = receiver.clone();+ let done_sender = done_sender.clone();+ std::thread::spawn(move || {+ let received: Vec<u32> = receiver.iter().collect();+ done_sender.send(received).ok();+ })+ })+ .collect::<Vec<_>>();+ drop(receiver);++ sender.send(Priority::Medium, 7).unwrap();+ std::thread::sleep(std::time::Duration::from_millis(50));+ assert!(+ done_receiver.try_recv().is_err(),+ "receivers keep waiting while the sender is alive"+ );++ drop(sender);+ let mut received = Vec::new();+ for _ in 0..2 {+ received.extend(+ done_receiver+ .recv_timeout(std::time::Duration::from_secs(5))+ .expect("a waiting receiver should end once the sender is dropped"),+ );+ }+ assert_eq!(received, [7]);+ for worker in workers {+ worker.join().expect("worker should exit cleanly");+ }+ }+#[test]fn all_tasks_get_yielded() {let (tx, mut rx) = PriorityQueueReceiver::new();diff --git a/crates/gpui/src/randomized_element_tree.rs b/crates/gpui/src/randomized_element_tree.rsnew file mode 100644index 00000000000..1366317bc6e--- /dev/null+++ b/crates/gpui/src/randomized_element_tree.rs@@ -0,0 +1,1745 @@+use crate::{+ AnyElement, AppContext as _, Context, ElementId, Entity, Hsla, InteractiveElement, IntoElement,+ ParentElement, Render, StatefulInteractiveElement, Styled, Window, div, px,+ util::FluentBuilder,+};+use collections::{HashMap, HashSet};+use rand::{Rng as _, SeedableRng as _, rngs::StdRng};+use std::{+ cell::RefCell,+ ops::RangeInclusive,+ rc::{Rc, Weak},+};++/// Dispatched to no one: the action generated elements register handlers for. Not+/// registered, so builds that enable `bench-support` through feature unification (such as+/// the workspace's tests) don't gain an action namespace.+#[derive(Clone, Default, PartialEq, crate::Action)]+#[action(namespace = randomized_element_tree, no_json, no_register)]+pub struct RandomizedElementTreeAction;++/// The initial shape generated for a randomized element tree.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum RandomizedElementTreeTopology {+ /// Every generated element is a direct child of the root.+ Wide,+ /// Every generated element is the child of the previous element.+ Deep,+ /// Generated elements choose parents throughout the existing tree.+ Mixed,+ /// Generated elements usually extend one deep branch, with occasional root children.+ Narrow,+}++impl RandomizedElementTreeTopology {+ /// Every topology.+ pub const ALL: [Self; 4] = [Self::Wide, Self::Deep, Self::Mixed, Self::Narrow];++ /// Whether the tree's depth grows with its element count. `Deep` nests every element+ /// under the previous one and `Narrow` nests four in five, so both recurse once per+ /// element through layout and paint; without gpui's `stacker` feature that overflows+ /// a 2 MB thread stack somewhere between 384 and 512 elements in a release build.+ pub fn is_tall(self) -> bool {+ matches!(self, Self::Deep | Self::Narrow)+ }++ fn from_seed(seed: u64) -> Self {+ match seed % 4 {+ 0 => Self::Wide,+ 1 => Self::Deep,+ 2 => Self::Mixed,+ _ => Self::Narrow,+ }+ }+}++/// Configuration for a reproducible randomized element-tree workload.+#[derive(Clone, Copy, Debug, PartialEq)]+pub struct RandomizedElementTreeConfig {+ seed: u64,+ element_count: usize,+ topology: RandomizedElementTreeTopology,+ entity_density: f64,+ handler_density: f64,+}++impl RandomizedElementTreeConfig {+ /// Creates an element-only workload whose topology is selected by `seed`.+ pub fn new(seed: u64, element_count: usize) -> Self {+ Self {+ seed,+ element_count,+ topology: RandomizedElementTreeTopology::from_seed(seed),+ entity_density: 0.0,+ handler_density: 0.0,+ }+ }++ /// Selects the initial tree topology independently of the RNG seed.+ pub fn with_topology(mut self, topology: RandomizedElementTreeTopology) -> Self {+ self.topology = topology;+ self+ }++ /// Sets the probability that each generated element is backed by a persistent entity.+ pub fn with_entity_density(mut self, density: f64) -> Self {+ self.entity_density = normalized_density(density);+ self+ }++ /// Sets the probability of each generated interaction feature on an element.+ pub fn with_handler_density(mut self, density: f64) -> Self {+ self.handler_density = normalized_density(density);+ self+ }++ /// Returns the seed used for generation and subsequent random mutations.+ pub fn seed(&self) -> u64 {+ self.seed+ }++ /// Returns the requested number of initial descendants.+ pub fn element_count(&self) -> usize {+ self.element_count+ }++ /// Returns the selected initial topology.+ pub fn topology(&self) -> RandomizedElementTreeTopology {+ self.topology+ }++ /// Returns the probability that a generated element is backed by an entity.+ pub fn entity_density(&self) -> f64 {+ self.entity_density+ }++ /// Returns the probability of each generated interaction feature.+ pub fn handler_density(&self) -> f64 {+ self.handler_density+ }+}++/// Bounds an RNG is drawn against to produce a [`RandomizedElementTreeConfig`]: a family+/// of trees, such as "tall trees of 32 to 256 elements with up to half of them entities",+/// from which one draw picks one member. Every field of the resulting config, and the+/// tree built from it, follows from the RNG's state, so an RNG seeded by `#[gpui::bench]`+/// or `#[gpui::test]` samples the family across seeds and any one seed reproduces exactly.+#[derive(Clone, Debug, PartialEq)]+pub struct RandomizedElementTreeBounds {+ topologies: Vec<RandomizedElementTreeTopology>,+ element_count: RangeInclusive<usize>,+ entity_density: RangeInclusive<f64>,+ handler_density: RangeInclusive<f64>,+}++impl RandomizedElementTreeBounds {+ /// Trees of any topology with `element_count` elements, no entities and no handlers.+ pub fn new(element_count: RangeInclusive<usize>) -> Self {+ Self {+ topologies: RandomizedElementTreeTopology::ALL.to_vec(),+ element_count,+ entity_density: 0.0..=0.0,+ handler_density: 0.0..=0.0,+ }+ }++ /// Restricts the topologies a seed may pick. An empty list keeps them all.+ pub fn with_topologies(+ mut self,+ topologies: impl IntoIterator<Item = RandomizedElementTreeTopology>,+ ) -> Self {+ let topologies: Vec<_> = topologies.into_iter().collect();+ if !topologies.is_empty() {+ self.topologies = topologies;+ }+ self+ }++ /// The range a seed draws the share of entity-backed elements from.+ pub fn with_entity_density(mut self, density: RangeInclusive<f64>) -> Self {+ self.entity_density =+ normalized_density(*density.start())..=normalized_density(*density.end());+ self+ }++ /// The range a seed draws the per-feature handler probability from.+ pub fn with_handler_density(mut self, density: RangeInclusive<f64>) -> Self {+ self.handler_density =+ normalized_density(*density.start())..=normalized_density(*density.end());+ self+ }++ /// Draws one config from these bounds. The config's own seed, which drives the tree's+ /// contents and mutation stream, is drawn from `rng` too, so all of it reproduces from+ /// the seed `rng` started from.+ pub fn sample(&self, rng: &mut StdRng) -> RandomizedElementTreeConfig {+ let topology = self.topologies[rng.random_range(0..self.topologies.len())];+ let element_count = rng.random_range(self.element_count.clone());+ RandomizedElementTreeConfig::new(rng.random(), element_count)+ .with_topology(topology)+ .with_entity_density(sample_range(&self.entity_density, rng))+ .with_handler_density(sample_range(&self.handler_density, rng))+ }+}++fn sample_range(range: &RangeInclusive<f64>, rng: &mut StdRng) -> f64 {+ if range.start() >= range.end() {+ *range.start()+ } else {+ rng.random_range(range.clone())+ }+}++/// Work performed while converting randomized entities and elements into an element tree.+#[derive(Clone, Debug, Default, Eq, PartialEq)]+pub struct RandomizedElementTreeWorkCounters {+ root_render_count: usize,+ entity_render_count: usize,+ element_render_count: usize,+ handler_registration_count: usize,+ rendered_entity_ids: Vec<u64>,+}++impl RandomizedElementTreeWorkCounters {+ /// Returns how many times the persistent root's `Render` implementation ran.+ pub fn root_render_count(&self) -> usize {+ self.root_render_count+ }++ /// Returns how many randomized child entities rendered.+ pub fn entity_render_count(&self) -> usize {+ self.entity_render_count+ }++ /// Returns how many randomized descendant elements were reconstructed.+ pub fn element_render_count(&self) -> usize {+ self.element_render_count+ }++ /// Returns how many interaction handlers were registered while rendering.+ pub fn handler_registration_count(&self) -> usize {+ self.handler_registration_count+ }++ /// Returns stable IDs for the randomized child entities that rendered, in render order.+ pub fn rendered_entity_ids(&self) -> &[u64] {+ &self.rendered_entity_ids+ }++ fn reset(&mut self) {+ self.root_render_count = 0;+ self.entity_render_count = 0;+ self.element_render_count = 0;+ self.handler_registration_count = 0;+ self.rendered_entity_ids.clear();+ }+}++/// A cloneable description of a randomized element tree's renderable state.+#[derive(Clone, Debug, PartialEq)]+pub struct RandomizedElementTreeSnapshot {+ root: RandomizedElementStyle,+ children: Vec<RandomizedElementNode>,+ next_element_id: u64,+ paths_by_element_id: HashMap<u64, Vec<usize>>,+}++impl RandomizedElementTreeSnapshot {+ /// Returns the number of randomized elements below the persistent root.+ pub fn descendant_count(&self) -> usize {+ let mut count = 0;+ let mut stack = self.children.iter().collect::<Vec<_>>();+ while let Some(node) = stack.pop() {+ count += 1;+ stack.extend(node.children.iter());+ }+ count+ }++ /// Returns the deepest randomized element below the persistent root.+ ///+ /// A snapshot with no descendants has depth zero.+ pub fn max_depth(&self) -> usize {+ let mut max_depth = 0;+ let mut stack = self+ .children+ .iter()+ .map(|node| (node, 1))+ .collect::<Vec<_>>();+ while let Some((node, depth)) = stack.pop() {+ max_depth = max_depth.max(depth);+ stack.extend(node.children.iter().map(|child| (child, depth + 1)));+ }+ max_depth+ }++ /// Returns the stable element IDs in depth-first order.+ pub fn element_ids(&self) -> Vec<u64> {+ node_paths(&self.children)+ .iter()+ .filter_map(|path| node_at_path(&self.children, path).map(|node| node.id))+ .collect()+ }++ /// Returns the number of descendants backed by persistent child entities.+ pub fn entity_count(&self) -> usize {+ let mut count = 0;+ let mut stack = self.children.iter().collect::<Vec<_>>();+ while let Some(node) = stack.pop() {+ count += usize::from(node.is_entity);+ stack.extend(node.children.iter());+ }+ count+ }++ /// Returns the number of interaction handlers in the generated tree.+ pub fn handler_count(&self) -> usize {+ let mut count = 0;+ let mut stack = self.children.iter().collect::<Vec<_>>();+ while let Some(node) = stack.pop() {+ count += node.features.handler_count();+ stack.extend(node.children.iter());+ }+ count+ }++ fn rebuild_element_paths(&mut self) {+ self.paths_by_element_id = node_paths(&self.children)+ .into_iter()+ .filter_map(|path| node_at_path(&self.children, &path).map(|node| (node.id, path)))+ .collect();+ }++ fn node(&self, element_id: u64) -> Option<&RandomizedElementNode> {+ node_at_path(&self.children, self.paths_by_element_id.get(&element_id)?)+ }+}++/// A requested class of deterministic randomized-tree mutation.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum RandomizedElementTreeMutationKind {+ /// Change the root background color.+ RootColor,+ /// Change the root opacity.+ RootOpacity,+ /// Change the root border.+ RootBorder,+ /// Change the root width and height.+ RootBounds,+ /// Toggle clipping on the root.+ RootClipping,+ /// Toggle root visibility.+ RootVisibility,+ /// Change a seeded random descendant's colors.+ ChildColor,+ /// Change a seeded random descendant's opacity.+ ChildOpacity,+ /// Change a seeded random descendant's border.+ ChildBorder,+ /// Change a seeded random descendant's width and height.+ ChildBounds,+ /// Toggle clipping on a seeded random descendant.+ ChildClipping,+ /// Toggle visibility on a seeded random descendant.+ ChildVisibility,+ /// Reorder a seeded random descendant among its siblings.+ Reorder,+ /// Insert a generated descendant below a seeded random parent.+ Insert,+ /// Remove a seeded random descendant and its subtree.+ Remove,+ /// Remove a seeded random childless descendant. Unlike [`Self::Remove`] this changes+ /// the element count by exactly one, so it can alternate with [`Self::Insert`] in a+ /// long benchmark loop without the tree collapsing or growing.+ RemoveLeaf,+}++impl RandomizedElementTreeMutationKind {+ /// Every supported mutation kind, suitable for parameterized tests and benchmarks.+ pub const ALL: [Self; 16] = [+ Self::RootColor,+ Self::RootOpacity,+ Self::RootBorder,+ Self::RootBounds,+ Self::RootClipping,+ Self::RootVisibility,+ Self::ChildColor,+ Self::ChildOpacity,+ Self::ChildBorder,+ Self::ChildBounds,+ Self::ChildClipping,+ Self::ChildVisibility,+ Self::Reorder,+ Self::Insert,+ Self::Remove,+ Self::RemoveLeaf,+ ];+}++/// A deterministic mutation applied to a [`RandomizedElementTree`].+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum RandomizedElementTreeMutation {+ /// Changed the root background color.+ RootColor,+ /// Changed the root opacity.+ RootOpacity,+ /// Changed the root border.+ RootBorder,+ /// Changed the root width and height.+ RootBounds,+ /// Toggled clipping on the root.+ RootClipping,+ /// Toggled root visibility.+ RootVisibility,+ /// Changed a descendant's colors.+ ChildColor {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Changed a descendant's opacity.+ ChildOpacity {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Changed a descendant's border.+ ChildBorder {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Changed a descendant's width and height.+ ChildBounds {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Toggled clipping on a descendant.+ ChildClipping {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Toggled a descendant's visibility.+ ChildVisibility {+ /// The stable ID of the mutated element.+ element_id: u64,+ },+ /// Moved an element to a different position among its siblings.+ Reordered {+ /// The stable ID of the moved element.+ element_id: u64,+ /// The stable ID of its parent, or `None` for the persistent root.+ parent_id: Option<u64>,+ /// The element's previous position.+ old_index: usize,+ /// The element's new position.+ new_index: usize,+ },+ /// Inserted a newly generated element.+ Inserted {+ /// The stable ID of the inserted element.+ element_id: u64,+ /// The stable ID of its parent, or `None` for the persistent root.+ parent_id: Option<u64>,+ /// The position of the inserted element.+ index: usize,+ },+ /// Removed an element and all of its descendants.+ Removed {+ /// The stable ID of the removed element.+ element_id: u64,+ /// The stable ID of its parent, or `None` for the persistent root.+ parent_id: Option<u64>,+ /// The former position of the removed element.+ index: usize,+ /// The total number of elements removed with the subtree.+ removed_element_count: usize,+ },+}++/// Where the elements changed by one frame sit relative to each other.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum ChangeLocality {+ /// Inside one random entity's subtree, as a user action or one component's state+ /// change would be; the change reaches the rest of the tree only if the subtree has+ /// fewer elements than are changing (a common ancestor changing).+ Localized,+ /// Anywhere in the tree, uniformly: the worst case for a renderer that reuses clean+ /// subtrees, since every changed element may sit in a different one.+ Spread,+}++/// What [`RandomizedElementTree::recolor_children`] changed.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub struct RecoloredChildren {+ /// Descendants whose background changed.+ pub recolored: usize,+ /// Entity boundaries (or the root) notified, each once.+ pub notified: usize,+}++/// A persistent root view for randomized GPUI element-tree tests and benchmarks.+pub struct RandomizedElementTree {+ snapshot: Rc<RefCell<RandomizedElementTreeSnapshot>>,+ entities: Rc<RefCell<RandomizedElementEntities>>,+ work_counters: Rc<RefCell<RandomizedElementTreeWorkCounters>>,+ config: RandomizedElementTreeConfig,+ rng: StdRng,+}++impl RandomizedElementTree {+ /// Generates a tree with `element_count` descendants and a mutation stream from `seed`.+ ///+ /// Different seeds deliberately produce wide, deep, mixed, and narrow initial topologies.+ pub fn new(seed: u64, element_count: usize) -> Self {+ Self::from_config(RandomizedElementTreeConfig::new(seed, element_count))+ }++ /// Generates a configured tree and creates its persistent child entities.+ pub fn new_with_config(config: RandomizedElementTreeConfig, cx: &mut Context<Self>) -> Self {+ let mut tree = Self::from_config(config);+ tree.synchronize_entities(cx);+ tree+ }++ fn from_config(config: RandomizedElementTreeConfig) -> Self {+ let mut rng = StdRng::seed_from_u64(config.seed);+ let mut children = Vec::new();+ let mut deepest_path = Vec::new();++ for element_index in 0..config.element_count {+ let mut parent_path = match config.topology {+ RandomizedElementTreeTopology::Wide => Vec::new(),+ RandomizedElementTreeTopology::Deep => deepest_path.clone(),+ RandomizedElementTreeTopology::Mixed => random_parent_path(&children, &mut rng),+ RandomizedElementTreeTopology::Narrow => {+ if rng.random_bool(0.8) {+ deepest_path.clone()+ } else {+ Vec::new()+ }+ }+ };+ let node = RandomizedElementNode::random(+ element_index as u64,+ config.entity_density,+ config.handler_density,+ &mut rng,+ );+ let (_, inserted_path) = insert_node(&mut children, &mut parent_path, node);+ deepest_path = inserted_path;+ }++ let mut snapshot = RandomizedElementTreeSnapshot {+ root: RandomizedElementStyle::random_root(&mut rng),+ children,+ next_element_id: config.element_count as u64,+ paths_by_element_id: HashMap::default(),+ };+ snapshot.rebuild_element_paths();++ Self {+ snapshot: Rc::new(RefCell::new(snapshot)),+ entities: Rc::new(RefCell::new(HashMap::default())),+ work_counters: Rc::new(RefCell::new(RandomizedElementTreeWorkCounters::default())),+ config,+ rng,+ }+ }++ /// Reconstructs a tree from renderable state captured by [`Self::snapshot`].+ pub fn from_snapshot(snapshot: RandomizedElementTreeSnapshot) -> Self {+ Self {+ config: RandomizedElementTreeConfig::new(0, snapshot.descendant_count()),+ snapshot: Rc::new(RefCell::new(snapshot)),+ entities: Rc::new(RefCell::new(HashMap::default())),+ work_counters: Rc::new(RefCell::new(RandomizedElementTreeWorkCounters::default())),+ rng: StdRng::seed_from_u64(0),+ }+ }++ /// Reconstructs a snapshot while preserving its randomized entity boundaries.+ pub fn from_snapshot_with_entities(+ snapshot: RandomizedElementTreeSnapshot,+ cx: &mut Context<Self>,+ ) -> Self {+ let mut tree = Self::from_snapshot(snapshot);+ tree.synchronize_entities(cx);+ tree+ }++ /// Captures all state that affects rendering and future element IDs.+ pub fn snapshot(&self) -> RandomizedElementTreeSnapshot {+ self.snapshot.borrow().clone()+ }++ /// Clears render work accumulated since construction or the previous reset.+ pub fn reset_work_counters(&mut self) {+ self.work_counters.borrow_mut().reset();+ }++ /// Returns work accumulated since construction or the previous reset.+ pub fn work_counters(&self) -> RandomizedElementTreeWorkCounters {+ self.work_counters.borrow().clone()+ }++ /// Applies one seeded random style or structural mutation.+ ///+ /// This does not notify persistent child entities. Entity-aware callers should use+ /// [`Self::apply_random_mutation`] from an entity update instead.+ pub fn mutate(&mut self) -> RandomizedElementTreeMutation {+ let kind = RandomizedElementTreeMutationKind::ALL[self+ .rng+ .random_range(0..RandomizedElementTreeMutationKind::ALL.len())];+ self.apply_mutation_without_notification(kind).mutation+ }++ /// Applies one seeded random mutation and notifies its nearest persistent entity boundary.+ pub fn apply_random_mutation(+ &mut self,+ cx: &mut Context<Self>,+ ) -> RandomizedElementTreeMutation {+ let kind = RandomizedElementTreeMutationKind::ALL[self+ .rng+ .random_range(0..RandomizedElementTreeMutationKind::ALL.len())];+ self.apply_mutation(kind, cx)+ }++ /// Applies a selected mutation kind and notifies its nearest persistent entity boundary.+ pub fn apply_mutation(+ &mut self,+ kind: RandomizedElementTreeMutationKind,+ cx: &mut Context<Self>,+ ) -> RandomizedElementTreeMutation {+ let applied = self.apply_mutation_without_notification(kind);+ if applied.synchronize_entities {+ self.synchronize_entities(cx);+ }+ self.notify_owner(applied.owner_entity_id, cx);+ applied.mutation+ }++ /// Generates and inserts an element below a seeded random parent.+ ///+ /// This does not synchronize or notify persistent child entities. Entity-aware callers should+ /// use [`Self::apply_mutation`] with [`RandomizedElementTreeMutationKind::Insert`].+ pub fn insert_child(&mut self) -> RandomizedElementTreeMutation {+ self.insert_child_internal().mutation+ }++ /// Removes a seeded random element and its subtree.+ ///+ /// This does not synchronize or notify persistent child entities. Entity-aware callers should+ /// use [`Self::apply_mutation`] with [`RandomizedElementTreeMutationKind::Remove`].+ pub fn remove_child(&mut self) -> Option<RandomizedElementTreeMutation> {+ self.remove_child_internal(false)+ .map(|applied| applied.mutation)+ }++ fn apply_mutation_without_notification(+ &mut self,+ kind: RandomizedElementTreeMutationKind,+ ) -> AppliedRandomizedElementTreeMutation {+ match kind {+ RandomizedElementTreeMutationKind::RootColor => {+ self.snapshot.borrow_mut().root.background = random_color(&mut self.rng);+ AppliedRandomizedElementTreeMutation::root(RandomizedElementTreeMutation::RootColor)+ }+ RandomizedElementTreeMutationKind::RootOpacity => {+ self.snapshot.borrow_mut().root.opacity = random_opacity(&mut self.rng);+ AppliedRandomizedElementTreeMutation::root(+ RandomizedElementTreeMutation::RootOpacity,+ )+ }+ RandomizedElementTreeMutationKind::RootBorder => {+ self.snapshot+ .borrow_mut()+ .root+ .randomize_border(&mut self.rng);+ AppliedRandomizedElementTreeMutation::root(+ RandomizedElementTreeMutation::RootBorder,+ )+ }+ RandomizedElementTreeMutationKind::RootBounds => {+ let mut snapshot = self.snapshot.borrow_mut();+ snapshot.root.width = self.rng.random_range(520.0..980.0);+ snapshot.root.height = self.rng.random_range(360.0..760.0);+ AppliedRandomizedElementTreeMutation::root(+ RandomizedElementTreeMutation::RootBounds,+ )+ }+ RandomizedElementTreeMutationKind::RootClipping => {+ let mut snapshot = self.snapshot.borrow_mut();+ snapshot.root.clipped = !snapshot.root.clipped;+ AppliedRandomizedElementTreeMutation::root(+ RandomizedElementTreeMutation::RootClipping,+ )+ }+ RandomizedElementTreeMutationKind::RootVisibility => {+ let mut snapshot = self.snapshot.borrow_mut();+ snapshot.root.visible = !snapshot.root.visible;+ AppliedRandomizedElementTreeMutation::root(+ RandomizedElementTreeMutation::RootVisibility,+ )+ }+ RandomizedElementTreeMutationKind::ChildColor => {+ self.mutate_random_child(RandomizedChildMutation::Color)+ }+ RandomizedElementTreeMutationKind::ChildOpacity => {+ self.mutate_random_child(RandomizedChildMutation::Opacity)+ }+ RandomizedElementTreeMutationKind::ChildBorder => {+ self.mutate_random_child(RandomizedChildMutation::Border)+ }+ RandomizedElementTreeMutationKind::ChildBounds => {+ self.mutate_random_child(RandomizedChildMutation::Bounds)+ }+ RandomizedElementTreeMutationKind::ChildClipping => {+ self.mutate_random_child(RandomizedChildMutation::Clipping)+ }+ RandomizedElementTreeMutationKind::ChildVisibility => {+ self.mutate_random_child(RandomizedChildMutation::Visibility)+ }+ RandomizedElementTreeMutationKind::Reorder => self.reorder_child(),+ RandomizedElementTreeMutationKind::Insert => self.insert_child_internal(),+ RandomizedElementTreeMutationKind::Remove => self+ .remove_child_internal(false)+ .unwrap_or_else(|| self.insert_child_internal()),+ RandomizedElementTreeMutationKind::RemoveLeaf => self+ .remove_child_internal(true)+ .unwrap_or_else(|| self.insert_child_internal()),+ }+ }++ fn insert_child_internal(&mut self) -> AppliedRandomizedElementTreeMutation {+ let mut snapshot = self.snapshot.borrow_mut();+ let element_id = snapshot.next_element_id;+ snapshot.next_element_id += 1;+ let mut parent_path = random_parent_path(&snapshot.children, &mut self.rng);+ let parent_id = node_at_path(&snapshot.children, &parent_path).map(|node| node.id);+ let owner_entity_id = nearest_entity_id(&snapshot.children, &parent_path);+ let node = RandomizedElementNode::random(+ element_id,+ self.config.entity_density,+ self.config.handler_density,+ &mut self.rng,+ );+ let (index, _) = insert_node(&mut snapshot.children, &mut parent_path, node);+ snapshot.rebuild_element_paths();++ AppliedRandomizedElementTreeMutation {+ mutation: RandomizedElementTreeMutation::Inserted {+ element_id,+ parent_id,+ index,+ },+ owner_entity_id,+ synchronize_entities: true,+ }+ }++ fn remove_child_internal(+ &mut self,+ leaves_only: bool,+ ) -> Option<AppliedRandomizedElementTreeMutation> {+ let mut snapshot = self.snapshot.borrow_mut();+ let path = if leaves_only {+ random_leaf_path(&snapshot.children, &mut self.rng)?+ } else {+ random_node_path(&snapshot.children, &mut self.rng)?+ };+ let (index, parent_path) = path.split_last()?;+ let parent_id = node_at_path(&snapshot.children, parent_path).map(|node| node.id);+ let owner_entity_id = nearest_entity_id(&snapshot.children, parent_path);+ let siblings = children_at_path_mut(&mut snapshot.children, parent_path)?;+ if *index >= siblings.len() {+ return None;+ }++ let removed = siblings.remove(*index);+ let removed_element_count = removed.element_count();+ let element_id = removed.id;+ snapshot.rebuild_element_paths();+ Some(AppliedRandomizedElementTreeMutation {+ mutation: RandomizedElementTreeMutation::Removed {+ element_id,+ parent_id,+ index: *index,+ removed_element_count,+ },+ owner_entity_id,+ synchronize_entities: true,+ })+ }++ /// Recolors `count` distinct seeded random descendants in one pass and notifies each+ /// affected entity boundary once (the root, for descendants owned by no entity).+ /// Returns how many descendants changed and how many boundaries were notified.+ ///+ /// This is the "share of the scene changing per frame" knob: `count` calls to+ /// [`Self::apply_mutation`] would each walk the tree and notify separately, costing+ /// more than the frame being measured once `count` is a fair share of the tree.+ pub fn recolor_children(+ &mut self,+ count: usize,+ locality: ChangeLocality,+ cx: &mut Context<Self>,+ ) -> RecoloredChildren {+ let owners = {+ let mut snapshot = self.snapshot.borrow_mut();+ let mut paths = node_paths(&snapshot.children);+ let count = count.min(paths.len());+ if locality == ChangeLocality::Localized {+ // Order the paths so one random entity's subtree comes first: the change+ // stays inside that entity unless there is more of it than the subtree.+ let entity_paths: Vec<_> = paths+ .iter()+ .filter(|path| {+ node_at_path(&snapshot.children, path).is_some_and(|node| node.is_entity)+ })+ .cloned()+ .collect();+ if let Some(entity_path) = entity_paths+ .get(self.rng.random_range(0..entity_paths.len().max(1)))+ .cloned()+ {+ let (inside, outside): (Vec<_>, Vec<_>) = paths+ .into_iter()+ .partition(|path| path.starts_with(&entity_path));+ paths = inside;+ paths.extend(outside);+ }+ }+ // A partial Fisher–Yates shuffle over the first `count` slots: for `Spread`, a+ // uniform sample of the tree; for `Localized`, a uniform sample of the chosen+ // subtree, spilling into the rest only once the subtree is exhausted.+ let sample_from = match locality {+ ChangeLocality::Spread => paths.len(),+ ChangeLocality::Localized => count,+ };+ for index in 0..count {+ let other = self.rng.random_range(index..sample_from.max(index + 1));+ paths.swap(index, other);+ }+ let mut owners = HashSet::default();+ for path in &paths[..count] {+ owners.insert(nearest_entity_id(&snapshot.children, path));+ if let Some(node) = node_at_path_mut(&mut snapshot.children, path) {+ node.style.background = random_color(&mut self.rng);+ }+ }+ owners+ };+ let notified = owners.len();+ let recolored = count.min(self.snapshot.borrow().descendant_count());+ for owner in owners {+ self.notify_owner(owner, cx);+ }+ RecoloredChildren {+ recolored,+ notified,+ }+ }++ fn mutate_random_child(+ &mut self,+ mutation: RandomizedChildMutation,+ ) -> AppliedRandomizedElementTreeMutation {+ let mut snapshot = self.snapshot.borrow_mut();+ let Some(path) = random_node_path(&snapshot.children, &mut self.rng) else {+ drop(snapshot);+ return self.insert_child_internal();+ };+ let owner_entity_id = nearest_entity_id(&snapshot.children, &path);+ let Some(node) = node_at_path_mut(&mut snapshot.children, &path) else {+ drop(snapshot);+ return self.insert_child_internal();+ };+ let element_id = node.id;++ let mutation = match mutation {+ RandomizedChildMutation::Color => {+ node.style.background = random_color(&mut self.rng);+ RandomizedElementTreeMutation::ChildColor { element_id }+ }+ RandomizedChildMutation::Opacity => {+ node.style.opacity = random_opacity(&mut self.rng);+ RandomizedElementTreeMutation::ChildOpacity { element_id }+ }+ RandomizedChildMutation::Border => {+ node.style.randomize_border(&mut self.rng);+ RandomizedElementTreeMutation::ChildBorder { element_id }+ }+ RandomizedChildMutation::Bounds => {+ node.style.width = self.rng.random_range(36.0..220.0);+ node.style.height = self.rng.random_range(28.0..180.0);+ RandomizedElementTreeMutation::ChildBounds { element_id }+ }+ RandomizedChildMutation::Clipping => {+ node.style.clipped = !node.style.clipped;+ RandomizedElementTreeMutation::ChildClipping { element_id }+ }+ RandomizedChildMutation::Visibility => {+ node.style.visible = !node.style.visible;+ RandomizedElementTreeMutation::ChildVisibility { element_id }+ }+ };+ AppliedRandomizedElementTreeMutation {+ mutation,+ owner_entity_id,+ synchronize_entities: false,+ }+ }++ fn reorder_child(&mut self) -> AppliedRandomizedElementTreeMutation {+ let mut snapshot = self.snapshot.borrow_mut();+ let parent_paths = parent_paths_with_multiple_children(&snapshot.children);+ if parent_paths.is_empty() {+ drop(snapshot);+ return self.insert_child_internal();+ }++ let parent_path_index = self.rng.random_range(0..parent_paths.len());+ let Some(parent_path) = parent_paths.get(parent_path_index) else {+ drop(snapshot);+ return self.insert_child_internal();+ };+ let parent_id = node_at_path(&snapshot.children, parent_path).map(|node| node.id);+ let owner_entity_id = nearest_entity_id(&snapshot.children, parent_path);+ let Some(child_count) =+ children_at_path(&snapshot.children, parent_path).map(<[RandomizedElementNode]>::len)+ else {+ drop(snapshot);+ return self.insert_child_internal();+ };+ if child_count < 2 {+ drop(snapshot);+ return self.insert_child_internal();+ }++ let old_index = self.rng.random_range(0..child_count);+ let mut new_index = self.rng.random_range(0..child_count - 1);+ if new_index >= old_index {+ new_index += 1;+ }++ let Some(siblings) = children_at_path_mut(&mut snapshot.children, parent_path) else {+ drop(snapshot);+ return self.insert_child_internal();+ };+ let child = siblings.remove(old_index);+ let element_id = child.id;+ siblings.insert(new_index, child);+ snapshot.rebuild_element_paths();+ AppliedRandomizedElementTreeMutation {+ mutation: RandomizedElementTreeMutation::Reordered {+ element_id,+ parent_id,+ old_index,+ new_index,+ },+ owner_entity_id,+ synchronize_entities: false,+ }+ }++ fn synchronize_entities(&mut self, cx: &mut Context<Self>) {+ let entity_ids = {+ let snapshot = self.snapshot.borrow();+ snapshot+ .element_ids()+ .into_iter()+ .filter(|element_id| {+ snapshot+ .node(*element_id)+ .is_some_and(|node| node.is_entity)+ })+ .collect::<Vec<_>>()+ };+ let entity_id_set = entity_ids.iter().copied().collect::<HashSet<_>>();++ self.entities+ .borrow_mut()+ .retain(|element_id, _| entity_id_set.contains(element_id));++ let missing_entity_ids = {+ let entities = self.entities.borrow();+ entity_ids+ .into_iter()+ .filter(|element_id| !entities.contains_key(element_id))+ .collect::<Vec<_>>()+ };++ let weak_entities = Rc::downgrade(&self.entities);+ for element_id in missing_entity_ids {+ let entity = cx.new(|_| RandomizedElementEntity {+ element_id,+ snapshot: self.snapshot.clone(),+ entities: weak_entities.clone(),+ work_counters: self.work_counters.clone(),+ });+ self.entities.borrow_mut().insert(element_id, entity);+ }++ let entity_count = self.entities.borrow().len();+ let mut work_counters = self.work_counters.borrow_mut();+ let additional_capacity =+ entity_count.saturating_sub(work_counters.rendered_entity_ids.len());+ work_counters+ .rendered_entity_ids+ .reserve(additional_capacity);+ }++ fn notify_owner(&self, owner_entity_id: Option<u64>, cx: &mut Context<Self>) {+ let owner =+ owner_entity_id.and_then(|element_id| self.entities.borrow().get(&element_id).cloned());+ if let Some(owner) = owner {+ owner.update(cx, |_, cx| cx.notify());+ } else {+ cx.notify();+ }+ }+}++impl Render for RandomizedElementTree {+ fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {+ let snapshot = self.snapshot.borrow();+ let root = &snapshot.root;+ let entities = self.entities.borrow();+ let mut work_counters = self.work_counters.borrow_mut();+ work_counters.root_render_count += 1;++ div()+ .flex()+ .when(root.vertical, |element| element.flex_col())+ .when(root.wrap, |element| element.flex_wrap())+ .gap(px(root.gap))+ .p(px(root.padding))+ .w(px(root.width))+ .h(px(root.height))+ .bg(root.background)+ .border(px(root.border_width))+ .border_color(root.border_color)+ .rounded(px(root.corner_radius))+ .opacity(root.opacity)+ .when(root.clipped, |element| element.overflow_hidden())+ .when(!root.visible, |element| element.invisible())+ .children(+ snapshot+ .children+ .iter()+ .map(|node| render_node(node, &entities, &mut work_counters)),+ )+ }+}++type RandomizedElementEntities = HashMap<u64, Entity<RandomizedElementEntity>>;++struct RandomizedElementEntity {+ element_id: u64,+ snapshot: Rc<RefCell<RandomizedElementTreeSnapshot>>,+ entities: Weak<RefCell<RandomizedElementEntities>>,+ work_counters: Rc<RefCell<RandomizedElementTreeWorkCounters>>,+}++impl Render for RandomizedElementEntity {+ fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {+ let snapshot = self.snapshot.borrow();+ let entities = self.entities.upgrade();+ let entities = entities.as_ref().map(|entities| entities.borrow());+ let mut work_counters = self.work_counters.borrow_mut();+ work_counters.entity_render_count += 1;+ work_counters.rendered_entity_ids.push(self.element_id);++ snapshot+ .node(self.element_id)+ .map(|node| render_node_body(node, entities.as_deref(), &mut work_counters))+ .unwrap_or_else(|| div().into_any_element())+ }+}++struct AppliedRandomizedElementTreeMutation {+ mutation: RandomizedElementTreeMutation,+ owner_entity_id: Option<u64>,+ synchronize_entities: bool,+}++impl AppliedRandomizedElementTreeMutation {+ fn root(mutation: RandomizedElementTreeMutation) -> Self {+ Self {+ mutation,+ owner_entity_id: None,+ synchronize_entities: false,+ }+ }+}++#[derive(Clone, Debug, PartialEq)]+struct RandomizedElementNode {+ id: u64,+ style: RandomizedElementStyle,+ is_entity: bool,+ features: RandomizedElementFeatures,+ children: Vec<RandomizedElementNode>,+}++impl RandomizedElementNode {+ fn random(id: u64, entity_density: f64, handler_density: f64, rng: &mut StdRng) -> Self {+ Self {+ id,+ style: RandomizedElementStyle::random_child(rng),+ is_entity: rng.random_bool(entity_density),+ features: RandomizedElementFeatures::random(handler_density, rng),+ children: Vec::new(),+ }+ }++ fn element_count(&self) -> usize {+ let mut count = 0;+ let mut stack = vec![self];+ while let Some(node) = stack.pop() {+ count += 1;+ stack.extend(node.children.iter());+ }+ count+ }+}++#[derive(Clone, Debug, PartialEq)]+struct RandomizedElementFeatures {+ action_handler: bool,+ click_handler: bool,+ key_context: bool,+}++impl RandomizedElementFeatures {+ fn random(handler_density: f64, rng: &mut StdRng) -> Self {+ Self {+ action_handler: rng.random_bool(handler_density),+ click_handler: rng.random_bool(handler_density),+ key_context: rng.random_bool(handler_density),+ }+ }++ fn handler_count(&self) -> usize {+ usize::from(self.action_handler) + usize::from(self.click_handler)+ }+}++#[derive(Clone, Debug, PartialEq)]+struct RandomizedElementStyle {+ background: Hsla,+ border_color: Hsla,+ opacity: f32,+ border_width: f32,+ corner_radius: f32,+ width: f32,+ height: f32,+ padding: f32,+ gap: f32,+ clipped: bool,+ visible: bool,+ vertical: bool,+ wrap: bool,+}++impl RandomizedElementStyle {+ fn random_root(rng: &mut StdRng) -> Self {+ Self {+ background: random_color(rng),+ border_color: random_color(rng),+ opacity: random_opacity(rng),+ border_width: rng.random_range(0.0..4.0),+ corner_radius: rng.random_range(0.0..18.0),+ width: rng.random_range(520.0..980.0),+ height: rng.random_range(360.0..760.0),+ padding: rng.random_range(4.0..18.0),+ gap: rng.random_range(2.0..14.0),+ clipped: rng.random_bool(0.5),+ visible: true,+ vertical: rng.random_bool(0.5),+ wrap: rng.random_bool(0.7),+ }+ }++ fn random_child(rng: &mut StdRng) -> Self {+ Self {+ background: random_color(rng),+ border_color: random_color(rng),+ opacity: random_opacity(rng),+ border_width: rng.random_range(0.0..4.0),+ corner_radius: rng.random_range(0.0..14.0),+ width: rng.random_range(36.0..220.0),+ height: rng.random_range(28.0..180.0),+ padding: rng.random_range(1.0..10.0),+ gap: rng.random_range(1.0..8.0),+ clipped: rng.random_bool(0.3),+ visible: rng.random_bool(0.9),+ vertical: rng.random_bool(0.5),+ wrap: rng.random_bool(0.4),+ }+ }++ fn randomize_border(&mut self, rng: &mut StdRng) {+ self.border_color = random_color(rng);+ self.border_width = rng.random_range(0.0..5.0);+ self.corner_radius = rng.random_range(0.0..18.0);+ }+}++#[derive(Clone, Copy)]+enum RandomizedChildMutation {+ Color,+ Opacity,+ Border,+ Bounds,+ Clipping,+ Visibility,+}++#[cfg_attr(feature = "stacker", stacksafe::stacksafe)]+fn render_node(+ node: &RandomizedElementNode,+ entities: &RandomizedElementEntities,+ work_counters: &mut RandomizedElementTreeWorkCounters,+) -> AnyElement {+ if node.is_entity+ && let Some(entity) = entities.get(&node.id)+ {+ return entity.clone().into_any_element();+ }++ render_node_body(node, Some(entities), work_counters)+}++#[cfg_attr(feature = "stacker", stacksafe::stacksafe)]+fn render_node_body(+ node: &RandomizedElementNode,+ entities: Option<&RandomizedElementEntities>,+ work_counters: &mut RandomizedElementTreeWorkCounters,+) -> AnyElement {+ work_counters.element_render_count += 1;+ work_counters.handler_registration_count += node.features.handler_count();+ let style = &node.style;+ div()+ .id(ElementId::NamedInteger(+ "randomized-element".into(),+ node.id,+ ))+ .flex()+ .when(style.vertical, |element| element.flex_col())+ .when(style.wrap, |element| element.flex_wrap())+ .gap(px(style.gap))+ .p(px(style.padding))+ .w(px(style.width))+ .h(px(style.height))+ .bg(style.background)+ .border(px(style.border_width))+ .border_color(style.border_color)+ .rounded(px(style.corner_radius))+ .opacity(style.opacity)+ .when(style.clipped, |element| element.overflow_hidden())+ .when(!style.visible, |element| element.invisible())+ .when(node.features.action_handler, |element| {+ element.on_action(|_: &RandomizedElementTreeAction, _window, _cx| {})+ })+ .when(node.features.click_handler, |element| {+ element.on_click(|_event, _window, _cx| {})+ })+ .when(node.features.key_context, |element| {+ element.key_context("RandomizedElementTree")+ })+ .children(node.children.iter().map(|child| {+ if let Some(entities) = entities {+ render_node(child, entities, work_counters)+ } else {+ render_node_body(child, None, work_counters)+ }+ }))+ .into_any_element()+}++fn insert_node(+ root_children: &mut Vec<RandomizedElementNode>,+ parent_path: &mut Vec<usize>,+ node: RandomizedElementNode,+) -> (usize, Vec<usize>) {+ let Some(children) = children_at_path_mut(root_children, parent_path) else {+ parent_path.clear();+ let index = root_children.len();+ root_children.push(node);+ return (index, vec![index]);+ };++ let index = children.len();+ children.push(node);+ let mut inserted_path = parent_path.clone();+ inserted_path.push(index);+ (index, inserted_path)+}++fn random_parent_path(root_children: &[RandomizedElementNode], rng: &mut StdRng) -> Vec<usize> {+ let paths = node_paths(root_children);+ let index = rng.random_range(0..=paths.len());+ paths.get(index).cloned().unwrap_or_default()+}++fn random_node_path(+ root_children: &[RandomizedElementNode],+ rng: &mut StdRng,+) -> Option<Vec<usize>> {+ let paths = node_paths(root_children);+ if paths.is_empty() {+ None+ } else {+ paths.get(rng.random_range(0..paths.len())).cloned()+ }+}++fn random_leaf_path(+ root_children: &[RandomizedElementNode],+ rng: &mut StdRng,+) -> Option<Vec<usize>> {+ let leaves = node_paths(root_children)+ .into_iter()+ .filter(|path| {+ node_at_path(root_children, path).is_some_and(|node| node.children.is_empty())+ })+ .collect::<Vec<_>>();+ if leaves.is_empty() {+ None+ } else {+ leaves.get(rng.random_range(0..leaves.len())).cloned()+ }+}++fn parent_paths_with_multiple_children(root_children: &[RandomizedElementNode]) -> Vec<Vec<usize>> {+ let mut parent_paths = Vec::new();+ if root_children.len() >= 2 {+ parent_paths.push(Vec::new());+ }+ parent_paths.extend(node_paths(root_children).into_iter().filter(|path| {+ node_at_path(root_children, path).is_some_and(|node| node.children.len() >= 2)+ }));+ parent_paths+}++fn node_paths(root_children: &[RandomizedElementNode]) -> Vec<Vec<usize>> {+ let mut paths = Vec::new();+ let mut stack = root_children+ .iter()+ .enumerate()+ .rev()+ .map(|(index, node)| (vec![index], node))+ .collect::<Vec<_>>();++ while let Some((path, node)) = stack.pop() {+ for (index, child) in node.children.iter().enumerate().rev() {+ let mut child_path = path.clone();+ child_path.push(index);+ stack.push((child_path, child));+ }+ paths.push(path);+ }+ paths+}++fn node_at_path<'a>(+ root_children: &'a [RandomizedElementNode],+ path: &[usize],+) -> Option<&'a RandomizedElementNode> {+ let (index, parent_path) = path.split_last()?;+ children_at_path(root_children, parent_path)?.get(*index)+}++fn node_at_path_mut<'a>(+ root_children: &'a mut Vec<RandomizedElementNode>,+ path: &[usize],+) -> Option<&'a mut RandomizedElementNode> {+ let (index, parent_path) = path.split_last()?;+ children_at_path_mut(root_children, parent_path)?.get_mut(*index)+}++fn children_at_path<'a>(+ mut children: &'a [RandomizedElementNode],+ path: &[usize],+) -> Option<&'a [RandomizedElementNode]> {+ for index in path {+ children = &children.get(*index)?.children;+ }+ Some(children)+}++fn children_at_path_mut<'a>(+ mut children: &'a mut Vec<RandomizedElementNode>,+ path: &[usize],+) -> Option<&'a mut Vec<RandomizedElementNode>> {+ for index in path {+ children = &mut children.get_mut(*index)?.children;+ }+ Some(children)+}++fn nearest_entity_id(root_children: &[RandomizedElementNode], path: &[usize]) -> Option<u64> {+ let mut children = root_children;+ let mut nearest_entity_id = None;+ for index in path {+ let node = children.get(*index)?;+ if node.is_entity {+ nearest_entity_id = Some(node.id);+ }+ children = &node.children;+ }+ nearest_entity_id+}++fn normalized_density(density: f64) -> f64 {+ if density.is_nan() {+ 0.0+ } else {+ density.clamp(0.0, 1.0)+ }+}++fn random_color(rng: &mut StdRng) -> Hsla {+ Hsla {+ h: rng.random_range(0.0..1.0),+ s: rng.random_range(0.25..0.9),+ l: rng.random_range(0.18..0.82),+ a: 1.0,+ }+}++fn random_opacity(rng: &mut StdRng) -> f32 {+ rng.random_range(0.25..1.0)+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{+ AnyWindowHandle, Background, BorderStyle, Bounds, ContentMask, Corners, Edges, Pixels,+ ScaledPixels, Size, TestAppContext, size,+ };+ use proptest::prelude::*;+ use std::collections::BTreeSet;++ const ELEMENT_COUNT: usize = 64;+ const MUTATION_COUNT: usize = 32;+ const WINDOW_SIZE: Size<Pixels> = size(px(1200.0), px(900.0));++ #[derive(Debug, PartialEq)]+ struct SceneSnapshot {+ quads: Vec<QuadSnapshot>,+ element_ids: BTreeSet<u64>,+ }++ impl SceneSnapshot {+ fn from_window(window: &Window) -> Self {+ Self {+ quads: window+ .rendered_frame+ .scene+ .quads+ .iter()+ .map(QuadSnapshot::from)+ .collect(),+ element_ids: window+ .rendered_frame+ .element_states+ .keys()+ .filter_map(|(global_id, _)| {+ let ElementId::NamedInteger(name, element_id) = global_id.0.last()? else {+ return None;+ };+ (name.as_ref() == "randomized-element").then_some(*element_id)+ })+ .collect(),+ }+ }+ }++ #[derive(Debug, PartialEq)]+ struct QuadSnapshot {+ order: u32,+ border_style: BorderStyle,+ bounds: Bounds<ScaledPixels>,+ content_mask: ContentMask<ScaledPixels>,+ background: Background,+ border_color: Hsla,+ corner_radii: Corners<ScaledPixels>,+ border_widths: Edges<ScaledPixels>,+ }++ impl From<&crate::Quad> for QuadSnapshot {+ fn from(quad: &crate::Quad) -> Self {+ Self {+ order: quad.order,+ border_style: quad.border_style,+ bounds: quad.bounds,+ content_mask: quad.content_mask,+ background: quad.background,+ border_color: quad.border_color,+ corner_radii: quad.corner_radii,+ border_widths: quad.border_widths,+ }+ }+ }++ #[gpui::test]+ fn localized_recoloring_notifies_one_entity_until_its_subtree_is_exhausted(+ cx: &mut TestAppContext,+ ) {+ // Wide: every element is a root child, so every entity's subtree is exactly one+ // element and localized change of k elements must touch k boundaries only when it+ // spills; a mixed tree has larger subtrees to stay inside.+ let config = RandomizedElementTreeConfig::new(11, 200)+ .with_topology(RandomizedElementTreeTopology::Mixed)+ .with_entity_density(0.1);+ let window = cx.open_window(WINDOW_SIZE, move |_, cx| {+ RandomizedElementTree::new_with_config(config, cx)+ });+ let root = window.root(cx).expect("window has a root");+ let (localized, spread) = root.update(cx, |tree, cx| {+ let localized = tree.recolor_children(20, ChangeLocality::Localized, cx);+ let spread = tree.recolor_children(20, ChangeLocality::Spread, cx);+ (localized, spread)+ });+ assert_eq!(localized.recolored, 20);+ assert_eq!(spread.recolored, 20);+ assert!(+ localized.notified <= spread.notified,+ "localized change touches no more boundaries than spread change: {localized:?} vs {spread:?}"+ );+ assert!(localized.notified >= 1);+ }++ #[test]+ fn bounds_sample_within_range_and_reproduce_from_the_seed() {+ let bounds = RandomizedElementTreeBounds::new(32..=256)+ .with_topologies([+ RandomizedElementTreeTopology::Deep,+ RandomizedElementTreeTopology::Narrow,+ ])+ .with_entity_density(0.1..=0.5)+ .with_handler_density(0.25..=0.25);+ let mut topologies = BTreeSet::new();+ let sample = |seed| bounds.sample(&mut StdRng::seed_from_u64(seed));+ for seed in 0..64 {+ let config = sample(seed);+ assert!((32..=256).contains(&config.element_count()), "{config:?}");+ assert!(config.topology().is_tall(), "{config:?}");+ assert!((0.1..=0.5).contains(&config.entity_density()), "{config:?}");+ assert_eq!(config.handler_density(), 0.25);+ assert_eq!(sample(seed), config, "a seed draws the same config twice");+ topologies.insert(format!("{:?}", config.topology()));+ }+ assert_eq!(topologies.len(), 2, "64 seeds pick both allowed topologies");+ assert_ne!(sample(0), sample(1));+ }++ #[test]+ fn generated_topologies_include_wide_deep_narrow_and_empty_trees() {+ let wide = RandomizedElementTree::new(0, ELEMENT_COUNT).snapshot();+ assert_eq!(wide.children.len(), ELEMENT_COUNT);+ assert_eq!(wide.max_depth(), 1);++ let deep = RandomizedElementTree::new(1, ELEMENT_COUNT).snapshot();+ assert_eq!(deep.children.len(), 1);+ assert_eq!(deep.max_depth(), ELEMENT_COUNT);++ let narrow = RandomizedElementTree::new(3, ELEMENT_COUNT).snapshot();+ assert_eq!(narrow.descendant_count(), ELEMENT_COUNT);+ assert!(narrow.max_depth() > 1);++ let empty = RandomizedElementTree::new(2, 0).snapshot();+ assert_eq!(empty.descendant_count(), 0);+ assert_eq!(empty.max_depth(), 0);+ }++ #[test]+ fn randomized_element_ids_are_unique_and_stable() {+ let mut tree = RandomizedElementTree::new(0, ELEMENT_COUNT);+ let initial_ids = tree.snapshot().element_ids();+ assert_eq!(initial_ids.len(), ELEMENT_COUNT);+ assert_eq!(+ initial_ids.iter().copied().collect::<BTreeSet<_>>().len(),+ ELEMENT_COUNT+ );++ for mutation in [+ RandomizedChildMutation::Color,+ RandomizedChildMutation::Opacity,+ RandomizedChildMutation::Border,+ RandomizedChildMutation::Bounds,+ RandomizedChildMutation::Clipping,+ RandomizedChildMutation::Visibility,+ ] {+ tree.mutate_random_child(mutation);+ assert_eq!(tree.snapshot().element_ids(), initial_ids);+ }++ tree.reorder_child();+ let reordered_ids = tree.snapshot().element_ids();+ assert_eq!(+ reordered_ids.iter().copied().collect::<BTreeSet<_>>(),+ initial_ids.iter().copied().collect()+ );++ tree.insert_child();+ let inserted_ids = tree.snapshot().element_ids();+ let inserted_id_set = inserted_ids.iter().copied().collect::<BTreeSet<_>>();+ assert_eq!(inserted_ids.len(), ELEMENT_COUNT + 1);+ assert!(initial_ids.iter().all(|id| inserted_id_set.contains(id)));++ tree.remove_child();+ let remaining_ids = tree.snapshot().element_ids();+ assert_eq!(+ remaining_ids.iter().copied().collect::<BTreeSet<_>>().len(),+ remaining_ids.len()+ );+ assert!(remaining_ids.iter().all(|id| inserted_id_set.contains(id)));+ }++ #[gpui::test]+ fn randomized_entities_and_work_counters_support_incremental_benchmarks(+ cx: &mut TestAppContext,+ ) {+ let config = RandomizedElementTreeConfig::new(0, 16)+ .with_topology(RandomizedElementTreeTopology::Wide)+ .with_entity_density(1.0)+ .with_handler_density(1.0);+ let window = cx.open_window(WINDOW_SIZE, move |_, cx| {+ RandomizedElementTree::new_with_config(config, cx)+ });+ let root = window+ .root(cx)+ .expect("randomized tree window should have a root");+ let window = AnyWindowHandle::from(window);++ draw_window(window, cx);+ let initial_counters = root.read_with(cx, |tree, _| tree.work_counters());+ assert!(initial_counters.root_render_count() >= 1);+ assert!(initial_counters.entity_render_count() >= 16);+ assert!(initial_counters.element_render_count() >= 16);+ assert!(initial_counters.handler_registration_count() >= 32);++ let mutation = root.update(cx, |tree, cx| {+ tree.reset_work_counters();+ tree.apply_mutation(RandomizedElementTreeMutationKind::ChildColor, cx)+ });+ let RandomizedElementTreeMutation::ChildColor { element_id } = mutation else {+ panic!("a nonempty tree should apply the requested child-color mutation");+ };+ draw_window(window, cx);++ let incremental_counters = root.read_with(cx, |tree, _| tree.work_counters());+ assert!(incremental_counters.root_render_count() >= 1);+ assert!(incremental_counters.entity_render_count() >= 1);+ assert!(incremental_counters.element_render_count() >= 1);+ assert!(incremental_counters.handler_registration_count() >= 2);+ assert!(+ incremental_counters+ .rendered_entity_ids()+ .contains(&element_id)+ );++ cx.update_window(window, |_, window, _| window.remove_window())+ .expect("randomized tree window should remain open until the case completes");+ }++ #[gpui::property_test(config = ProptestConfig {+ cases: 4,+ ..Default::default()+ })]+ fn randomized_element_tree_matches_full_render(cx: &mut TestAppContext, seed: u64) {+ for topology in 0..4 {+ let topology_seed = (seed & !3) | topology;+ let config = RandomizedElementTreeConfig::new(topology_seed, ELEMENT_COUNT)+ .with_entity_density(0.2)+ .with_handler_density(0.2);+ run_render_equivalence_case(cx, config);+ }+ run_render_equivalence_case(+ cx,+ RandomizedElementTreeConfig::new(seed, 0)+ .with_entity_density(0.2)+ .with_handler_density(0.2),+ );+ }++ fn run_render_equivalence_case(cx: &mut TestAppContext, config: RandomizedElementTreeConfig) {+ let persistent_window = cx.open_window(WINDOW_SIZE, move |_, cx| {+ RandomizedElementTree::new_with_config(config, cx)+ });+ let persistent_root = persistent_window+ .root(cx)+ .expect("randomized tree window should have a root");+ let persistent_window = AnyWindowHandle::from(persistent_window);++ assert_matches_reference(&persistent_root, persistent_window, cx, config, None);++ for mutation_index in 0..MUTATION_COUNT {+ let mutation = persistent_root.update(cx, |tree, cx| match mutation_index {+ 0 => tree.apply_mutation(RandomizedElementTreeMutationKind::Insert, cx),+ 1 => tree.apply_mutation(RandomizedElementTreeMutationKind::Remove, cx),+ _ => tree.apply_random_mutation(cx),+ });++ assert_matches_reference(+ &persistent_root,+ persistent_window,+ cx,+ config,+ Some((mutation_index, mutation)),+ );+ }++ cx.update_window(persistent_window, |_, window, _| window.remove_window())+ .expect("persistent window should remain open until the case completes");+ }++ fn assert_matches_reference(+ persistent_root: &crate::Entity<RandomizedElementTree>,+ persistent_window: AnyWindowHandle,+ cx: &mut TestAppContext,+ config: RandomizedElementTreeConfig,+ mutation: Option<(usize, RandomizedElementTreeMutation)>,+ ) {+ let snapshot = persistent_root.read_with(cx, |tree, _| tree.snapshot());+ let expected_element_ids = snapshot.element_ids().into_iter().collect::<BTreeSet<_>>();+ let persistent_scene = draw_window(persistent_window, cx);+ assert_eq!(+ persistent_scene.element_ids, expected_element_ids,+ "persistent frame element IDs diverged from the model for config {config:?}, after \+ mutation {mutation:?}"+ );+ let reference_scene = draw_fresh_reference(snapshot, cx);++ assert_eq!(+ persistent_scene, reference_scene,+ "rendered scene diverged for config {config:?}, after mutation {mutation:?}"+ );+ }++ fn draw_fresh_reference(+ snapshot: RandomizedElementTreeSnapshot,+ cx: &mut TestAppContext,+ ) -> SceneSnapshot {+ let reference_window = cx.open_window(WINDOW_SIZE, move |_, cx| {+ RandomizedElementTree::from_snapshot_with_entities(snapshot, cx)+ });+ let reference_window = AnyWindowHandle::from(reference_window);+ let scene = draw_window(reference_window, cx);+ cx.update_window(reference_window, |_, window, _| window.remove_window())+ .expect("reference window should remain open until its frame is captured");+ scene+ }++ fn draw_window(window: AnyWindowHandle, cx: &mut TestAppContext) -> SceneSnapshot {+ cx.update_window(window, |_, window, cx| {+ let arena_clear_needed = window.draw(cx);+ let scene = SceneSnapshot::from_window(window);+ arena_clear_needed.clear(cx);+ scene+ })+ .expect("randomized element tree window should remain open while drawing")+ }+}diff --git a/crates/gpui_linux/src/linux/dispatcher.rs b/crates/gpui_linux/src/linux/dispatcher.rsindex fd9caf89fc1..3fb3aec6ef2 100644--- a/crates/gpui_linux/src/linux/dispatcher.rs+++ b/crates/gpui_linux/src/linux/dispatcher.rs@@ -60,8 +60,14 @@ impl LinuxDispatcher {let handle = event_loop.handle();let timer_handle = event_loop.handle();+ let signal = event_loop.get_signal();handle.insert_source(timer_channel, move |e, _, _| {+ // The dispatcher owning the sender is gone; timers already+ // scheduled would run tasks nothing can observe.+ if let channel::Event::Closed = e {+ signal.stop();+ }if let channel::Event::Msg(timer) = e {let mut runnable = Some(timer.runnable);timer_handlediff --git a/crates/gpui_macros/src/bench.rs b/crates/gpui_macros/src/bench.rsindex 7bf5f429df0..d14f1ade87a 100644--- a/crates/gpui_macros/src/bench.rs+++ b/crates/gpui_macros/src/bench.rs@@ -179,7 +179,7 @@ pub fn bench(args: TokenStream, function: TokenStream) -> TokenStream {Some(Box::new(|| {gpui_platform::current_headless_renderer()})),- gpui_platform::current_platform(true).text_system(),+ gpui_platform::bench_text_system(),),Some(stringify!(#outer_fn_name)),bencher,diff --git a/crates/gpui_platform/Cargo.toml b/crates/gpui_platform/Cargo.tomlindex ce2e057c4ae..daf154f0f1d 100644--- a/crates/gpui_platform/Cargo.toml+++ b/crates/gpui_platform/Cargo.toml@@ -28,11 +28,17 @@ name = "inspector_render"harness = falserequired-features = ["bench-support"]+[[bench]]+name = "randomized_element_tree"+harness = false+required-features = ["bench-support"]+[dependencies]anyhow.workspace = truegpui.workspace = true[dev-dependencies]+bench_metrics.workspace = truecriterion.workspace = true[target.'cfg(target_os = "macos")'.dependencies]diff --git a/crates/gpui_platform/benches/randomized_element_tree.rs b/crates/gpui_platform/benches/randomized_element_tree.rsnew file mode 100644index 00000000000..01dbfd4b2c8--- /dev/null+++ b/crates/gpui_platform/benches/randomized_element_tree.rs@@ -0,0 +1,601 @@+//! Frame cost of a seeded element tree under one class of change per frame.+//!+//! Each benchmark builds a [`RandomizedElementTree`] before timing, then measures frames in+//! which exactly one thing happens: nothing (a notified root that re-renders an unchanged+//! tree), a leaf's style, a leaf's bounds, the root's layout, one structural change, or a+//! sibling reorder. The same fixture runs on any GPUI revision, so it doubles as the+//! before/after harness for changes to rendering.+//!+//! Trees come from *families*: bounds on topology, element count and entity density that+//! a seeded RNG is drawn against (see [`RandomizedElementTreeBounds`]). Each family is an+//! input and `#[gpui::bench]` runs it once per seed, so an id like `tree/wide/seed-3`+//! names one tree whose whole shape follows from the seed, and a run over a family's+//! seeds is a sample of that family. `SEED` and `ITERATIONS` choose the seeds as for+//! `#[gpui::test]`. Filter by family to ask a narrower question, e.g.+//! `--bench randomized_element_tree -- 'tree/tall/'`. Each tree is described once, with+//! the heap its window holds after the first frame, before it is measured.+//!+//! The tree's own work counters are asserted after each measured loop: a faster frame that+//! stopped rendering the changed element is not an improvement.++use std::{+ cell::RefCell,+ fmt,+ time::{Duration, Instant},+};++use gpui::{+ BenchAppContext, Context, Window,+ private::rand::{Rng as _, SeedableRng as _, rngs::StdRng},+ randomized_element_tree::{+ ChangeLocality, RandomizedElementTree, RandomizedElementTreeBounds,+ RandomizedElementTreeConfig, RandomizedElementTreeMutation,+ RandomizedElementTreeMutationKind, RandomizedElementTreeTopology, RecoloredChildren,+ },+};++/// A named family of trees: the bounds one seed's tree is drawn against.+#[derive(Clone)]+struct TreeFamily {+ name: &'static str,+ bounds: RandomizedElementTreeBounds,+}++impl TreeFamily {+ /// Draws this family's tree for the seed `rng` started from. Every family consumes+ /// its RNG the same way, so drawing from `rng` directly would give each family the+ /// same relative draw at a given seed (overlapping families the same tree); mixing in+ /// the family's name gives each family its own stream for the same seed.+ fn sample(&self, rng: &mut StdRng) -> TreeInput {+ // FNV-1a, which unlike `DefaultHasher` is fixed across Rust releases, so a seed+ // names the same tree on every toolchain.+ let name_hash = self+ .name+ .bytes()+ .fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {+ (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3)+ });+ let mut family_rng = StdRng::seed_from_u64(rng.random::<u64>() ^ name_hash);+ TreeInput {+ family: self.name,+ config: self.bounds.sample(&mut family_rng),+ }+ }+}++impl fmt::Display for TreeFamily {+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {+ formatter.write_str(self.name)+ }+}++/// One tree drawn from a family. Its `Display` describes the draw, which the benchmark+/// id (family and seed) does not.+struct TreeInput {+ family: &'static str,+ config: RandomizedElementTreeConfig,+}++impl fmt::Display for TreeInput {+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {+ let topology = match self.config.topology() {+ RandomizedElementTreeTopology::Wide => "wide",+ RandomizedElementTreeTopology::Deep => "deep",+ RandomizedElementTreeTopology::Mixed => "mixed",+ RandomizedElementTreeTopology::Narrow => "narrow",+ };+ write!(+ formatter,+ "{} tree: {topology}, {} elements, {}% entities, {}% handlers",+ self.family,+ self.config.element_count(),+ (self.config.entity_density() * 100.0).round() as usize,+ (self.config.handler_density() * 100.0).round() as usize+ )+ }+}++/// Tall trees recurse once per element through layout and paint; without gpui's `stacker`+/// feature a chain of 384 draws and one of 512 overflows a 2 MB stack, so tall families+/// stop well short of that. Real UI is not hundreds of levels deep either.+const TALL_MAX_ELEMENTS: usize = 256;++/// Entities are how GPUI structures a UI, so every family has at least this share of its+/// elements backed by one; a tree with none is a single view, which no renderer can+/// partially reuse and which real UI never is.+const MIN_ENTITY_DENSITY: f64 = 0.05;++/// The families every benchmark samples. Each is a question: what does this class of+/// change cost on a tree like *this*?+fn families() -> Vec<TreeFamily> {+ let handlers = 0.1..=0.4;+ vec![+ TreeFamily {+ name: "any",+ bounds: RandomizedElementTreeBounds::new(32..=TALL_MAX_ELEMENTS)+ .with_entity_density(MIN_ENTITY_DENSITY..=0.5)+ .with_handler_density(handlers.clone()),+ },+ TreeFamily {+ name: "wide",+ bounds: RandomizedElementTreeBounds::new(128..=2048)+ .with_topologies([RandomizedElementTreeTopology::Wide])+ .with_entity_density(MIN_ENTITY_DENSITY..=0.25)+ .with_handler_density(handlers.clone()),+ },+ TreeFamily {+ name: "tall",+ bounds: RandomizedElementTreeBounds::new(32..=TALL_MAX_ELEMENTS)+ .with_topologies(+ RandomizedElementTreeTopology::ALL+ .into_iter()+ .filter(|topology| topology.is_tall()),+ )+ .with_entity_density(MIN_ENTITY_DENSITY..=0.25)+ .with_handler_density(handlers.clone()),+ },+ TreeFamily {+ name: "mixed",+ bounds: RandomizedElementTreeBounds::new(128..=1024)+ .with_topologies([RandomizedElementTreeTopology::Mixed])+ .with_entity_density(MIN_ENTITY_DENSITY..=0.25)+ .with_handler_density(handlers.clone()),+ },+ TreeFamily {+ name: "dense-entities",+ bounds: RandomizedElementTreeBounds::new(128..=1024)+ .with_topologies([+ RandomizedElementTreeTopology::Wide,+ RandomizedElementTreeTopology::Mixed,+ ])+ .with_entity_density(0.75..=1.0)+ .with_handler_density(handlers),+ },+ ]+}++/// Builds the tree, draws it once so layout and the scene are warm, and measures+/// `mutate` once per frame. Returns how many frames were measured, after checking that+/// the frames did render something: a frame that skipped the changed element would be+/// fast and wrong.+fn measure(+ group: &str,+ input: &TreeInput,+ cx: &mut BenchAppContext,+ mut mutate: impl FnMut(&mut RandomizedElementTree, &mut Context<RandomizedElementTree>),+) -> usize {+ measure_in_window(group, input, cx, |tree, _, cx| mutate(tree, cx)).frames+}++/// What a measured loop did: frames measured, and the entity renders the frames showed.+struct Measured {+ frames: usize,+ entity_renders: usize,+}++fn measure_in_window(+ group: &str,+ input: &TreeInput,+ cx: &mut BenchAppContext,+ mut mutate: impl FnMut(&mut RandomizedElementTree, &mut Window, &mut Context<RandomizedElementTree>),+) -> Measured {+ let config = input.config;+ let before_window = bench_metrics::allocation_stats();+ let mut window = cx.add_empty_window();+ let tree = window.update(|window, cx| {+ window.replace_root(cx, |_, cx| {+ RandomizedElementTree::new_with_config(config, cx)+ })+ });+ cx.run_until_idle();+ describe_once(input, before_window, bench_metrics::allocation_stats());++ let snapshot = tree.read_with(cx, |tree, _| tree.snapshot());+ assert_eq!(snapshot.descendant_count(), config.element_count());+ let initial = tree.read_with(cx, |tree, _| tree.work_counters());+ assert!(+ initial.root_render_count() >= 1,+ "the tree must have drawn before timing"+ );+ assert!(+ initial.element_render_count() >= config.element_count(),+ "every generated element renders in the first frame"+ );++ // Each iteration reads what the previous frame rendered before resetting for its own+ // mutation. Frame pacing may coalesce two mutations into one drawn frame, the more+ // often the faster the frames, so some observation windows legitimately see no draw;+ // a renderer that skipped the changed element would see none in any of them.+ let mut frames = 0;+ let mut frames_that_rendered = 0;+ let mut entity_renders = 0;+ let mut summary = SeedTotals::default();+ let mut previous_iteration: Option<(Instant, Option<bench_metrics::AllocationStats>)> = None;+ cx.bench_renderer(tree, |tree, window, cx| {+ // From one iteration's start to the next is one whole iteration, the frame+ // included, as Criterion times it; the reads add well under a microsecond.+ let now = Instant::now();+ let stats = bench_metrics::allocation_stats();+ if let Some((started, started_stats)) = previous_iteration {+ summary.elapsed += now - started;+ summary.iterations += 1;+ if let Some((start, end)) = started_stats.zip(stats) {+ summary.allocations += end.allocations - start.allocations;+ }+ }+ previous_iteration = Some((now, stats));++ let previous = tree.work_counters();+ entity_renders += previous.entity_render_count();+ if previous.root_render_count() + previous.entity_render_count() >= 1+ && previous.element_render_count() >= 1+ {+ frames_that_rendered += 1;+ }+ tree.reset_work_counters();+ mutate(tree, window, cx);+ frames += 1;+ });+ assert!(+ frames_that_rendered * 2 >= frames,+ "the notified root or entity re-rendered in {frames_that_rendered} of {frames} frames"+ );+ record_for_summary(group, input, summary);+ Measured {+ frames,+ entity_renders,+ }+}++thread_local! {+ static LAST_DESCRIBED: RefCell<Option<RandomizedElementTreeConfig>> =+ const { RefCell::new(None) };+}++/// Prints the tree and the heap its window holds after the first frame, once per run of+/// consecutive routine calls on the same tree: Criterion calls a benchmark's routine for+/// warm-up and every sample, and each call builds the same tree.+fn describe_once(+ input: &TreeInput,+ before_window: Option<bench_metrics::AllocationStats>,+ after_first_frame: Option<bench_metrics::AllocationStats>,+) {+ let is_new =+ LAST_DESCRIBED.with(|last| last.borrow_mut().replace(input.config)) != Some(input.config);+ if !is_new {+ return;+ }+ match before_window.zip(after_first_frame) {+ Some((before, after)) => eprintln!(+ "{input}; window holds {:.1} KB after the first frame",+ (after.live_bytes() - before.live_bytes()) as f64 / 1024.+ ),+ None => eprintln!("{input}"),+ }+}++/// One seed's measured iterations, summed over every routine call Criterion makes for it.+#[derive(Clone, Copy, Default)]+struct SeedTotals {+ elapsed: Duration,+ iterations: u64,+ allocations: u64,+}++/// Totals per group and family, each holding one entry per tree (seed), in run order.+type SummaryTotals = Vec<(+ String,+ &'static str,+ Vec<(RandomizedElementTreeConfig, SeedTotals)>,+)>;++thread_local! {+ static SUMMARY: RefCell<SummaryTotals> = const { RefCell::new(Vec::new()) };+}++fn record_for_summary(group: &str, input: &TreeInput, totals: SeedTotals) {+ SUMMARY.with(|summary| {+ let mut summary = summary.borrow_mut();+ let index = match summary.iter().position(|(existing_group, family, _)| {+ existing_group == group && *family == input.family+ }) {+ Some(index) => index,+ None => {+ summary.push((group.to_owned(), input.family, Vec::new()));+ summary.len() - 1+ }+ };+ let seeds = &mut summary[index].2;+ match seeds.iter_mut().find(|(config, _)| *config == input.config) {+ Some((_, seed)) => {+ seed.elapsed += totals.elapsed;+ seed.iterations += totals.iterations;+ seed.allocations += totals.allocations;+ }+ None => seeds.push((input.config, totals)),+ }+ });+}++fn geometric_mean(values: impl IntoIterator<Item = f64>) -> f64 {+ let (log_sum, count) = values+ .into_iter()+ .fold((0.0, 0_u32), |(sum, count), value| {+ (sum + value.max(f64::MIN_POSITIVE).ln(), count + 1)+ });+ (log_sum / f64::from(count.max(1))).exp()+}++/// Prints one line per group and family: the geometric mean across its seeds of the time+/// and allocations per iteration, so a family reads as one number while each seed stays+/// its own reproducible benchmark. Registered as the last target so it runs after every+/// group; prints nothing when a filter matched no randomized tree benchmark.+fn family_summary(_: &mut criterion::Criterion<gpui::BenchMeasurement>) {+ let summary = SUMMARY.with(|summary| summary.take());+ if summary.is_empty() {+ return;+ }+ eprintln!(+ "\nRandomizedTree family summary (geometric mean across seeds; includes Criterion warm-up)"+ );+ eprintln!(+ " {:<34} {:<15} {:>5} {:>14} {:>14}",+ "group", "family", "seeds", "time/iter", "allocs/iter"+ );+ for (group, family, seeds) in summary {+ let per_iteration = |value: fn(&SeedTotals) -> f64| {+ geometric_mean(+ seeds+ .iter()+ .filter(|(_, totals)| totals.iterations > 0)+ .map(|(_, totals)| value(totals) / totals.iterations as f64),+ )+ };+ let time = per_iteration(|totals| totals.elapsed.as_secs_f64());+ let allocations = per_iteration(|totals| totals.allocations as f64);+ eprintln!(+ " {group:<34} {family:<15} {:>5} {:>11.1} µs {allocations:>14.0}",+ seeds.len(),+ time * 1e6+ );+ }+}++/// A frame that rebuilds everything: `Window::refresh`, the fallback GPUI uses whenever+/// it cannot tell what changed (window activation, theme or settings changes via+/// `refresh_windows`, an inspector toggle). Nothing in the tree changes, so this is the+/// pure cost of the known-good path, and the renderer's worst case: every view and element+/// rendered again, none reused. Its difference between two GPUI revisions is the price of+/// a full refresh on the newer one.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/full refresh", fps = 120)]+fn full_refresh(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let measured = measure_in_window("full refresh", input, cx, |_, window, _| window.refresh());+ assert!(measured.frames > 0);+ // A full refresh renders every entity; a renderer that replayed them would render none.+ if input.config.entity_density() >= 0.25 {+ assert!(+ measured.entity_renders > 0,+ "a full refresh re-renders entities"+ );+ }+}++/// A frame in which the root is notified but nothing in the tree changed. On a renderer+/// that rebuilds every frame this is the whole tree's cost; on one that retains output+/// it is the floor for a frame that has to do nothing.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/unchanged", fps = 120)]+fn unchanged(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let frames = measure("unchanged", input, cx, |_, cx| cx.notify());+ assert!(frames > 0);+}++/// One descendant's background color changes each frame: the smallest possible change,+/// confined to one element and, when it has one, its owning entity.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/leaf style", fps = 120)]+fn leaf_style(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let mut recolored = 0usize;+ let frames = measure("leaf style", input, cx, |tree, cx| {+ if let RandomizedElementTreeMutation::ChildColor { .. } =+ tree.apply_mutation(RandomizedElementTreeMutationKind::ChildColor, cx)+ {+ recolored += 1;+ }+ });+ assert!(frames > 0);+ assert_eq!(+ recolored, frames,+ "a nonempty tree recolors a child every frame"+ );+}++/// A descendant's width and height change each frame, so its siblings and ancestors+/// re-lay out even where their own content did not change.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/leaf bounds", fps = 120)]+fn leaf_bounds(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let frames = measure("leaf bounds", input, cx, |tree, cx| {+ tree.apply_mutation(RandomizedElementTreeMutationKind::ChildBounds, cx);+ });+ assert!(frames > 0);+}++/// The root's width and height change each frame: every element's layout is stale.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/root layout", fps = 120)]+fn root_layout(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let frames = measure("root layout", input, cx, |tree, cx| {+ tree.apply_mutation(RandomizedElementTreeMutationKind::RootBounds, cx);+ });+ assert!(frames > 0);+}++/// One element is inserted or removed per frame, alternating so the tree keeps its size+/// to within one element over the whole loop. Removal takes a leaf, never a subtree.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/insert-remove", fps = 120)]+fn insert_remove(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let mut insert = true;+ let mut inserted = 0usize;+ let mut removed = 0usize;+ let frames = measure("insert-remove", input, cx, |tree, cx| {+ let kind = if insert {+ RandomizedElementTreeMutationKind::Insert+ } else {+ RandomizedElementTreeMutationKind::RemoveLeaf+ };+ match tree.apply_mutation(kind, cx) {+ RandomizedElementTreeMutation::Inserted { .. } => inserted += 1,+ RandomizedElementTreeMutation::Removed {+ removed_element_count,+ ..+ } => {+ assert_eq!(removed_element_count, 1, "leaf removal removes one element");+ removed += 1;+ }+ other => panic!("structural mutation produced {other:?}"),+ }+ insert = !insert;+ });+ assert!(frames > 0);+ assert_eq!(inserted + removed, frames);+ assert!(inserted.abs_diff(removed) <= 1);+}++/// A descendant moves among its siblings each frame; the set of elements is unchanged+/// but their order, and therefore every sibling's position, is not.+#[gpui::bench(inputs = families(), input_name = "tree", iterations = 2, group = "RandomizedTree/reorder", fps = 120)]+fn reorder(family: &TreeFamily, mut rng: StdRng, cx: &mut BenchAppContext) {+ let input = &family.sample(&mut rng);+ let mut reordered = 0usize;+ let frames = measure("reorder", input, cx, |tree, cx| {+ if let RandomizedElementTreeMutation::Reordered { .. } =+ tree.apply_mutation(RandomizedElementTreeMutationKind::Reorder, cx)+ {+ reordered += 1;+ }+ });+ assert!(frames > 0);+ // A `Deep` tree has no parent with two children until an insert creates one, so the+ // harness inserts instead; every other shape reorders on every frame.+ if input.config.topology() != RandomizedElementTreeTopology::Deep {+ assert_eq!(reordered, frames);+ }+}++/// A tree family, the share of its elements that change each frame, and where those+/// changes sit.+#[derive(Clone)]+struct ChangingShareInput {+ family: TreeFamily,+ share_percent: usize,+ locality: ChangeLocality,+}++impl ChangingShareInput {+ fn locality_name(&self) -> &'static str {+ match self.locality {+ ChangeLocality::Localized => "local",+ ChangeLocality::Spread => "spread",+ }+ }+}++impl fmt::Display for ChangingShareInput {+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {+ let locality = self.locality_name();+ write!(+ formatter,+ "{}-f{}-{locality}",+ self.family, self.share_percent+ )+ }+}++/// The share of elements recolored per frame, from one element to all of them. A renderer+/// that retains clean entity subtrees should fall toward the left of this axis; one that+/// redraws everything is flat across it, so the two curves' distance is what retention+/// buys at each share.+///+/// The main line keeps each frame's change inside one entity, as a user action does. Two+/// `spread` points scatter it uniformly instead: the pessimistic bound, where every+/// changed element may dirty a different subtree.+fn changing_share_inputs() -> Vec<ChangingShareInput> {+ families()+ .into_iter()+ .flat_map(|family| {+ [+ (0, ChangeLocality::Localized),+ (1, ChangeLocality::Localized),+ (5, ChangeLocality::Localized),+ (25, ChangeLocality::Localized),+ (100, ChangeLocality::Localized),+ (5, ChangeLocality::Spread),+ (25, ChangeLocality::Spread),+ ]+ .into_iter()+ .map(move |(share_percent, locality)| ChangingShareInput {+ family: family.clone(),+ share_percent,+ locality,+ })+ })+ .collect()+}++// One seed rather than two: every family is multiplied by seven shares.+#[gpui::bench(inputs = changing_share_inputs(), input_name = "tree", iterations = 1, group = "RandomizedTree/changing share", fps = 120)]+fn changing_share(input: &ChangingShareInput, mut rng: StdRng, cx: &mut BenchAppContext) {+ let tree = &input.family.sample(&mut rng);+ let element_count = tree.config.element_count();+ let per_frame = if input.share_percent == 0 {+ 0+ } else {+ (input.share_percent * element_count).div_ceil(100).max(1)+ };+ let mut last = RecoloredChildren {+ recolored: 0,+ notified: 0,+ };+ let group = format!(+ "changing share f{}-{}",+ input.share_percent,+ input.locality_name()+ );+ let frames = measure(&group, tree, cx, |tree, cx| {+ if per_frame == 0 {+ cx.notify();+ } else {+ last = tree.recolor_children(per_frame, input.locality, cx);+ }+ });+ assert!(frames > 0);+ if per_frame > 0 {+ assert_eq!(last.recolored, per_frame.min(element_count));+ assert!(last.notified >= 1);+ }+}++// Frames take 0.4 to 2.5 ms, so a 1 s measurement is already hundreds to thousands of+// frames; Criterion's 3 s warm-up and 5 s measurement per id would make the suite take+// most of an hour. Pass `ITERATIONS=6`, `--warm-up-time` or `--measurement-time` for a+// closer comparison.+gpui::bench_group!(+ name = benches;+ config = criterion::Criterion::default()+ .warm_up_time(Duration::from_millis(500))+ .measurement_time(Duration::from_secs(1))+ .sample_size(20);+ targets = full_refresh,+ unchanged,+ leaf_style,+ leaf_bounds,+ root_layout,+ insert_remove,+ reorder,+ changing_share,+ family_summary+);+gpui::bench_main!(benches);diff --git a/crates/gpui_platform/src/gpui_platform.rs b/crates/gpui_platform/src/gpui_platform.rsindex 5e165d5c402..350a41f1749 100644--- a/crates/gpui_platform/src/gpui_platform.rs+++ b/crates/gpui_platform/src/gpui_platform.rs@@ -80,6 +80,22 @@ pub fn current_platform(headless: bool) -> Rc<dyn Platform> {}}+/// Returns the current platform's text system for benchmark contexts, built once per+/// thread and shared by every context on it.+///+/// `#[gpui::bench]` creates a context for every Criterion routine call. Building a whole+/// platform each time only to take its text system leaks the platform's threads on Linux+/// (a 20-worker dispatcher and a timer thread per call), which slows every later+/// benchmark in the process.+#[cfg(feature = "bench-support")]+pub fn bench_text_system() -> std::sync::Arc<dyn gpui::PlatformTextSystem> {+ thread_local! {+ static TEXT_SYSTEM: std::sync::Arc<dyn gpui::PlatformTextSystem> =+ current_platform(true).text_system();+ }+ TEXT_SYSTEM.with(|text_system| text_system.clone())+}+/// Returns a new [`HeadlessRenderer`] for the current platform, if available.#[cfg(any(feature = "bench-support", feature = "test-support"))]pub fn current_headless_renderer() -> anyhow::Result<Option<Box<dyn gpui::PlatformHeadlessRenderer>>>diff --git a/crates/gpui_wgpu/src/wgpu_context.rs b/crates/gpui_wgpu/src/wgpu_context.rsindex 4a6e9cbe0e4..4c55ef18e1a 100644--- a/crates/gpui_wgpu/src/wgpu_context.rs+++ b/crates/gpui_wgpu/src/wgpu_context.rs@@ -7,6 +7,7 @@ use std::sync::Arc;use std::sync::atomic::{AtomicBool, Ordering};use wgpu::TextureFormat;+#[derive(Clone)]pub struct WgpuContext {pub instance: wgpu::Instance,pub adapter: wgpu::Adapter,@@ -72,6 +73,12 @@ impl DeviceErrorState {*generation = last_error.0;last_error.1.clone()}++ /// The generation an observer should start from to see only errors raised after+ /// this call, for a renderer joining a device other renderers already used.+ pub fn current_generation(&self) -> u64 {+ self.last_error.lock().0+ }}#[derive(Clone, Copy, Debug, Eq, PartialEq)]@@ -173,6 +180,28 @@ impl WgpuContext {any(test, feature = "bench-support", feature = "test-support")))]pub(crate) fn new_headless() -> anyhow::Result<(Self, wgpu::TextureFormat)> {+ // Creating a device takes tens of milliseconds, and benchmarks create a headless+ // renderer for every window they open, so the process shares one device until it+ // is lost. A static rather than a thread-local: the cached device is never dropped,+ // because wgpu's queue reads its own thread-locals on drop and would panic if+ // dropped while a thread's thread-locals are being destroyed.+ static HEADLESS: Mutex<Option<(WgpuContext, wgpu::TextureFormat)>> = Mutex::new(None);+ let mut headless = HEADLESS.lock();+ if let Some((context, format)) = headless.as_ref()+ && !context.device_lost()+ {+ return Ok((context.clone(), *format));+ }+ let created = Self::create_headless()?;+ *headless = Some(created.clone());+ Ok(created)+ }++ #[cfg(all(+ not(target_family = "wasm"),+ any(test, feature = "bench-support", feature = "test-support")+ ))]+ fn create_headless() -> anyhow::Result<(Self, wgpu::TextureFormat)> {let instance = Self::instance(None);let device_id_filter = Self::device_id_filter();let (adapter, device, queue, dual_source_blending, color_texture_format, target_format) =diff --git a/crates/gpui_wgpu/src/wgpu_renderer.rs b/crates/gpui_wgpu/src/wgpu_renderer.rsindex 0f120bbad0e..8387528acbe 100644--- a/crates/gpui_wgpu/src/wgpu_renderer.rs+++ b/crates/gpui_wgpu/src/wgpu_renderer.rs@@ -2320,6 +2320,9 @@ pub struct WgpuHeadlessRenderer {impl WgpuHeadlessRenderer {pub fn new() -> anyhow::Result<Self> {let (context, target_format) = WgpuContext::new_headless()?;+ // The device may be shared with earlier renderers; observe only errors raised from+ // here on, including any while this renderer builds its pipelines.+ let observed_error_generation = context.errors().current_generation();let atlas = Arc::new(WgpuAtlas::from_context(&context));let core = WgpuRendererCore::new(&context,@@ -2332,7 +2335,7 @@ impl WgpuHeadlessRenderer {context,core,render_target: None,- observed_error_generation: 0,+ observed_error_generation,})}