diff --git a/dom/animation/AnimationTimeline.cpp b/dom/animation/AnimationTimeline.cpp index 9870dfac48c8..6397b11b4d04 100644 --- a/dom/animation/AnimationTimeline.cpp +++ b/dom/animation/AnimationTimeline.cpp @@ -40,17 +40,18 @@ AnimationTimeline::~AnimationTimeline() { mAnimationOrder.clear(); } bool AnimationTimeline::Tick(TickState& aState) { bool needsTicks = false; - AutoTArray, 32> animationsToTick; +#ifdef DEBUG for (Animation* animation : mAnimationOrder) { MOZ_ASSERT(mAnimations.Contains(animation), "The sampling order list should be a subset of the hashset"); MOZ_ASSERT(!animation->IsHiddenByContentVisibility(), "The sampling order list should not contain any animations " "that are hidden by content-visibility"); - animationsToTick.AppendElement(animation); } +#endif - for (Animation* animation : animationsToTick) { + for (Animation* animation : + ToTArray, 32>>(mAnimationOrder)) { // Skip any animations that are longer need associated with this timeline. if (animation->GetTimeline() != this) { RemoveAnimation(animation); diff --git a/dom/animation/DocumentTimeline.cpp b/dom/animation/DocumentTimeline.cpp index 34b8fa165013..14e86cb9d105 100644 --- a/dom/animation/DocumentTimeline.cpp +++ b/dom/animation/DocumentTimeline.cpp @@ -160,12 +160,8 @@ void DocumentTimeline::NotifyAnimationUpdated(Animation& aAnimation) { } void DocumentTimeline::TriggerAllPendingAnimationsNow() { - AutoTArray, 32> animationsToTrigger; - for (Animation* animation : mAnimationOrder) { - animationsToTrigger.AppendElement(animation); - } - - for (Animation* animation : animationsToTrigger) { + for (Animation* animation : + ToTArray, 32>>(mAnimationOrder)) { animation->TryTriggerNow(); } } diff --git a/layout/base/nsRefreshDriver.cpp b/layout/base/nsRefreshDriver.cpp index d8df566f7092..e40312f5b0de 100644 --- a/layout/base/nsRefreshDriver.cpp +++ b/layout/base/nsRefreshDriver.cpp @@ -2311,15 +2311,10 @@ void nsRefreshDriver::DetermineProximityToViewportAndNotifyResizeObservers() { } static CallState UpdateAndReduceAnimations(Document& aDocument) { - { - AutoTArray, 32> timelinesToTick; - for (DocumentTimeline* timeline : aDocument.Timelines()) { - timelinesToTick.AppendElement(timeline); - } - - for (DocumentTimeline* tl : timelinesToTick) { - tl->WillRefresh(); - } + for (DocumentTimeline* tl : + ToTArray, 32>>( + aDocument.Timelines())) { + tl->WillRefresh(); } if (nsPresContext* pc = aDocument.GetPresContext()) { diff --git a/mfbt/LinkedList.h b/mfbt/LinkedList.h index 850b8594c751..15e53e80c1d4 100644 --- a/mfbt/LinkedList.h +++ b/mfbt/LinkedList.h @@ -409,13 +409,13 @@ class LinkedListElement { template class LinkedList { private: - typedef typename detail::LinkedListElementTraits Traits; - typedef typename Traits::RawType RawType; - typedef typename Traits::ConstRawType ConstRawType; - typedef typename Traits::ClientType ClientType; - typedef typename Traits::ConstClientType ConstClientType; - typedef LinkedListElement* ElementType; - typedef const LinkedListElement* ConstElementType; + using Traits = typename detail::LinkedListElementTraits; + using RawType = typename Traits::RawType; + using ConstRawType = typename Traits::ConstRawType; + using ClientType = typename Traits::ClientType; + using ConstClientType = typename Traits::ConstClientType; + using ElementType = LinkedListElement*; + using ConstElementType = const LinkedListElement*; LinkedListElement sentinel; @@ -445,6 +445,9 @@ class LinkedList { } }; + using const_iterator = Iterator; + using iterator = Iterator; + LinkedList() : sentinel(LinkedListElement::NodeKind::Sentinel) {} LinkedList(LinkedList&& aOther) : sentinel(std::move(aOther.sentinel)) {} @@ -701,6 +704,11 @@ class LinkedList { LinkedList(const LinkedList& aOther) = delete; }; +template +size_t RangeSizeEstimate(const LinkedList&) { + return 0; +} + template inline void ImplCycleCollectionUnlink(LinkedList>& aField) { aField.clear(); diff --git a/toolkit/components/extensions/MatchPattern.cpp b/toolkit/components/extensions/MatchPattern.cpp index 6a627650d675..2c028dd624aa 100644 --- a/toolkit/components/extensions/MatchPattern.cpp +++ b/toolkit/components/extensions/MatchPattern.cpp @@ -30,7 +30,9 @@ template static AtomSet::ArrayType AtomSetFromRange(Range&& aRange, AsAtom&& aTransform) { AtomSet::ArrayType atoms; - atoms.SetCapacity(RangeSize(aRange)); + if (auto estimate = RangeSizeEstimate(aRange)) { + atoms.SetCapacity(estimate); + } std::transform(aRange.begin(), aRange.end(), MakeBackInserter(atoms), std::forward(aTransform)); diff --git a/xpcom/ds/nsBaseHashtable.h b/xpcom/ds/nsBaseHashtable.h index 7b57ef46660f..f64fea757e25 100644 --- a/xpcom/ds/nsBaseHashtable.h +++ b/xpcom/ds/nsBaseHashtable.h @@ -175,7 +175,8 @@ class nsBaseHashtableValueRange { }; template -auto RangeSize(const detail::nsBaseHashtableValueRange& aRange) { +size_t RangeSizeEstimate( + const detail::nsBaseHashtableValueRange& aRange) { return aRange.Count(); } diff --git a/xpcom/ds/nsTArray.h b/xpcom/ds/nsTArray.h index 1689051993e4..055edf0ebbd6 100644 --- a/xpcom/ds/nsTArray.h +++ b/xpcom/ds/nsTArray.h @@ -3274,11 +3274,14 @@ class nsTArrayView { const Span mSpan; }; -template ::iterator_category, - std::random_access_iterator_tag>>> -auto RangeSize(const Range& aRange) { +// NOTE(emilio): If changing the name of this or so, make sure to change +// specializations too. +template ::iterator>::iterator_category, + std::random_access_iterator_tag>>> +size_t RangeSizeEstimate(const Range& aRange) { // See https://en.cppreference.com/w/cpp/iterator/begin, section 'User-defined // overloads'. using std::begin; @@ -3293,12 +3296,14 @@ auto RangeSize(const Range& aRange) { * convertible from the range's value type. */ template -auto ToTArray(const Range& aRange) { +auto ToTArray(Range&& aRange) { using std::begin; using std::end; Array res; - res.SetCapacity(RangeSize(aRange)); + if (auto estimate = RangeSizeEstimate(aRange)) { + res.SetCapacity(estimate); + } std::copy(begin(aRange), end(aRange), MakeBackInserter(res)); return res; } @@ -3307,21 +3312,22 @@ auto ToTArray(const Range& aRange) { * Materialize a range as a nsTArray of its (decayed) value type. */ template -auto ToArray(const Range& aRange) { - return ToTArray::value_type>>>( - aRange); +auto ToArray(Range&& aRange) { + return ToTArray::iterator>::value_type>>>( + std::forward(aRange)); } /** * Appends all elements from a range to an array. */ template -void AppendToArray(Array& aArray, const Range& aRange) { +void AppendToArray(Array& aArray, Range&& aRange) { using std::begin; using std::end; - - aArray.SetCapacity(aArray.Length() + RangeSize(aRange)); + if (auto estimate = RangeSizeEstimate(aRange)) { + aArray.SetCapacity(aArray.Length() + estimate); + } std::copy(begin(aRange), end(aRange), MakeBackInserter(aArray)); } diff --git a/xpcom/ds/nsTHashSet.h b/xpcom/ds/nsTHashSet.h index 4d905299dbfa..89404221c0f0 100644 --- a/xpcom/ds/nsTHashSet.h +++ b/xpcom/ds/nsTHashSet.h @@ -134,7 +134,7 @@ class nsTBaseHashSet : protected nsTHashtable { }; template -auto RangeSize(const nsTBaseHashSet& aRange) { +size_t RangeSizeEstimate(const nsTBaseHashSet& aRange) { return aRange.Count(); } diff --git a/xpcom/ds/nsTHashtable.h b/xpcom/ds/nsTHashtable.h index b991261de495..1dc4320c167f 100644 --- a/xpcom/ds/nsTHashtable.h +++ b/xpcom/ds/nsTHashtable.h @@ -168,7 +168,8 @@ class nsTHashtableKeyRange { }; template -auto RangeSize(const ::detail::nsTHashtableKeyRange& aRange) { +size_t RangeSizeEstimate( + const ::detail::nsTHashtableKeyRange& aRange) { return aRange.Count(); }