Closed Bug 1812774 Opened 1 year ago Closed 1 year ago

Use AutoTArray in FlattenedDisplayListIterator to reduce reallocation overhead

Categories

(Core :: Web Painting, task)

task

Tracking

()

RESOLVED FIXED
111 Branch
Tracking Status
firefox111 --- fixed

People

(Reporter: dholbert, Assigned: dholbert)

References

(Blocks 1 open bug)

Details

(Whiteboard: [sp3])

Attachments

(1 file)

FlattenedDisplayListIterator is (in practice) a stack-only object, though it has a heap-backed nsTArray member-var called mStack.

In a run of Speedometer 3, I got 3 samples into nsTArray::AppendElement for that variable:
https://share.firefox.dev/3Y1MH3v

We can avoid spending any measurable time appending there if we switch to a small AutoTArray. Given that the iterator itself is a temporary stack-allocated thing, it's fine for it to bring along a little bit of extra memory.

I ran Speedometer v3 in a local build with a printf to display mStack.Length() after each mStack.AppendElement call here:
https://searchfox.org/mozilla-central/rev/ef5dad8c11a556acd68ca3b9dcd77b226e0aaa29/layout/painting/nsDisplayList.h#6807

I got:

  • 21620 lines logging a length of 1
  • 10074 lines logging a length of 2
  • 707 lines logging a length of 3
  • ...and nothing larger than that.

(Note that there's a bit of double-dipping here -- every array that reaches 3 will also log for 1 and 2 on the way there.)

I also did a quick visit to some real web content for a bit of real-world usage -- I went to https://example.org/ , and then https://google.com and did a search for "test", and then https://maps.google.com , and then I quit. During that experiment, I got:

  • 14648 lines logging a length of 1
  • 915 lines logging a length of 2
  • 662 lines logging a length of 3
  • 34 lines logging a length of 4
  • ...and nothing larger than that.

So it seems that size <= 2 is by far the dominant scenario, as well as size=3 to a lesser extent; and sizes of 4-and-above are much rarer (and probably fine to spend a little time on allocation for).

I'm going to post a patch using an AutoTArray of size 3 so that we don't have to reallocate up to that size.

(This patch doesn't change behavior.)

In addition to the switch-to-AutoTArray, this patch also annotates
FlattenedDisplayListIterator as stack-only, to make it clear that the
AutoTArray buffer won't bloat the size of any long-lived heap-allocated object.

I've chosen 3 as the default size for the AutoTArray based on the observed size
of this member-variable in a run of the Speedometer 3 benchmark, as well as
during a short browsing session. See notes on Bug 1812774 for more details.

Pushed by dholbert@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/5b9df698dbea
Use AutoTArray to avoid allocation churn in FlattenedDisplayListIterator. r=tnikkel
Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 111 Branch
Whiteboard: [sp3]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: