From 19b5e14851891061cde8fedcec583011a20c1564 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Wed, 2 Jul 2025 17:02:56 +0200 Subject: [PATCH] layout: Improve and expand category names for layout profiling (#37833) This improves naming of layout categories and adds tracing for each layout phase. Testing: This just adds / adjusts profiling categories, so doesn't need tests. Signed-off-by: Martin Robinson Co-authored-by: Oriol Brufau --- components/layout/flow/root.rs | 2 ++ components/layout/layout_impl.rs | 46 +++++++++++++++++++------------ components/layout/traversal.rs | 1 + components/shared/profile/time.rs | 4 +-- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/components/layout/flow/root.rs b/components/layout/flow/root.rs index b199704f785..72db9db8fea 100644 --- a/components/layout/flow/root.rs +++ b/components/layout/flow/root.rs @@ -45,6 +45,7 @@ pub struct BoxTree { } impl BoxTree { + #[servo_tracing::instrument(name = "Box Tree Construction", skip_all)] pub(crate) fn construct(context: &LayoutContext, root_element: ServoLayoutNode<'_>) -> Self { let boxes = construct_for_root_element(context, root_element); @@ -191,6 +192,7 @@ fn construct_for_root_element( } impl BoxTree { + #[servo_tracing::instrument(name = "Fragment Tree Construction", skip_all)] pub(crate) fn layout( &self, layout_context: &LayoutContext, diff --git a/components/layout/layout_impl.rs b/components/layout/layout_impl.rs index a4db91ecf87..cd0e84e3710 100644 --- a/components/layout/layout_impl.rs +++ b/components/layout/layout_impl.rs @@ -440,7 +440,7 @@ impl Layout for LayoutThread { fn reflow(&mut self, reflow_request: ReflowRequest) -> Option { time_profile!( - profile_time::ProfilerCategory::LayoutPerform, + profile_time::ProfilerCategory::Layout, self.profiler_metadata(), self.time_profiler_chan.clone(), || self.handle_reflow(reflow_request), @@ -821,27 +821,34 @@ impl LayoutThread { .restyle .as_ref() .expect("Should not get here if there is not restyle."); - let dirty_root = unsafe { - ServoLayoutNode::new(&restyle.dirty_root.unwrap()) - .as_element() - .unwrap() - }; - let recalc_style_traversal = RecalcStyle::new(&layout_context); - let token = { - let shared = - DomTraversal::::shared_context(&recalc_style_traversal); - RecalcStyle::pre_traverse(dirty_root, shared) - }; + let recalc_style_traversal; + let dirty_root; + { + #[cfg(feature = "tracing")] + let _span = tracing::trace_span!("Styling", servo_profiling = true).entered(); - if !token.should_traverse() { - layout_context.style_context.stylist.rule_tree().maybe_gc(); - return (RestyleDamage::empty(), IFrameSizes::default()); + let original_dirty_root = unsafe { + ServoLayoutNode::new(&restyle.dirty_root.unwrap()) + .as_element() + .unwrap() + }; + + recalc_style_traversal = RecalcStyle::new(&layout_context); + let token = { + let shared = + DomTraversal::::shared_context(&recalc_style_traversal); + RecalcStyle::pre_traverse(original_dirty_root, shared) + }; + + if !token.should_traverse() { + layout_context.style_context.stylist.rule_tree().maybe_gc(); + return (RestyleDamage::empty(), IFrameSizes::default()); + } + + dirty_root = driver::traverse_dom(&recalc_style_traversal, token, rayon_pool).as_node(); } - let dirty_root: ServoLayoutNode = - driver::traverse_dom(&recalc_style_traversal, token, rayon_pool).as_node(); - let root_node = root_element.as_node(); let mut damage = compute_damage_and_repair_style(&layout_context.style_context, root_node); if viewport_changed { @@ -909,6 +916,7 @@ impl LayoutThread { (damage, std::mem::take(&mut *iframe_sizes)) } + #[servo_tracing::instrument(name = "Overflow Calculation", skip_all)] fn calculate_overflow(&self, damage: RestyleDamage) { if !damage.contains(RestyleDamage::RECALCULATE_OVERFLOW) { return; @@ -927,6 +935,7 @@ impl LayoutThread { self.need_new_stacking_context_tree.set(true); } + #[servo_tracing::instrument(name = "Stacking Context Tree Construction", skip_all)] fn build_stacking_context_tree(&self, reflow_request: &ReflowRequest, damage: RestyleDamage) { if !ReflowPhases::necessary(&reflow_request.reflow_goal) .contains(ReflowPhases::StackingContextTreeConstruction) @@ -999,6 +1008,7 @@ impl LayoutThread { /// Build the display list for the current layout and send it to the renderer. If no display /// list is built, returns false. + #[servo_tracing::instrument(name = "Display List Construction", skip_all)] fn build_display_list( &self, reflow_request: &ReflowRequest, diff --git a/components/layout/traversal.rs b/components/layout/traversal.rs index 7723ca4cb25..d65afbe093f 100644 --- a/components/layout/traversal.rs +++ b/components/layout/traversal.rs @@ -92,6 +92,7 @@ where } } +#[servo_tracing::instrument(skip_all)] pub(crate) fn compute_damage_and_repair_style( context: &SharedStyleContext, node: ServoLayoutNode<'_>, diff --git a/components/shared/profile/time.rs b/components/shared/profile/time.rs index bbc6515b4f8..9e2059b11d0 100644 --- a/components/shared/profile/time.rs +++ b/components/shared/profile/time.rs @@ -66,7 +66,7 @@ pub enum ProfilerCategory { Compositing = 0x00, /// The script thread is doing layout work. - LayoutPerform = 0x10, + Layout = 0x10, ImageSaving = 0x51, ScriptAttachLayout = 0x60, @@ -126,7 +126,7 @@ impl ProfilerCategory { pub const fn variant_name(&self) -> &'static str { match self { ProfilerCategory::Compositing => "Compositing", - ProfilerCategory::LayoutPerform => "LayoutPerform", + ProfilerCategory::Layout => "Layout", ProfilerCategory::ImageSaving => "ImageSaving", ProfilerCategory::ScriptAttachLayout => "ScriptAttachLayout", ProfilerCategory::ScriptConstellationMsg => "ScriptConstellationMsg",