From ce8331a4bd80fa759f9057cde28c36c4bff5a2eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= Date: Sat, 17 Dec 2022 09:43:09 +0000 Subject: [PATCH] Remove allocations in NGInlineLayoutAlgorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Auto-Submit: Peter Boström Commit-Queue: Koji Ishii Cr-Commit-Position: refs/heads/main@{#1084692} --- .../layout/ng/inline/ng_inline_layout_algorithm.cc | 13 ++++++++----- .../layout/ng/inline/ng_inline_layout_algorithm.h | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.cc b/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.cc index 1988631d7eff1..8cd424be33d9b 100644 --- a/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.cc +++ b/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.cc @@ -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()) { DCHECK(context); } @@ -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(); + 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) { @@ -197,7 +199,8 @@ void NGInlineLayoutAlgorithm::CheckBoxStates( const NGInlineBreakToken* break_token) const { NGInlineLayoutStateStack rebuilt; RebuildBoxStates(line_info, break_token, &rebuilt); - NGLogicalLineItems& line_box = *MakeGarbageCollected(); + 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_); @@ -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(); + 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])); diff --git a/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.h b/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.h index 625643a9872e1..0655d6c2af928 100644 --- a/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.h +++ b/third_party/blink/renderer/core/layout/ng/inline/ng_inline_layout_algorithm.h @@ -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.