From a407966592e8aab5c7b0757867a2f0585175fe80 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 23 Feb 2026 11:30:09 -0800 Subject: [PATCH] Revert Optimize copying of matchers vector to speed up `ElementsAreArray` by reserving a `std::vector` copied via `push_back`. If the iterators are forward iterators, we can measure the size and reserve the vector, avoiding reallocations and copying. PiperOrigin-RevId: 874172640 Change-Id: Ie081fdf7952858d7e41ddda63a3ae15d9867c8c5 --- googlemock/include/gmock/gmock-matchers.h | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 9f5118e8c..2532d597f 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -2821,7 +2821,6 @@ class [[nodiscard]] QuantifierMatcherImpl : public MatcherInterface { StlContainerReference stl_container = View::ConstReference(container); size_t i = 0; std::vector match_elements; - match_elements.reserve(stl_container.size()); for (auto it = stl_container.begin(); it != stl_container.end(); ++it, ++i) { StringMatchResultListener inner_listener; @@ -3592,18 +3591,6 @@ class [[nodiscard]] FieldsAreMatcher { std::tuple matchers_; }; -// Returns the distance between [first, last) if known, otherwise 0. -template -size_t DistanceIfKnown(Iter first, Iter last) { - if constexpr (std::is_base_of_v< - std::forward_iterator_tag, - typename std::iterator_traits::iterator_category>) { - return std::distance(first, last); - } else { - return 0; - } -} - // Implements ElementsAre() and ElementsAreArray(). template class [[nodiscard]] ElementsAreMatcherImpl @@ -3619,7 +3606,6 @@ class [[nodiscard]] ElementsAreMatcherImpl // element matchers. template ElementsAreMatcherImpl(InputIter first, InputIter last) { - matchers_.reserve(DistanceIfKnown(first, last)); while (first != last) { matchers_.push_back(MatcherCast(*first++)); } @@ -3895,11 +3881,9 @@ class [[nodiscard]] UnorderedElementsAreMatcherImpl UnorderedElementsAreMatcherImpl(UnorderedMatcherRequire::Flags matcher_flags, InputIter first, InputIter last) : UnorderedElementsAreMatcherImplBase(matcher_flags) { - matchers_.reserve(DistanceIfKnown(first, last)); for (; first != last; ++first) { matchers_.push_back(MatcherCast(*first)); } - matcher_describers().reserve(matchers_.size()); for (const auto& m : matchers_) { matcher_describers().push_back(m.GetDescriber()); } @@ -3933,7 +3917,6 @@ class [[nodiscard]] UnorderedElementsAreMatcherImpl ::std::vector* element_printouts, MatchResultListener* listener) const { element_printouts->clear(); - element_printouts->reserve(DistanceIfKnown(elem_first, elem_last)); ::std::vector did_match; size_t num_elements = 0; DummyMatchResultListener dummy; @@ -5104,7 +5087,6 @@ UnorderedPointwise(const Tuple2Matcher& tuple2_matcher, // Create a matcher for each element in rhs_container. ::std::vector> matchers; - matchers.reserve(rhs_stl_container.size()); for (auto it = rhs_stl_container.begin(); it != rhs_stl_container.end(); ++it) { matchers.push_back(internal::MatcherBindSecond(tuple2_matcher, *it));