mirror of
https://github.com/google/googletest.git
synced 2026-06-26 20:38:56 +08:00
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
This commit is contained in:
parent
73e0fc18eb
commit
a407966592
@ -2821,7 +2821,6 @@ class [[nodiscard]] QuantifierMatcherImpl : public MatcherInterface<Container> {
|
||||
StlContainerReference stl_container = View::ConstReference(container);
|
||||
size_t i = 0;
|
||||
std::vector<size_t> 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<Inner...> matchers_;
|
||||
};
|
||||
|
||||
// Returns the distance between [first, last) if known, otherwise 0.
|
||||
template <typename Iter>
|
||||
size_t DistanceIfKnown(Iter first, Iter last) {
|
||||
if constexpr (std::is_base_of_v<
|
||||
std::forward_iterator_tag,
|
||||
typename std::iterator_traits<Iter>::iterator_category>) {
|
||||
return std::distance(first, last);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Implements ElementsAre() and ElementsAreArray().
|
||||
template <typename Container>
|
||||
class [[nodiscard]] ElementsAreMatcherImpl
|
||||
@ -3619,7 +3606,6 @@ class [[nodiscard]] ElementsAreMatcherImpl
|
||||
// element matchers.
|
||||
template <typename InputIter>
|
||||
ElementsAreMatcherImpl(InputIter first, InputIter last) {
|
||||
matchers_.reserve(DistanceIfKnown(first, last));
|
||||
while (first != last) {
|
||||
matchers_.push_back(MatcherCast<const Element&>(*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<const Element&>(*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<std::string>* element_printouts,
|
||||
MatchResultListener* listener) const {
|
||||
element_printouts->clear();
|
||||
element_printouts->reserve(DistanceIfKnown(elem_first, elem_last));
|
||||
::std::vector<char> 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<internal::BoundSecondMatcher<Tuple2Matcher, Second>> 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));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user