]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add always_inline to ranges iterator ops and access functions
authorJonathan Wakely <jwakely@redhat.com>
Thu, 11 Sep 2025 08:34:02 +0000 (09:34 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 11 Sep 2025 13:38:58 +0000 (14:38 +0100)
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<typename T> requires C<T> [[nodiscard]] auto operator()(T&&)

instead of:

  template<typename T> requires C<T> auto operator() [[nodiscard]] (T&&)

The latter form was necessary because -fconcepts-ts used a different
grammar for the requires-clause, parsing 'C<T>[[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 <tkaminsk@redhat.com>
libstdc++-v3/include/bits/ranges_base.h

index 7df638db2d6fdca214cb9383291f2e53a3c7cc9b..691a181bd29600b7c1258e86a224ddf4cd85dd93 100644 (file)
@@ -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<integral _Tp>
+      [[__gnu__::__always_inline__]]
       constexpr auto
       __to_unsigned_like(_Tp __t) noexcept
       { return static_cast<make_unsigned_t<_Tp>>(__t); }
@@ -118,8 +121,9 @@ namespace ranges
       template<__maybe_borrowed_range _Tp>
        requires is_array_v<remove_reference_t<_Tp>> || __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<remove_reference_t<_Tp>>)
            {
@@ -168,8 +172,9 @@ namespace ranges
       template<__maybe_borrowed_range _Tp>
        requires is_bounded_array_v<remove_reference_t<_Tp>>
          || __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<remove_reference_t<_Tp>>)
            {
@@ -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<typename _Tp>
        requires is_bounded_array_v<remove_reference_t<_Tp>>
          || __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<remove_reference_t<_Tp>>)
            return extent_v<remove_reference_t<_Tp>>;
@@ -373,8 +381,9 @@ namespace ranges
       // 3403. Domain of ranges::ssize(E) doesn't match ranges::size(E)
       template<typename _Tp>
        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<typename _Tp>
        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<input_range _Range>
+      [[__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<typename _To, typename _Tp>
+      [[__gnu__::__always_inline__]]
       constexpr decltype(auto)
       __as_const(_Tp& __t) noexcept
       {
@@ -967,13 +980,13 @@ namespace ranges
       }
 
     template<typename _It, sized_sentinel_for<decay_t<_It>> _Sent>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr iter_difference_t<decay_t<_It>>
       operator()(_It&& __first, _Sent __last) const
       { return __last - static_cast<const decay_t<_It>&>(__first); }
 
     template<range _Range>
-      [[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<input_or_output_iterator _It>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x) const
       {
@@ -1000,7 +1013,7 @@ namespace ranges
       }
 
     template<input_or_output_iterator _It>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x, iter_difference_t<_It> __n) const
       {
@@ -1009,7 +1022,7 @@ namespace ranges
       }
 
     template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x, _Sent __bound) const
       {
@@ -1018,7 +1031,7 @@ namespace ranges
       }
 
     template<input_or_output_iterator _It, sentinel_for<_It> _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<bidirectional_iterator _It>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x) const
       {
@@ -1043,7 +1056,7 @@ namespace ranges
       }
 
     template<bidirectional_iterator _It>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x, iter_difference_t<_It> __n) const
       {
@@ -1052,7 +1065,7 @@ namespace ranges
       }
 
     template<bidirectional_iterator _It>
-      [[nodiscard]]
+      [[nodiscard, __gnu__::__always_inline__]]
       constexpr _It
       operator()(_It __x, iter_difference_t<_It> __n, _It __bound) const
       {