Commit f6978cc5 authored by Christian Biesinger's avatar Christian Biesinger Committed by Commit Bot

[layoutng] Prereserve vector size for the children/offset vectors

Cq-Include-Trybots: luci.chromium.try:linux_layout_tests_layout_ng
Change-Id: I9eefb234301b9bc8a9c4912d032eaf83a0b80d93
Reviewed-on: https://chromium-review.googlesource.com/c/1253983
Commit-Queue: Christian Biesinger <cbiesinger@chromium.org>
Reviewed-by: default avatarMorten Stenshorne <mstensho@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596445}
parent abbfa845
......@@ -141,6 +141,10 @@ scoped_refptr<NGLayoutResult> NGLineBoxFragmentBuilder::ToLineBoxFragment() {
child->PropagateContentsInkOverflow(&contents_ink_overflow, child.Offset());
}
// Because this vector will be long-lived, make sure to not waaste space.
// (We reserve an initial capacity when adding the first child)
if (children_.size())
children_.ShrinkToReasonableCapacity();
scoped_refptr<const NGPhysicalLineBoxFragment> fragment =
base::AdoptRef(new NGPhysicalLineBoxFragment(
Style(), style_variant_, physical_size, children_,
......
......@@ -104,6 +104,10 @@ NGContainerFragmentBuilder& NGContainerFragmentBuilder::AddChild(
has_last_resort_break_ = true;
}
}
// Assume that if we have one child, we may have more than one and try to
// limit the number of allocations we would do in that case.
if (!children_.capacity())
children_.ReserveCapacity(16);
children_.emplace_back(std::move(child), NGPhysicalOffset());
offsets_.push_back(child_offset);
return *this;
......
......@@ -31,6 +31,8 @@ class CORE_EXPORT NGContainerFragmentBuilder : public NGBaseFragmentBuilder {
STACK_ALLOCATED();
public:
typedef Vector<NGLogicalOffset, 16> OffsetVector;
~NGContainerFragmentBuilder() override;
LayoutUnit InlineSize() const { return size_.inline_size; }
......@@ -210,12 +212,17 @@ class CORE_EXPORT NGContainerFragmentBuilder : public NGBaseFragmentBuilder {
// Store NGLinks rather than NGPhysicalOffsets even though we don't have the
// offsets yet to allow us to move the entire vector to the fragment at
// construction time.
// Unlike OffsetVector, we don't want to keep the inline capacity around
// because this vector will be long-lived, and it would be wasteful to reserve
// space for 16 children in every fragment. Instead, we reserve some initial
// capacity for it when adding the first child and shrink it down when
// creating the fragment.
Vector<NGLink> children_;
// Logical offsets for the children. Stored as logical offsets as we can't
// convert to physical offsets until layout of all children has been
// determined.
Vector<NGLogicalOffset> offsets_;
OffsetVector offsets_;
NGFloatTypes adjoining_floats_ = kFloatTypeNone;
......
......@@ -310,6 +310,10 @@ scoped_refptr<NGLayoutResult> NGFragmentBuilder::ToBoxFragment(
}
}
// Because this vector will be long-lived, make sure to not waste space.
// (We reserve an initial capacity when adding the first child)
if (children_.size())
children_.ShrinkToReasonableCapacity();
scoped_refptr<const NGPhysicalBoxFragment> fragment =
base::AdoptRef(new NGPhysicalBoxFragment(
layout_object_, Style(), style_variant_, physical_size, children_,
......
......@@ -129,8 +129,8 @@ class CORE_EXPORT NGFragmentBuilder final : public NGContainerFragmentBuilder {
scoped_refptr<NGLayoutResult> Abort(NGLayoutResult::NGLayoutResultStatus);
// A vector of child offsets. Initially set by AddChild().
const Vector<NGLogicalOffset>& Offsets() const { return offsets_; }
Vector<NGLogicalOffset>& MutableOffsets() { return offsets_; }
const OffsetVector& Offsets() const { return offsets_; }
OffsetVector& MutableOffsets() { return offsets_; }
NGPhysicalFragment::NGBoxType BoxType() const;
NGFragmentBuilder& SetBoxType(NGPhysicalFragment::NGBoxType);
......
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