From f463e6bd8e84f12bde60f3e24e2d30da7fcda419 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 11 Sep 2025 09:34:02 +0100 Subject: libstdc++: Add always_inline to ranges iterator ops and access functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most of the basis operations for ranges such as ranges::begin and ranges::next are trivial one-line function bodies, so can be made always_inline to reduce the abstraction penalty for -O0 code. Now that we no longer need to support the -fconcepts-ts grammar, we can also move some [[nodiscard]] attributes to the more natural position before the function declaration, instead of between the declarator-id and the function parameters, e.g. we can use: template requires C [[nodiscard]] auto operator()(T&&) instead of: template requires C auto operator() [[nodiscard]] (T&&) The latter form was necessary because -fconcepts-ts used a different grammar for the requires-clause, parsing 'C[[x]]' as a subscripting operator with an ill-formed argument '[x]'. In the C++20 grammar you would need to use parentheses to use a subscript in a constraint, so without parentheses it's parsed as an attribute. libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (__detail::__to_unsigned_like) (__access::__possible_const_range, __access::__as_const) (__distance_fn::operator(), __next_fn::operator()) (__prev_fn::operator()): Add always_inline attribute. (_Begin::operator(), _End::operator(), _RBegin::operator()) (_REnd::operator(), _Size::operator(), _SSize::operator()) (_Empty::operator(), _Data::operator(), _SSize::operator()): Likewise. Move nodiscard attribute to start of declaration. Reviewed-by: Tomasz KamiƄski --- libstdc++-v3/include/bits/ranges_base.h | 47 +++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 7df638d..691a181 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -68,15 +68,18 @@ namespace ranges namespace __detail { + [[__gnu__::__always_inline__]] constexpr __max_size_type __to_unsigned_like(__max_size_type __t) noexcept { return __t; } + [[__gnu__::__always_inline__]] constexpr __max_size_type __to_unsigned_like(__max_diff_type __t) noexcept { return __max_size_type(__t); } template + [[__gnu__::__always_inline__]] constexpr auto __to_unsigned_like(_Tp __t) noexcept { return static_cast>(__t); } @@ -118,8 +121,9 @@ namespace ranges template<__maybe_borrowed_range _Tp> requires is_array_v> || __member_begin<_Tp> || __adl_begin<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_array_v>) { @@ -168,8 +172,9 @@ namespace ranges template<__maybe_borrowed_range _Tp> requires is_bounded_array_v> || __member_end<_Tp> || __adl_end<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_bounded_array_v>) { @@ -232,8 +237,9 @@ namespace ranges public: template<__maybe_borrowed_range _Tp> requires __member_rbegin<_Tp> || __adl_rbegin<_Tp> || __reversable<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_rbegin<_Tp>) @@ -289,8 +295,9 @@ namespace ranges public: template<__maybe_borrowed_range _Tp> requires __member_rend<_Tp> || __adl_rend<_Tp> || __reversable<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_rend<_Tp>) @@ -353,8 +360,9 @@ namespace ranges template requires is_bounded_array_v> || __member_size<_Tp> || __adl_size<_Tp> || __sentinel_size<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_bounded_array_v>) return extent_v>; @@ -373,8 +381,9 @@ namespace ranges // 3403. Domain of ranges::ssize(E) doesn't match ranges::size(E) template requires requires (_Tp& __t) { _Size{}(__t); } + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(noexcept(_Size{}(__t))) + operator()(_Tp&& __t) const noexcept(noexcept(_Size{}(__t))) { auto __size = _Size{}(__t); using __size_type = decltype(__size); @@ -429,8 +438,9 @@ namespace ranges template requires __member_empty<_Tp> || __size0_empty<_Tp> || __eq_iter_empty<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr bool - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_empty<_Tp>) return bool(__t.empty()); @@ -470,8 +480,9 @@ namespace ranges public: template<__maybe_borrowed_range _Tp> requires __member_data<_Tp> || __begin_data<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp>()) { if constexpr (__member_data<_Tp>) return __t.data(); @@ -632,6 +643,7 @@ namespace ranges { #if __glibcxx_ranges_as_const // >= C++23 template + [[__gnu__::__always_inline__]] constexpr auto& __possibly_const_range(_Range& __r) noexcept { @@ -645,6 +657,7 @@ namespace ranges #else // If _To is an lvalue-reference, return const _Tp&, otherwise const _Tp&&. template + [[__gnu__::__always_inline__]] constexpr decltype(auto) __as_const(_Tp& __t) noexcept { @@ -967,13 +980,13 @@ namespace ranges } template> _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr iter_difference_t> operator()(_It&& __first, _Sent __last) const { return __last - static_cast&>(__first); } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr range_difference_t<_Range> operator()(_Range&& __r) const { @@ -991,7 +1004,7 @@ namespace ranges struct __next_fn final { template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x) const { @@ -1000,7 +1013,7 @@ namespace ranges } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n) const { @@ -1009,7 +1022,7 @@ namespace ranges } template _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, _Sent __bound) const { @@ -1018,7 +1031,7 @@ namespace ranges } template _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n, _Sent __bound) const { @@ -1034,7 +1047,7 @@ namespace ranges struct __prev_fn final { template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x) const { @@ -1043,7 +1056,7 @@ namespace ranges } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n) const { @@ -1052,7 +1065,7 @@ namespace ranges } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n, _It __bound) const { -- cgit v1.1