This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Optimzed vs. readable code
Pierre THIERRY wrote:
> Scribit Jonathan Wakely dies 30/11/2005 hora 17:18:
>> If the change was to be accepted, should it be a new iterator object
>> or just a reference to give the existing object a new name?
>
> Now that I have thought of it (see my another post), I think the right
> thing would be a reference. I'd let others do the choice, because I'm
> not used to references much...
While I'm sure it would be optimized out, it's possible the alias tracker
would have some more work to do.
>
>> but possibly worse if a regression occurs in the compiler
>
> We have to let compilers' developpers deal with that. Are you telling
> you will never adapt your coding techniques to the evolution of computer
> science?
>
Some of my own recent work was re-writing much of the code in <algorithm>,
so that there wasn't a separate predicate and non-predicate version of
many algorithms. The trade-offs involves could be argued as similar. That
change removed code duplication, and could (although I checked carefully)
have lead to slower code. It was decided by myself (and people who know
much better than me!) that the trade-off was probably worth it to improve
maintainability and reduce code duplication.
Also remember, such changes are not completely free, as it requires one of
the maintainers to check the code, and also there is the slim but non-zero
possibility something could be broken in the process.
The biggest argument against I would say is that I believe it would
decrease readability. Personally, if I saw a copy or a reference being
taken, I would assume it was for some reason, and then look for the places
where the copy/reference and the original were used, and then get confused
that it wasn't. You could of course add a comment above saying "We are
taking this reference to give a better name to what we are doing", but for
that you may as well just add a comment saying "We use the first iterator
to keep the current position".
Chris