public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-2995] libstdc++: Minor optimization for min/max/minmax Date: Wed, 18 Aug 2021 14:36:14 +0000 (GMT) [thread overview] Message-ID: <20210818143614.6BEA23986426@sourceware.org> (raw) https://gcc.gnu.org/g:de44eee5d52f5980b6b2b2120940f70cc2fa007e commit r12-2995-gde44eee5d52f5980b6b2b2120940f70cc2fa007e Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Aug 18 12:24:12 2021 +0100 libstdc++: Minor optimization for min/max/minmax The debug mode checks for a valid range are redundant when we have an initializer_list argument, because we know it's a valid range already. By making std::min(initialier_list<T>) call the internal __min_element function directly we avoid a function call and skip those checks. The same can be done for the overload taking a comparison function, and also for the std::max and std::minmax overloads for initializer_list arguments. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: * include/bits/stl_algo.h (min(initializer_list<T>)) (min(initializer_list<T>, Compare)): Call __min_element directly to avoid redundant debug checks for valid ranges. (max(initializer_list<T>), max(initializer_list<T>, Compare)): Likewise, for __max_element. (minmax(initializer_list<T>), minmax(initializer_list<T>, Compare)): Likewise, for __minmax_element. Diff: --- libstdc++-v3/include/bits/stl_algo.h | 78 +++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 54ad383711f..ac4f2d0f721 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -3445,38 +3445,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __gnu_cxx::__ops::__iter_comp_iter(__comp)); } - // N2722 + DR 915. - template<typename _Tp> - _GLIBCXX14_CONSTEXPR - inline _Tp - min(initializer_list<_Tp> __l) - { return *std::min_element(__l.begin(), __l.end()); } - - template<typename _Tp, typename _Compare> - _GLIBCXX14_CONSTEXPR - inline _Tp - min(initializer_list<_Tp> __l, _Compare __comp) - { return *std::min_element(__l.begin(), __l.end(), __comp); } - - template<typename _Tp> - _GLIBCXX14_CONSTEXPR - inline _Tp - max(initializer_list<_Tp> __l) - { return *std::max_element(__l.begin(), __l.end()); } - - template<typename _Tp, typename _Compare> - _GLIBCXX14_CONSTEXPR - inline _Tp - max(initializer_list<_Tp> __l, _Compare __comp) - { return *std::max_element(__l.begin(), __l.end(), __comp); } - template<typename _Tp> _GLIBCXX14_CONSTEXPR inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> __l) { + __glibcxx_requires_irreflexive(__l.begin(), __l.end()); pair<const _Tp*, const _Tp*> __p = - std::minmax_element(__l.begin(), __l.end()); + std::__minmax_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); return std::make_pair(*__p.first, *__p.second); } @@ -3485,8 +3462,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> __l, _Compare __comp) { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp); pair<const _Tp*, const _Tp*> __p = - std::minmax_element(__l.begin(), __l.end(), __comp); + std::__minmax_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); return std::make_pair(*__p.first, *__p.second); } @@ -3793,7 +3772,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); } -#endif +#endif // USE C99_STDINT #endif // C++11 @@ -5746,6 +5725,49 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO __gnu_cxx::__ops::__iter_comp_iter(__comp)); } +#if __cplusplus >= 201103L + // N2722 + DR 915. + template<typename _Tp> + _GLIBCXX14_CONSTEXPR + inline _Tp + min(initializer_list<_Tp> __l) + { + __glibcxx_requires_irreflexive(__l.begin(), __l.end()); + return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); + } + + template<typename _Tp, typename _Compare> + _GLIBCXX14_CONSTEXPR + inline _Tp + min(initializer_list<_Tp> __l, _Compare __comp) + { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp); + return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); + } + + template<typename _Tp> + _GLIBCXX14_CONSTEXPR + inline _Tp + max(initializer_list<_Tp> __l) + { + __glibcxx_requires_irreflexive(__l.begin(), __l.end()); + return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); + } + + template<typename _Tp, typename _Compare> + _GLIBCXX14_CONSTEXPR + inline _Tp + max(initializer_list<_Tp> __l, _Compare __comp) + { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp); + return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); + } +#endif // C++11 + #if __cplusplus >= 201402L /// Reservoir sampling algorithm. template<typename _InputIterator, typename _RandomAccessIterator,
reply other threads:[~2021-08-18 14:36 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210818143614.6BEA23986426@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).