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
  1. luke-jr commented at 2:46 PM on April 22, 2012: member

    Postincrement (var++) requires making a temporary copy of var in C++.

  2. Change all post-increments to pre-increments, where it doesn't affect the result 352310ebdc
  3. 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.

  4. 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

    http://luke.dashjr.org/tmp/code/preinc.bin.diff.bz2

  5. 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.

  6. luke-jr commented at 3:47 PM on April 22, 2012: member

    I consider unnecessary post-increments to be akin to unused variables.

  7. jgarzik commented at 4:04 PM on April 22, 2012: contributor

    @sipa +100

  8. 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.

  9. luke-jr commented at 4:30 PM on April 22, 2012: member

    FWIW, KDE fixed this in 2007.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. gavinandresen commented at 9:24 PM on April 22, 2012: contributor

    Too much code change for zero gain.

  15. gavinandresen closed this on Apr 22, 2012

  16. suprnurd referenced this in commit c31ba8ba4c on Dec 5, 2017
  17. lateminer referenced this in commit d5f6a50c85 on Jan 22, 2019
  18. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 15:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me