Commit 4b9875ae authored by Koji Ishii's avatar Koji Ishii Committed by Commit Bot

[LayoutNG] Reduce memory allocations in NGInlineNode::ShapeText

32 can avoid most of memory allocations, by consuming 512
bytes on stack ((4+4+8) * 32) on 64 bits.

Most major sites are up to 8 or so, such as Google SRP,
facebook, twitter, MDN, youtube, etc. Wikipedia is up to 21.

Change-Id: I7f06f0ce01b5d2960becf99e0b453d92723e5253
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1821501
Auto-Submit: Koji Ishii <kojii@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Emil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699655}
parent f77a12bd
......@@ -1158,9 +1158,9 @@ void NGInlineNode::ShapeText(NGInlineItemsData* data,
// If the text is from multiple items, split the ShapeResult to
// corresponding items.
DCHECK_GT(num_text_items, 0u);
std::unique_ptr<ShapeResult::ShapeRange[]> text_item_ranges =
std::make_unique<ShapeResult::ShapeRange[]>(num_text_items);
unsigned range_index = 0;
// "32" is heuristic, most major sites are up to 8 or so, wikipedia is 21.
Vector<ShapeResult::ShapeRange, 32> text_item_ranges;
text_item_ranges.ReserveInitialCapacity(num_text_items);
for (; index < end_index; index++) {
NGInlineItem& item = (*items)[index];
if (item.Type() != NGInlineItem::kText || !item.Length())
......@@ -1174,12 +1174,12 @@ void NGInlineNode::ShapeText(NGInlineItemsData* data,
// item that has its first code unit keeps the glyph.
scoped_refptr<ShapeResult> item_result =
ShapeResult::CreateEmpty(*shape_result.get());
item.shape_result_ = item_result;
text_item_ranges[range_index++] = {item.StartOffset(), item.EndOffset(),
item_result.get()};
text_item_ranges.emplace_back(item.StartOffset(), item.EndOffset(),
item_result.get());
item.shape_result_ = std::move(item_result);
}
DCHECK_EQ(range_index, num_text_items);
shape_result->CopyRanges(&text_item_ranges[0], num_text_items);
DCHECK_EQ(text_item_ranges.size(), num_text_items);
shape_result->CopyRanges(text_item_ranges.data(), text_item_ranges.size());
}
#if DCHECK_IS_ON()
......
......@@ -263,8 +263,9 @@ class PLATFORM_EXPORT ShapeResult : public RefCounted<ShapeResult> {
void CopyRange(unsigned start, unsigned end, ShapeResult*) const;
struct ShapeRange {
// ShapeRange(unsigned start, unsigned end, ShapeResult* target)
// : start(start), end(end), target(target){};
ShapeRange(unsigned start, unsigned end, ShapeResult* target)
: start(start), end(end), target(target) {}
unsigned start;
unsigned end;
ShapeResult* target;
......
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