]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize std::projected<I, std::identity>
authorPatrick Palka <ppalka@redhat.com>
Thu, 22 Aug 2024 13:24:39 +0000 (09:24 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 22 Aug 2024 13:24:39 +0000 (09:24 -0400)
Algorithms that are generalized to take projections typically default the
projection to std::identity, which is equivalent to no projection at all.
In that case, I believe we could shortcut the projection logic to return
the iterator unchanged rather than wrapping it.  This should reduce compile
times especially after P2609R3 which made the indirect invocability
concepts more expensive to check when actual projections are involved.

libstdc++-v3/ChangeLog:

* include/bits/iterator_concepts.h (__detail::__projected): Define
an optimized partial specialization for when the projection is
std::identity.
* testsuite/24_iterators/indirect_callable/projected.cc: Verify the
optimization.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/include/bits/iterator_concepts.h
libstdc++-v3/testsuite/24_iterators/indirect_callable/projected.cc

index d849ddc32fc261a9627050623d0713004e8c025a..642c709fee0c28754c1942395a822720d58312ac 100644 (file)
@@ -803,6 +803,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          using __projected_Proj = _Proj;
        };
       };
+
+    // Optimize the common case of the projection being std::identity.
+    template<typename _Iter>
+      struct __projected<_Iter, identity>
+      { using __type = _Iter; };
   } // namespace __detail
 
   /// [projected], projected
index 0eec42c2d5fa09b7f4c4b55784a55913afc28cbe..9ab7db9d65df11c02d3294b218451b3fe6285bb0 100644 (file)
 template<typename T>
   using PI = std::projected<T, std::identity>;
 
+#if __GLIBCXX__
+// Verify our projected<I, identity> optimization.
+static_assert(std::same_as<PI<int*>, int*>);
+#else
 static_assert(std::same_as<PI<int*>::value_type, int>);
+#endif
 static_assert(std::same_as<decltype(*std::declval<const PI<int*>&>()), int&>);
 
 struct X