Closed Bug 1357694 Opened 7 years ago Closed 7 years ago

Avoid heap allocations (usually) in BidiLineData

Categories

(Core :: Layout, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla55
Performance Impact ?
Tracking Status
firefox55 --- fixed

People

(Reporter: jfkthame, Assigned: jfkthame)

References

Details

Attachments

(1 file)

The BidiLineData struct contains several nsTArray members, which end up doing heap allocations. We can avoid this (for all but the most pathologically complex cases) by making these AutoTArrays instead. BidiLineData is a short-lived stack-allocated struct, and AFAICT is not used recursively, so the increase in the struct's size is relatively unimportant compared to avoiding several heap allocations every time it's used.

In testing on my MBPro, this shaves somewhere around 100ms off the time for a window-resize reflow of https://html.spec.whatwg.org/.
Assignee: nobody → jfkthame
Status: NEW → ASSIGNED
Comment on attachment 8859532 [details] [diff] [review]
Use AutoTArray for the array members in BidiLineData, to reduce heap allocation costs

Review of attachment 8859532 [details] [diff] [review]:
-----------------------------------------------------------------

r=me with nits:

::: layout/base/nsBidiPresUtils.cpp
@@ +371,5 @@
>  };
>  
>  struct BidiLineData {
> +  AutoTArray<nsIFrame*, 16> mLogicalFrames;
> +  AutoTArray<nsIFrame*, 16> mVisualFrames;

Please declare the struct as MOZ_STACK_CLASS (and add an include for mozilla/Attributes.h to provide that), to enforce that these AutoTArrays are really on the stack.

@@ +418,5 @@
>  
>      // Strip virtual frames
>      if (hasVirtualControls) {
>        auto originalCount = mLogicalFrames.Length();
> +      AutoTArray<int32_t, 16> realFrameMap;

If you can (and I think you can), we should still pass in |originalCount| to this constructor, like the old code does.

When originalCount is under 16, it won't matter, & that's fine.  But for cases when |originalCount| is larger than 16 (and especially when it's significantly larger), this will let us preallocate the correct amount of heap memory right away, rather than spinning our wheels with the default nsTArray doubling behavior.
Attachment #8859532 - Flags: review?(dholbert) → review+
(In reply to Daniel Holbert [:dholbert] from comment #2)
> Comment on attachment 8859532 [details] [diff] [review]
> Use AutoTArray for the array members in BidiLineData, to reduce heap
> allocation costs
> 
> Review of attachment 8859532 [details] [diff] [review]:
> -----------------------------------------------------------------
> 
> r=me with nits:
> 
> ::: layout/base/nsBidiPresUtils.cpp
> @@ +371,5 @@
> >  };
> >  
> >  struct BidiLineData {
> > +  AutoTArray<nsIFrame*, 16> mLogicalFrames;
> > +  AutoTArray<nsIFrame*, 16> mVisualFrames;
> 
> Please declare the struct as MOZ_STACK_CLASS (and add an include for
> mozilla/Attributes.h to provide that), to enforce that these AutoTArrays are
> really on the stack.

Will do.

> @@ +418,5 @@
> >  
> >      // Strip virtual frames
> >      if (hasVirtualControls) {
> >        auto originalCount = mLogicalFrames.Length();
> > +      AutoTArray<int32_t, 16> realFrameMap;
> 
> If you can (and I think you can), we should still pass in |originalCount| to
> this constructor, like the old code does.

That doesn't build; AutoTArray doesn't have a constructor that takes an initial capacity. I suppose we could call SetCapacity(originalCount) on it immediately after construction, so that if it does need enlarging, we'll only do one allocation rather than potentially several steps. I'll add that (though I suspect it'll be so rare as to be insignificant in practice).
https://hg.mozilla.org/integration/mozilla-inbound/rev/bf3ee74eb31893e0f0bbcb3f6ffdc329043b3c70
Bug 1357694 - Use AutoTArray for the array members in BidiLineData, to reduce heap allocation costs. r=dholbert
https://hg.mozilla.org/mozilla-central/rev/bf3ee74eb318
Status: ASSIGNED → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla55
Performance Impact: --- → ?
Whiteboard: [qf?]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: