Retain opt-in layout performance diagnostics

This commit is contained in:
iris-ai committed 2026-09-14 16:42:03 -04:00
1 parent 84f589e364
commit 480f0bc99f
8 files changed
+613 -22

No files matched your search

+28 -3
View File
@@ -1,3 +1,5 @@
#[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter};
use crate::{
Axis, Len, RenderedText, Size, StrongWidget, TextAttrs, TextBuffer, TextData, TextureHandle,
UiRegion, UiRenderState, UiRsc, UiScalar, UiVec2, WidgetId,
@@ -37,6 +39,8 @@ impl<'a> Painter<'a> {
/// Takes the kind, for a caller writing many of one primitive.
fn write<P: Primitive>(&mut self, kind: PrimitiveKind<P>, primitive: P, region: UiRegion) {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::PrimitiveWrites);
let h = self.state.layers.write(
self.layer,
PrimitiveInst {
@@ -104,6 +108,8 @@ impl<'a> Painter<'a> {
id: &'s StrongWidget<W>,
region: UiRegion,
) -> DrawResult<'s, 'a, W> {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::PlaceCalls);
let region = region.within(&self.region);
self.widget_at(id, region, true)
}
@@ -139,9 +145,24 @@ impl<'a> Painter<'a> {
/// What a child says its length is without being drawn, if it can say.
/// Asking counts as reading its size.
pub fn size_hint<W: ?Sized>(&mut self, id: &StrongWidget<W>, axis: Axis) -> Option<Len> {
let hint = self.rsc.widgets().get_dyn(id.id())?.size_hint(axis)?;
self.depend_on_size(id);
Some(hint)
let hint = self
.rsc
.widgets()
.get_dyn(id.id())
.and_then(|widget| widget.size_hint(axis));
match hint {
Some(hint) => {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::HintHits);
self.depend_on_size(id);
Some(hint)
}
None => {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::HintMisses);
None
}
}
}
fn depend_on_size<W: ?Sized>(&mut self, child: &StrongWidget<W>) {
@@ -156,6 +177,8 @@ impl<'a> Painter<'a> {
attrs: &TextAttrs,
width: Option<f32>,
) -> RenderedText {
#[cfg(feature = "layout-diagnostics")]
diag::render_text(self.id, self.rsc.widgets().label(self.id), width);
let ui = self.rsc.ui_mut();
ui.text.render(buffer, attrs, width)
}
@@ -238,6 +261,8 @@ pub struct DrawResult<'p, 'a, W: ?Sized> {
impl<W: ?Sized> DrawResult<'_, '_, W> {
pub fn size(self) -> Size {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::SizeReads);
self.painter.depend_on_size(self.child);
self.size
}