layout: Report memory usage for fragment and box trees. (#36553)

Add memory reporter integration for the fragment and box trees that are
persisted in the layout thread.

Testing: Looked at the numbers for https://servo.org and
https://html.spec.whatwg.org/. The former was very small, but the latter
was 700mb.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-04-18 16:05:15 -04:00 committed by GitHub
parent add8c51f47
commit c787688afc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 230 additions and 69 deletions

View file

@ -8,8 +8,9 @@
//! Layout. Performs layout on the DOM, builds display lists and sends them to be
//! painted.
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::cell::{Cell, LazyCell, RefCell};
use std::collections::{HashMap, HashSet};
use std::ffi::c_void;
use std::fmt::Debug;
use std::process;
use std::sync::{Arc, LazyLock};
@ -38,7 +39,7 @@ use layout::query::{
use layout::traversal::RecalcStyle;
use layout::{BoxTree, FragmentTree};
use log::{debug, error};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use malloc_size_of::{MallocConditionalSizeOf, MallocSizeOf, MallocSizeOfOps};
use net_traits::image_cache::{ImageCache, UsePlaceholder};
use parking_lot::{Mutex, RwLock};
use profile_traits::mem::{Report, ReportKind};
@ -417,7 +418,28 @@ impl Layout for LayoutThread {
reports.push(Report {
path: path![formatted_url, "layout-thread", "font-context"],
kind: ReportKind::ExplicitJemallocHeapSize,
size: self.font_context.size_of(ops),
size: self.font_context.conditional_size_of(ops),
});
reports.push(Report {
path: path![formatted_url, "layout-thread", "box-tree"],
kind: ReportKind::ExplicitJemallocHeapSize,
size: self
.box_tree
.borrow()
.as_ref()
.map_or(0, |tree| tree.conditional_size_of(ops)),
});
reports.push(Report {
path: path![formatted_url, "layout-thread", "fragment-tree"],
kind: ReportKind::ExplicitJemallocHeapSize,
size: self
.fragment_tree
.borrow()
.as_ref()
.map(|tree| tree.conditional_size_of(ops))
.unwrap_or_default(),
});
}
@ -1210,3 +1232,7 @@ impl Debug for LayoutFontMetricsProvider {
f.debug_tuple("LayoutFontMetricsProvider").finish()
}
}
thread_local!(static SEEN_POINTERS: LazyCell<RefCell<HashSet<*const c_void>>> = const {
LazyCell::new(|| RefCell::new(HashSet::new()))
});