This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Optimize std::advance for single increments


LWG 2931 suggests a new overload of std::next for the default n==1
case, because it can be simpler for some random access iterators to do
++i than i += 1, e.g. for std::deque::iterator.

I've suggested we close that issue as NAD, because we don't need a new
overload to do that, and here's the proof. This performs the same
optimisation when the argument to __advance<RandomAccessIterator> is
known at compile-time to be +1 or -1, as is the case when called from
std::next(i) or std::prev(i).

This makes the generated assembly for
std::next<std::deque<T,A>::iterator>(i) quite a lot shorter.

In practice we probably don't need the optimisation in std::advance,
only in std::next and std::prev, because it would be very unusual to
call std::advance(i, 1) or std::advance(i, -1) when you could just do
++i or --i. But doing it here means we handle the std::next/std::prev
cases, and std::advance, and any unusual cases like std::next(i, -1).

	* include/bits/stl_iterator_base_funcs.h
	(__advance<_RandomAccessIterator, _Distance>): Optimize for next/prev
	cases where incrementing or decrementing a single step.

Tested powerpc64le-linux, committed to trunk.

Attachment: patch.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]