unnessasary complexity in std::rotate for bidirectional iterators

Chris Jefferson caj@cs.york.ac.uk
Tue Sep 14 17:33:00 GMT 2004


I was just looking at the std::rotate overload for bidirectional iterators and found something unusual.  After squinting for a while, I decided everything after the first two calls to reverse, ie this code:

-----
      while (__first != __middle && __middle != __last)
         swap(*__first++, *--__last);

       if (__first == __middle)
         std::__reverse(__middle, __last,  bidirectional_iterator_tag());
       else
         std::__reverse(__first,  __middle,bidirectional_iterator_tag());
-----

Is simply implementing
std::__reverse(__first, __last,    bidirectional_iterator_tag());

I don't see why this more complicated method would be any more optimised 
than simply calling std::__reverse (except of course that this code is 
calling swap directly instead of iter_swap so might use more optimised 
swapping, but I suspect that isn't intentional). A quick benchmark on 
x86 seem to run (very slightly, but repeatedly) faster just calling 
reverse.

Is there any kind of document listing design decisions?

As I assume this was done for optimisation reasons, I wouldn't want to change it to the more obvious implementation with knowing that this wouldn't degrade things under some systems, or in future versions of g++. One option would be to either put a new version into the performance 
testsuite or alter the code and put the old version into the performance 
testsuite, and then see what happens when some benchmarks come in and 
how things change over time. Is there any pre-existing examples or guidelines about how to do this type of thing? (I couldn't see any in testsuite/performance)

Thanks,

Chris



More information about the Libstdc++ mailing list