Skip to content

Commit

Permalink
Remove allocations in NGInlineLayoutAlgorithm
Browse files Browse the repository at this point in the history
This creates a single `cached_line_items_` which is used instead of
MakeGarbageCollected in NGInlineLayoutAlgorithm.

Hopefully this should improve performance on DCHECK builds where
::CheckBoxStates() is called very frequently, but it may be very
marginal as we haven't profiled this.

Bug: 1380588
Change-Id: I4ced6f630cc25f36504cb45396e2dd7b521d61ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4113878
Reviewed-by: Koji Ishii <kojii@chromium.org>
Auto-Submit: Peter Boström <pbos@chromium.org>
Commit-Queue: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1084692}
  • Loading branch information
pbos authored and Chromium LUCI CQ committed Dec 17, 2022
1 parent 6e61ffa commit ce8331a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Expand Up @@ -66,7 +66,8 @@ NGInlineLayoutAlgorithm::NGInlineLayoutAlgorithm(
context_(context),
column_spanner_path_(column_spanner_path),
baseline_type_(container_builder_.Style().GetFontBaseline()),
quirks_mode_(inline_node.GetDocument().InLineHeightQuirksMode()) {
quirks_mode_(inline_node.GetDocument().InLineHeightQuirksMode()),
cached_line_items_(MakeGarbageCollected<NGLogicalLineItems>()) {
DCHECK(context);
}

Expand Down Expand Up @@ -179,7 +180,8 @@ void NGInlineLayoutAlgorithm::RebuildBoxStates(
line_info.ItemsData().GetOpenTagItems(break_token->ItemIndex(), &open_items);

// Create box states for tags that are not closed yet.
NGLogicalLineItems& line_box = *MakeGarbageCollected<NGLogicalLineItems>();
DCHECK_EQ(cached_line_items_->size(), 0u);
NGLogicalLineItems& line_box = *cached_line_items_;
box_states->OnBeginPlaceItems(Node(), line_info.LineStyle(), baseline_type_,
quirks_mode_, &line_box);
for (const NGInlineItem* item : open_items) {
Expand All @@ -197,7 +199,8 @@ void NGInlineLayoutAlgorithm::CheckBoxStates(
const NGInlineBreakToken* break_token) const {
NGInlineLayoutStateStack rebuilt;
RebuildBoxStates(line_info, break_token, &rebuilt);
NGLogicalLineItems& line_box = *MakeGarbageCollected<NGLogicalLineItems>();
DCHECK_EQ(cached_line_items_->size(), 0u);
NGLogicalLineItems& line_box = *cached_line_items_;
rebuilt.OnBeginPlaceItems(Node(), line_info.LineStyle(), baseline_type_,
quirks_mode_, &line_box);
DCHECK(box_states_);
Expand Down Expand Up @@ -1632,8 +1635,8 @@ void NGInlineLayoutAlgorithm::BidiReorder(TextDirection base_direction,
NGBidiParagraph::IndicesInVisualOrder(levels, &indices_in_visual_order);

// Reorder to the visual order.
NGLogicalLineItems& visual_items =
*MakeGarbageCollected<NGLogicalLineItems>();
DCHECK_EQ(cached_line_items_->size(), 0u);
NGLogicalLineItems& visual_items = *cached_line_items_;
visual_items.ReserveInitialCapacity(line_box->size());
for (unsigned logical_index : indices_in_visual_order)
visual_items.AddChild(std::move((*line_box)[logical_index]));
Expand Down
Expand Up @@ -153,6 +153,8 @@ class CORE_EXPORT NGInlineLayoutAlgorithm final
// https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk
unsigned quirks_mode_ : 1;

NGLogicalLineItems* const cached_line_items_;

#if DCHECK_IS_ON()
// True if |box_states_| is taken from |context_|, to check the |box_states_|
// is the same as when it is rebuilt.
Expand Down

0 comments on commit ce8331a

Please sign in to comment.