Postincrement (var++) requires making a temporary copy of var in C++.
Change all post-increments to pre-increments, where it doesn't affect the result #1135
pull luke-jr wants to merge 1 commits into bitcoin:master from luke-jr:preinc changing 41 files +368 −368-
luke-jr commented at 2:46 PM on April 22, 2012: member
-
Change all post-increments to pre-increments, where it doesn't affect the result 352310ebdc
-
Diapolo commented at 2:56 PM on April 22, 2012: none
Wow many code changes for a little win, but I'm fine with them.
-
luke-jr commented at 3:15 PM on April 22, 2012: member
Binary changes to: addrman, bitcoinrpc, db, guiutil, keystore, main, net, script, wallet, walletdb
-
sipa commented at 3:45 PM on April 22, 2012: member
I consider pre-increment/post-increment more of a code style preference - no need to change it if it doesn't gain you anything.
-
luke-jr commented at 3:47 PM on April 22, 2012: member
I consider unnecessary post-increments to be akin to unused variables.
-
laanwj commented at 4:17 PM on April 22, 2012: member
The number of changes could be brought down if you'd only do this for non-primitive types (ie, iterators).
But even then, this is a kind of a micro-optimization that makes sense for tight inner loops. I'm not sure it's worth changing everywhere.
-
luke-jr commented at 4:30 PM on April 22, 2012: member
FWIW, KDE fixed this in 2007.
-
tcatm commented at 6:31 PM on April 22, 2012: none
I just compiled a simple for-loop twice (i++ and ++i) and compared the disassembled code. There were no differences even when using -O0.
-
luke-jr commented at 6:33 PM on April 22, 2012: member
This isn't the only cleanup for 0.6.1 that has almost no effect on binary output. I suspect the actual binary differences are all in the iterators.
-
tcatm commented at 7:06 PM on April 22, 2012: none
I've just talk to someone who knows a thing about C++ compilers. He said iterators from the std library can be inlined and thus it++ vs ++it won't make a difference there either.
In case we overload ++ in Bitcoin this might in fact make a difference but the better way to fix it would be by making the overloaded method inline-able.
-
luke-jr commented at 7:11 PM on April 22, 2012: member
That's assuming glibc and GCC. Best to do things the "right way" so we don't depend on implementation-specific optimizations.
-
gavinandresen commented at 9:24 PM on April 22, 2012: contributor
Too much code change for zero gain.
- gavinandresen closed this on Apr 22, 2012
- suprnurd referenced this in commit c31ba8ba4c on Dec 5, 2017
- lateminer referenced this in commit d5f6a50c85 on Jan 22, 2019
- DrahtBot locked this on Sep 8, 2021