Shrink-to-fit Display::LineBox/Display::Run vectors.
https://bugs.webkit.org/show_bug.cgi?id=208343

Reviewed by Antti Koivisto.

From the collected data in PLT5, 95% of Runs / LineBoxes are <= 4, while we are having 10 and 5 inlineCapacity.
We adjust this inlineCapacity to 4 based on this number. It also covers almost all of Speedometer2.0 content (in it, 99.9% is 1).
We also call `shrinkToFit` to make it the exact size after baking Display::InlineContent. It should be no-op in 95% cases.

* layout/displaytree/DisplayInlineContent.h:
* layout/inlineformatting/InlineFormattingState.h:
(WebCore::Layout::InlineFormattingState::shrinkDisplayInlineContent):
* layout/integration/LayoutIntegrationLineLayout.cpp:
(WebCore::LayoutIntegration::LineLayout::layout):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@257637 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index b5a1a14..1ba89ac 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,20 @@
+2020-02-28  Yusuke Suzuki  <ysuzuki@apple.com>
+
+        Shrink-to-fit Display::LineBox/Display::Run vectors.
+        https://bugs.webkit.org/show_bug.cgi?id=208343
+
+        Reviewed by Antti Koivisto.
+
+        From the collected data in PLT5, 95% of Runs / LineBoxes are <= 4, while we are having 10 and 5 inlineCapacity.
+        We adjust this inlineCapacity to 4 based on this number. It also covers almost all of Speedometer2.0 content (in it, 99.9% is 1).
+        We also call `shrinkToFit` to make it the exact size after baking Display::InlineContent. It should be no-op in 95% cases.
+
+        * layout/displaytree/DisplayInlineContent.h:
+        * layout/inlineformatting/InlineFormattingState.h:
+        (WebCore::Layout::InlineFormattingState::shrinkDisplayInlineContent):
+        * layout/integration/LayoutIntegrationLineLayout.cpp:
+        (WebCore::LayoutIntegration::LineLayout::layout):
+
 2020-02-28  Alberto Garcia  <berto@igalia.com>
 
         [GStreamer] Unreviewed. Fix build warning.
diff --git a/Source/WebCore/layout/displaytree/DisplayInlineContent.h b/Source/WebCore/layout/displaytree/DisplayInlineContent.h
index d7dddb3..7034438 100644
--- a/Source/WebCore/layout/displaytree/DisplayInlineContent.h
+++ b/Source/WebCore/layout/displaytree/DisplayInlineContent.h
@@ -38,8 +38,8 @@
 struct InlineContent : public RefCounted<InlineContent> {
     ~InlineContent();
 
-    using Runs = Vector<Run, 10>;
-    using LineBoxes = Vector<LineBox, 5>;
+    using Runs = Vector<Run, 4>;
+    using LineBoxes = Vector<LineBox, 4>;
 
     Runs runs;
     LineBoxes lineBoxes;
diff --git a/Source/WebCore/layout/inlineformatting/InlineFormattingState.h b/Source/WebCore/layout/inlineformatting/InlineFormattingState.h
index f7bdb3b..6879d41 100644
--- a/Source/WebCore/layout/inlineformatting/InlineFormattingState.h
+++ b/Source/WebCore/layout/inlineformatting/InlineFormattingState.h
@@ -50,7 +50,9 @@
 
     const Display::InlineContent* displayInlineContent() const { return m_displayInlineContent.get(); }
     Display::InlineContent& ensureDisplayInlineContent();
+
     void clearDisplayInlineContent() { m_displayInlineContent = nullptr; }
+    void shrinkDisplayInlineContent();
 
 private:
     // Cacheable input to line layout.
@@ -66,6 +68,14 @@
     return *m_displayInlineContent;
 }
 
+inline void InlineFormattingState::shrinkDisplayInlineContent()
+{
+    if (!m_displayInlineContent)
+        return;
+    m_displayInlineContent->runs.shrinkToFit();
+    m_displayInlineContent->lineBoxes.shrinkToFit();
+}
+
 }
 }
 
diff --git a/Source/WebCore/layout/integration/LayoutIntegrationLineLayout.cpp b/Source/WebCore/layout/integration/LayoutIntegrationLineLayout.cpp
index e4be04a..e8db378 100644
--- a/Source/WebCore/layout/integration/LayoutIntegrationLineLayout.cpp
+++ b/Source/WebCore/layout/integration/LayoutIntegrationLineLayout.cpp
@@ -111,6 +111,7 @@
     auto verticalConstraints = Layout::VerticalConstraints { m_flow.borderAndPaddingBefore(), { } };
 
     inlineFormattingContext.layoutInFlowContent(invalidationState, horizontalConstraints, verticalConstraints);
+    m_inlineFormattingState.shrinkDisplayInlineContent();
 }
 
 void LineLayout::prepareLayoutState()