Refactor: Changes postincrement to preincrement for iterator in for loops in src/wallet files #14753

pull vim88 wants to merge 1 commits into bitcoin:master from vim88:postincrement_to_preincrement_src_wallet changing 2 files +2 −2
  1. vim88 commented at 3:17 PM on November 18, 2018: contributor

    Changed the postincrement operator to a preincrement operator for the iterator in the for loops in src/wallet/rpcdump.cpp and src/wallet/wallet.cpp.

    For integers the compiler does the optimisation automatically. In this case an iterator is used, so this changes avoid creating an unnecessary temporary copy of the iterator.

    Comments on this PR suggest the use of a preincrement for optimisation.

    Targeted only the files in src/wallet to not overcomplicate things and to keep this changes isolated to the scr/wallet files, so it can be easier to review and test.

    I look forward to your opinions about this.

    Thank you!

  2. Refactor: Changes postincrement to preincrement for iterator in for loops in src/wallet files a1f8901f9c
  3. gmaxwell commented at 6:45 PM on November 18, 2018: contributor

    I would be pretty surprised if this difference isn't optimized out.

  4. fanquake added the label Refactoring on Nov 18, 2018
  5. fanquake added the label Wallet on Nov 18, 2018
  6. meshcollider commented at 1:22 AM on November 19, 2018: contributor

    @gmaxwell surprisingly this SO answer suggests that's not true https://stackoverflow.com/a/38948073/8253658

  7. gmaxwell commented at 6:10 AM on November 19, 2018: contributor
    #include <stdio.h>
    #include <stdint.h>
    #include <vector>
    
    extern std::vector<int64_t> tv;
    int main(void){
      for (std::vector<int64_t>::const_iterator it = tv.begin(); it != tv.end(); it++) {
        printf("bloop.\n");
      }
    }
    

    Gives the same object code for me when compiled with g++ 7.3.1 with either it++ or ++it. Anyone care to improve my test?

  8. ken2812221 commented at 7:18 AM on November 19, 2018: contributor

    It might be better to use range-based for loop that doesn't have to call end() every time. (Maybe it's already optimized by the compiler in -O2) Anyhow, it would not be a really long code anymore.

  9. sipa commented at 7:47 AM on November 19, 2018: member

    None of these things matter at all.

    The suggestion for using preincrement over postincrement is because in some cases, one can avoid creating a copy. In practice, for all simple types this does not matter, including most standard library iterators (which just encapsulate a single pointer).

    It's a good guideline, but there is no need to go over the code and change things everywhere (the developer guidelines even point out that you shouldn't make PRs that just change style). If you believe this is not just a stylistic change, please demonstrate that there is an observable performance benefit (by adding a benchmark).

  10. vim88 commented at 8:05 PM on November 20, 2018: contributor

    These small changes can indeed be considered not that important. To go and apply them everywhere now, might not make much sense, indeed.

    Thank you all for your input!

  11. vim88 closed this on Nov 20, 2018

  12. 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-15 15:14 UTC

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