Skip to content

More granular script thread memory reporting - #46931

Merged
jdm merged 1 commit into
servo:mainfrom
pylbrecht:pylbrecht/push-usq
Aug 25, 2026
Merged

jdm merged 1 commit into
servo:mainfrom
pylbrecht:pylbrecht/push-usq

Conversation

@pylbrecht

@pylbrecht pylbrecht commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

So far, all Rust memory for DOM objects ended up in a single category (MallocHeap).

To get a more fine-grained view into memory usage, we are now counting DOM object sizes:

struct DocumentSizes {
    element_nodes_size: usize,
    text_nodes_size: usize,
    attribute_nodes_size: usize,
    other_nodes_size: usize,
}

For flexibility reasons, we do the counting within Servo instead of within SpiderMonkey.

When collecting memory reports from the script thread, we traverse each document and accumulate node sizes into DocumentSizes. We remember, which nodes we already visited (ALREADY_COMPUTED_OBJECTS), and ignore these in get_size() to avoid counting them twice.

image

Testing: manual
Fixes: #44995

@pylbrecht
pylbrecht force-pushed the pylbrecht/push-usq branch from e5e7cc1 to b91e14a Compare August 2, 2026 07:46
Comment thread components/script/script_runtime.rs Outdated
@pylbrecht
pylbrecht force-pushed the pylbrecht/push-usq branch 3 times, most recently from f5cde69 to 0f1881d Compare August 5, 2026 20:10
@pylbrecht
pylbrecht force-pushed the pylbrecht/push-usq branch 5 times, most recently from 5458f17 to 4385d39 Compare August 20, 2026 16:59

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start! We just need to shuffle around a few pieces to make sure that we don't overcount.

Comment thread components/script/dom/document/document.rs Outdated
Comment thread components/script/dom/document/document.rs Outdated
Comment thread components/script/script_runtime.rs Outdated
Comment thread components/script/script_runtime.rs Outdated
Comment thread components/script/script_runtime.rs Outdated
Comment thread components/script/script_runtime.rs Outdated
Comment thread components/script/event_loop/script_thread.rs Outdated
Comment thread components/script/event_loop/script_thread.rs Outdated
@pylbrecht
pylbrecht force-pushed the pylbrecht/push-usq branch 5 times, most recently from cd06bef to 7cdbea6 Compare August 23, 2026 05:29
@pylbrecht
pylbrecht force-pushed the pylbrecht/push-usq branch 3 times, most recently from 100a3dc to 288c93d Compare August 25, 2026 10:24
@pylbrecht
pylbrecht marked this pull request as ready for review August 25, 2026 10:24
@pylbrecht
pylbrecht requested a review from gterzian as a code owner August 25, 2026 10:24
@pylbrecht

Copy link
Copy Markdown
Contributor Author

@jdm, I cleaned up the commit mess and added a PR description. Let me know if you have any more feedback. Happy to address it!

So far, all Rust memory for DOM objects ended up in a single category
(MallocHeap).

To get a more fine-grained view into memory usage, we are now counting
DOM object sizes:

```rust
struct DocumentSizes {
    element_nodes_size: usize,
    text_nodes_size: usize,
    attribute_nodes_size: usize,
    other_nodes_size: usize,
}
```

For flexibility reasons, we do the counting within Servo instead of
within SpiderMonkey.

When collecting memory reports from the script thread, we traverse each
document and accumulate node sizes into `DocumentSizes`. We remember,
which nodes we already visited (`ALREADY_COMPUTED_OBJECTS`), and ignore
these in `get_size()` to avoid counting them twice.

---

Signed-off-by: pylbrecht <pylbrecht@mailbox.org>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Aug 25, 2026

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Aug 25, 2026
@jdm
jdm added this pull request to the merge queue Aug 25, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Aug 25, 2026
@pylbrecht

Copy link
Copy Markdown
Contributor Author

Thanks, @jdm! I couldn't have done it without your support. 🙏

Merged via the queue into servo:main with commit b4b6d8c Aug 25, 2026
36 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Aug 25, 2026
pull Bot pushed a commit to Haofei/servo that referenced this pull request Aug 28, 2026
…ervo#47531)

Building on top of servo#46931, this adds a new `out-of-tree` entry to the
DOM memory report which automatically groups all memory for each
previously-unmeasured WebIDL interface that is encountered when
measuring the objects in the GC graph. Any object measured when
traversing the tree is excluded from this, so we won't count any objects
twice even if we add new grouping categories.

Testing: No automated tests for about:memory at this time.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
So far, all Rust memory for DOM objects ended up in a single category
(MallocHeap).

To get a more fine-grained view into memory usage, we are now counting
DOM object sizes:

```rust
struct DocumentSizes {
    element_nodes_size: usize,
    text_nodes_size: usize,
    attribute_nodes_size: usize,
    other_nodes_size: usize,
}
```

For flexibility reasons, we do the counting within Servo instead of
within SpiderMonkey.

When collecting memory reports from the script thread, we traverse each
document and accumulate node sizes into `DocumentSizes`. We remember,
which nodes we already visited (`ALREADY_COMPUTED_OBJECTS`), and ignore
these in `get_size()` to avoid counting them twice.

<img width="1354" height="1568" alt="image"
src="https://github.com/user-attachments/assets/fc217398-fd99-4665-b9bf-9ea0f6ed140b"
/>

---

Testing: manual
Fixes: servo#44995

Signed-off-by: pylbrecht <pylbrecht@mailbox.org>
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
…ervo#47531)

Building on top of servo#46931, this adds a new `out-of-tree` entry to the
DOM memory report which automatically groups all memory for each
previously-unmeasured WebIDL interface that is encountered when
measuring the objects in the GC graph. Any object measured when
traversing the tree is excluded from this, so we won't count any objects
twice even if we add new grouping categories.

Testing: No automated tests for about:memory at this time.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Psychpsyo pushed a commit to Psychpsyo/servo that referenced this pull request Oct 12, 2026
So far, all Rust memory for DOM objects ended up in a single category
(MallocHeap).

To get a more fine-grained view into memory usage, we are now counting
DOM object sizes:

```rust
struct DocumentSizes {
    element_nodes_size: usize,
    text_nodes_size: usize,
    attribute_nodes_size: usize,
    other_nodes_size: usize,
}
```

For flexibility reasons, we do the counting within Servo instead of
within SpiderMonkey.

When collecting memory reports from the script thread, we traverse each
document and accumulate node sizes into `DocumentSizes`. We remember,
which nodes we already visited (`ALREADY_COMPUTED_OBJECTS`), and ignore
these in `get_size()` to avoid counting them twice.

<img width="1354" height="1568" alt="image"
src="https://github.com/user-attachments/assets/fc217398-fd99-4665-b9bf-9ea0f6ed140b"
/>

---

Testing: manual
Fixes: servo#44995

Signed-off-by: pylbrecht <pylbrecht@mailbox.org>
Psychpsyo pushed a commit to Psychpsyo/servo that referenced this pull request Oct 12, 2026
…ervo#47531)

Building on top of servo#46931, this adds a new `out-of-tree` entry to the
DOM memory report which automatically groups all memory for each
previously-unmeasured WebIDL interface that is encountered when
measuring the objects in the GC graph. Any object measured when
traversing the tree is excluded from this, so we won't count any objects
twice even if we add new grouping categories.

Testing: No automated tests for about:memory at this time.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
@pylbrecht
pylbrecht deleted the pylbrecht/push-usq branch October 18, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Script thread memory report is not granular enough

3 participants