Bug 1346610 - Compute width and height only when necessary to avoid exponential time complexity r=longsonr

When only width is needed, we should not compute height, and vice versa. Otherwise
there are exponentially repeated computations for nested svg elements, which will
make the tab unresponsive due to 100% CPU usage for exponentially long time.

Differential Revision: https://phabricator.services.mozilla.com/D20947

--HG--
extra : moz-landing-system : lando
This commit is contained in:
violet 2019-03-08 07:27:24 +00:00
parent e84d7f028b
commit 6756284172
3 changed files with 84 additions and 7 deletions

View File

@ -205,19 +205,32 @@ float SVGViewportElement::GetLength(uint8_t aCtxType) {
? &GetViewBoxInternal().GetAnimValue()
: nullptr;
float h, w;
float h = 0.0f, w = 0.0f;
bool shouldComputeWidth =
(aCtxType == SVGContentUtils::X || aCtxType == SVGContentUtils::XY),
shouldComputeHeight =
(aCtxType == SVGContentUtils::Y || aCtxType == SVGContentUtils::XY);
if (viewbox) {
w = viewbox->width;
h = viewbox->height;
} else if (IsInner()) {
SVGViewportElement* ctx = GetCtx();
w = mLengthAttributes[ATTR_WIDTH].GetAnimValue(ctx);
h = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(ctx);
if (shouldComputeWidth) {
w = mLengthAttributes[ATTR_WIDTH].GetAnimValue(ctx);
}
if (shouldComputeHeight) {
h = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(ctx);
}
} else if (ShouldSynthesizeViewBox()) {
w = ComputeSynthesizedViewBoxDimension(mLengthAttributes[ATTR_WIDTH],
mViewportWidth, this);
h = ComputeSynthesizedViewBoxDimension(mLengthAttributes[ATTR_HEIGHT],
mViewportHeight, this);
if (shouldComputeWidth) {
w = ComputeSynthesizedViewBoxDimension(mLengthAttributes[ATTR_WIDTH],
mViewportWidth, this);
}
if (shouldComputeHeight) {
h = ComputeSynthesizedViewBoxDimension(mLengthAttributes[ATTR_HEIGHT],
mViewportHeight, this);
}
} else {
w = mViewportWidth;
h = mViewportHeight;

View File

@ -91,3 +91,4 @@ load 1477853.html
load 1486488.html
load 1493447.html
skip-if(Android) load 1507961-1.html # times out on Android due to the test size
load test_nested_svg.html

View File

@ -0,0 +1,63 @@
<!-- if not handled properly, this file will cause 100% CPU for exponentially long time -->
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>
<svg>

After

Width:  |  Height:  |  Size: 464 B