Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2221)

Unified Diff: Source/platform/geometry/Region.cpp

Issue 183663030: Reduce number of vector buffer reallocs in Regions::unite . (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Use new Vector::shrinkToReasonableCapacity() API. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/platform/geometry/Region.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/geometry/Region.cpp
diff --git a/Source/platform/geometry/Region.cpp b/Source/platform/geometry/Region.cpp
index e755a8b11680dd3f5ecb5ff72de35b373958d9a2..813c1edc2baf6cc95222973128c5d9b06882f5e0 100644
--- a/Source/platform/geometry/Region.cpp
+++ b/Source/platform/geometry/Region.cpp
@@ -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,19 @@ Region::Shape::Shape(const IntRect& rect)
appendSpan(rect.maxY());
}
+Region::Shape::Shape(size_t segmentsCapacity, size_t spansCapacity)
+{
+ if (segmentsCapacity > m_segments.capacity()) {
+ segmentsCapacity = (segmentsCapacity + segmentsMask) & ~segmentsMask;
+ m_segments.reserveCapacity(segmentsCapacity);
+ }
+
+ if (spansCapacity > m_spans.capacity()) {
+ spansCapacity = (spansCapacity + spansMask) & ~spansMask;
+ m_spans.reserveCapacity(spansCapacity);
+ }
+}
+
void Region::Shape::appendSpan(int y)
{
m_spans.append(Span(y, m_segments.size()));
@@ -393,7 +412,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 +430,9 @@ Region::Shape Region::Shape::shapeOperation(const Shape& shape1, const Shape& sh
SegmentIterator segments2 = 0;
SegmentIterator segments2End = 0;
+ Vector<int, segmentsDefaultCapacity> segments;
+ segments.reserveCapacity(std::max(shape1.segmentsSize(), shape2.segmentsSize()));
+
// Iterate over all spans.
while (spans1 != spans1End && spans2 != spans2End) {
int y = 0;
@@ -435,7 +459,8 @@ 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);
danakj 2014/05/12 22:57:09 Can you assert the capacity after this verify it k
ostap 2014/05/13 19:05:21 Assert that capacity didn't grow and there was now
danakj 2014/05/13 19:09:03 I meant assert that the capacity was not shrunken
ostap 2014/05/13 20:07:48 Done.
// Now iterate over the segments in each span and construct a new vector of segments.
while (s1 != segments1End && s2 != segments2End) {
@@ -476,6 +501,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;
}
« no previous file with comments | « Source/platform/geometry/Region.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698