Reduce number of vector buffer reallocs in Regions::unite .

When acceleratedCompositingForFixedPositionEnabled is not set
FrameView::scrollContentsFastPath() performs invalidate for every sticky or
fixed layer. Page http://people.mozilla.org/~jorendorff/es6-draft.html#sec-terms-and-definitions-symbol-type 
causes Region::unite a lot. Vector buffer reallocations are hot in profiler.
This patch pre-allocates vector buffers in Region::Shape::shapeOperation() and
moves Vector allocation out of inner loop. This significantly reduces number of
vector buffer reallocs.

BUG=335306

Review URL: https://codereview.chromium.org/183663030

git-svn-id: svn://svn.chromium.org/blink/trunk@175822 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent f633d337
......@@ -207,6 +207,12 @@ bool Region::Shape::compareShapes(const Shape& aShape, const Shape& bShape)
return result;
}
void Region::Shape::trimCapacities()
{
m_segments.shrinkToReasonableCapacity();
m_spans.shrinkToReasonableCapacity();
}
struct Region::Shape::CompareContainsOperation {
const static bool defaultResult = true;
inline static bool aOutsideB(bool& /* result */) { return false; }
......@@ -233,6 +239,12 @@ Region::Shape::Shape(const IntRect& rect)
appendSpan(rect.maxY());
}
Region::Shape::Shape(size_t segmentsCapacity, size_t spansCapacity)
{
m_segments.reserveCapacity(segmentsCapacity);
m_spans.reserveCapacity(spansCapacity);
}
void Region::Shape::appendSpan(int y)
{
m_spans.append(Span(y, m_segments.size()));
......@@ -393,7 +405,9 @@ Region::Shape Region::Shape::shapeOperation(const Shape& shape1, const Shape& sh
COMPILE_ASSERT(!(!Operation::shouldAddRemainingSegmentsFromSpan1 && Operation::shouldAddRemainingSegmentsFromSpan2), invalid_segment_combination);
COMPILE_ASSERT(!(!Operation::shouldAddRemainingSpansFromShape1 && Operation::shouldAddRemainingSpansFromShape2), invalid_span_combination);
Shape result;
size_t segmentsCapacity = shape1.segmentsSize() + shape2.segmentsSize();
size_t spansCapacity = shape1.spansSize() + shape2.spansSize();
Shape result(segmentsCapacity, spansCapacity);
if (Operation::trySimpleOperation(shape1, shape2, result))
return result;
......@@ -409,6 +423,9 @@ Region::Shape Region::Shape::shapeOperation(const Shape& shape1, const Shape& sh
SegmentIterator segments2 = 0;
SegmentIterator segments2End = 0;
Vector<int, 32> segments;
segments.reserveCapacity(std::max(shape1.segmentsSize(), shape2.segmentsSize()));
// Iterate over all spans.
while (spans1 != spans1End && spans2 != spans2End) {
int y = 0;
......@@ -435,7 +452,9 @@ Region::Shape Region::Shape::shapeOperation(const Shape& shape1, const Shape& sh
SegmentIterator s1 = segments1;
SegmentIterator s2 = segments2;
Vector<int, 32> segments;
// Clear vector without dropping capacity.
segments.resize(0);
ASSERT(segments.capacity());
// Now iterate over the segments in each span and construct a new vector of segments.
while (s1 != segments1End && s2 != segments2End) {
......@@ -476,6 +495,8 @@ Region::Shape Region::Shape::shapeOperation(const Shape& shape1, const Shape& sh
else if (Operation::shouldAddRemainingSpansFromShape2 && spans2 != spans2End)
result.appendSpans(shape2, spans2, spans2End);
result.trimCapacities();
return result;
}
......
......@@ -78,6 +78,7 @@ private:
public:
Shape();
Shape(const IntRect&);
Shape(size_t segmentsCapacity, size_t spansCapacity);
IntRect bounds() const;
bool isEmpty() const { return m_spans.isEmpty(); }
......@@ -86,10 +87,12 @@ private:
typedef const Span* SpanIterator;
SpanIterator spansBegin() const;
SpanIterator spansEnd() const;
size_t spansSize() const { return m_spans.size(); }
typedef const int* SegmentIterator;
SegmentIterator segmentsBegin(SpanIterator) const;
SegmentIterator segmentsEnd(SpanIterator) const;
size_t segmentsSize() const { return m_segments.size(); }
static Shape unionShapes(const Shape& shape1, const Shape& shape2);
static Shape intersectShapes(const Shape& shape1, const Shape& shape2);
......@@ -103,6 +106,7 @@ private:
template<typename CompareOperation>
static bool compareShapes(const Shape& shape1, const Shape& shape2);
void trimCapacities();
#ifndef NDEBUG
void dump() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment