Optimize std::advance for single increments
authorJonathan Wakely <jwakely@redhat.com>
Mon, 5 Jun 2017 10:34:13 +0000 (11:34 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 5 Jun 2017 10:34:13 +0000 (11:34 +0100)
* include/bits/stl_iterator_base_funcs.h
(__advance<_RandomAccessIterator, _Distance>): Optimize for next/prev
cases where incrementing or decrementing a single step.

From-SVN: r248875

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_iterator_base_funcs.h

index 4ee4c42c7ea240202d2a84888916dd0fd4be3c33..0b0881091e3dd20b2fe3c95b7867f56ed24fb53b 100644 (file)
@@ -1,5 +1,9 @@
 2017-06-05  Jonathan Wakely  <jwakely@redhat.com>
 
+       * include/bits/stl_iterator_base_funcs.h
+       (__advance<_RandomAccessIterator, _Distance>): Optimize for next/prev
+       cases where incrementing or decrementing a single step.
+
        * include/bits/shared_ptr_base.h (__shared_ptr::owner_before)
        (__weak_ptr::owner_before, _Sp_owner_less::operator()): Add noexcept
        specifiers as per LWG 2873 and LWG 2942.
index ce6c3d20e03ae9307d3d679af46703f57d450b84..e14a22ce25512b5e4996e5b70efd36da7a24286c 100644 (file)
@@ -177,7 +177,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // concept requirements
       __glibcxx_function_requires(_RandomAccessIteratorConcept<
                                  _RandomAccessIterator>)
-      __i += __n;
+      if (__builtin_constant_p(__n) && __n == 1)
+       ++__i;
+      else if (__builtin_constant_p(__n) && __n == -1)
+       --__i;
+      else
+       __i += __n;
     }
 
   /**