Closed Bug 1822184 Opened 2 years ago Closed 2 years ago

Consider to use AutoTArray for ContextState::clipsAndTransforms

Categories

(Core :: Graphics: Canvas2D, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
113 Branch
Tracking Status
firefox113 --- fixed

People

(Reporter: smaug, Assigned: jfkthame)

References

(Blocks 1 open bug)

Details

(Whiteboard: [sp3])

Attachments

(1 file)

Whiteboard: [sp3]

I did a bit of random browsing (various canvas demos/tests, and some Google Docs) with an instrumented build, and found that in the overwhelming majority of instances, we only ever append one element to this array. So yes, we should try to eliminate that allocation.

Rather than an AutoTArray, where we'll end up using LastElement() as the "current" state -- which means every access has to look up the array length and use it as an index -- maybe we should store a ClipState directly in the ContextState, along with an auxiliary (non-auto) array, usually unused, to push any saved states to. The overall footprint of that should be comparable to a 1-element AutoTArray, but with optimized access for the current state.

[edit: After looking over the code, I don't think that's worth doing, as our usage patterns mostly aren't focused on just accessing the "current" (last) element but on iterating over the states. So the simple AutoTArray approach makes most sense here.]

Assignee: nobody → jfkthame
Status: NEW → ASSIGNED

It doesn't work to simply use AutoTArray here because the canvas context has an array of ContextStates, which requires ContextState to be memmovable.

In theory we could probably resolve that by adding ContextState to the types declared here as needing to use move constructors rather than memmove. But that would end up pretty invasive, as we can't forward-declare the nested ContextState class; we'd have to hoist it out of CanvasRenderingContext2D, and expose it more widely. And no longer being able to memmove the state would come with its own performance impact.

So instead, I propose to use a memmove-able helper to store a single ClipState element directly in the ContextState, and convert to an nsTArray only when more than one element is pushed to the list.

Attachment #9323184 - Attachment description: Bug 1822184 - Use a 1-element AutoTArray for the clipsAndTransforms stack in canvas ContextState, as most instances only ever set a single element. r=#gfx-reviewers → Bug 1822184 - Create an ElementOrArray<T> helper in CanvasRenderingContext2D, and use this to store a single ClipState directly without a separate array allocation. r=#gfx-reviewers
Pushed by jkew@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/f38530a9bedf Create an ElementOrArray<T> helper in CanvasRenderingContext2D, and use this to store a single ClipState directly without a separate array allocation. r=gfx-reviewers,lsalzman

Backed out for causing build bustage on CanvasRenderingContext2D.h

[task 2023-03-17T11:44:54.912Z] 11:44:54     INFO -  In file included from /builds/worker/checkouts/gecko/dom/canvas/CanvasGradient.cpp:7:0,
[task 2023-03-17T11:44:54.912Z] 11:44:54     INFO -                   from Unified_cpp_dom_canvas0.cpp:11:
[task 2023-03-17T11:44:54.913Z] 11:44:54     INFO -  /builds/worker/workspace/obj-build/dist/include/mozilla/dom/CanvasRenderingContext2D.h: In member function 'T* mozilla::dom::CanvasRenderingContext2D::ElementOrArray<T>::AppendElement(const T&) [with T = mozilla::dom::CanvasRenderingContext2D::ClipState]':
[task 2023-03-17T11:44:54.913Z] 11:44:54    ERROR -  /builds/worker/workspace/obj-build/dist/include/mozilla/dom/CanvasRenderingContext2D.h:147:5: error: control reaches end of non-void function [-Werror=return-type]
[task 2023-03-17T11:44:54.913Z] 11:44:54     INFO -       }
[task 2023-03-17T11:44:54.914Z] 11:44:54     INFO -       ^
[task 2023-03-17T11:44:54.914Z] 11:44:54     INFO -  In file included from Unified_cpp_dom_canvas0.cpp:38:0:
[task 2023-03-17T11:44:54.914Z] 11:44:54     INFO -  /builds/worker/checkouts/gecko/dom/canvas/CanvasRenderingContext2D.cpp: In member function 'mozilla::dom::TextMetrics* mozilla::dom::CanvasRenderingContext2D::DrawOrMeasureText(const nsAString&, float, float, const mozilla::dom::Optional<double>&, mozilla::dom::CanvasRenderingContext2D::TextDrawOperation, mozilla::ErrorResult&)':
[task 2023-03-17T11:44:54.915Z] 11:44:54  WARNING -  /builds/worker/checkouts/gecko/dom/canvas/CanvasRenderingContext2D.cpp:4448:10: warning: 'isRTL' may be used uninitialized in this function [-Wmaybe-uninitialized]
[task 2023-03-17T11:44:54.915Z] 11:44:54     INFO -     aError = nsBidiPresUtils::ProcessText(
[task 2023-03-17T11:44:54.915Z] 11:44:54     INFO -     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[task 2023-03-17T11:44:54.915Z] 11:44:54     INFO -         textToDraw.get(), textToDraw.Length(),
[task 2023-03-17T11:44:54.916Z] 11:44:54     INFO -         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[task 2023-03-17T11:44:54.916Z] 11:44:54     INFO -         isRTL ? intl::BidiEmbeddingLevel::RTL() : intl::BidiEmbeddingLevel::LTR(),
[task 2023-03-17T11:44:54.916Z] 11:44:54     INFO -         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[task 2023-03-17T11:44:54.916Z] 11:44:54     INFO -         presContext, processor, nsBidiPresUtils::MODE_DRAW, nullptr, 0, nullptr,
[task 2023-03-17T11:44:54.917Z] 11:44:54     INFO -         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[task 2023-03-17T11:44:54.917Z] 11:44:54     INFO -         mBidiEngine);
[task 2023-03-17T11:44:54.917Z] 11:44:54     INFO -         ~~~~~~~~~~~~
[task 2023-03-17T11:44:54.917Z] 11:44:54     INFO -  cc1plus: all warnings being treated as errors
Flags: needinfo?(jfkthame)
Pushed by jkew@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/96d01e70878e Create an ElementOrArray<T> helper in CanvasRenderingContext2D, and use this to store a single ClipState directly without a separate array allocation. r=gfx-reviewers,lsalzman
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 113 Branch
Flags: needinfo?(jfkthame)
Blocks: 1823215
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: