scripted-diff: Avoid temporary copies when looping over std::map #13241

pull Empact wants to merge 1 commits into bitcoin:master from Empact:pair-const-key changing 5 files +18 −18
  1. Empact commented at 11:22 pm on May 15, 2018: member

    The ::value_type of the std::map/std::multimap/std::unordered_map containers is std::pair<const Key, T>. Dropping the const results in an unnecessary copy, for example in C++11 range-based loops.

    For this I started with a more general scripted diff, then narrowed it down based on the inspection showing that all actual map/multimap/unordered_map variables used in loops start with m or have map in the name.

  2. Empact commented at 11:22 pm on May 15, 2018: member
    This picks up from #12169
  3. fanquake added the label Refactoring on May 15, 2018
  4. Empact force-pushed on May 15, 2018
  5. Empact force-pushed on May 15, 2018
  6. sipa commented at 0:05 am on May 16, 2018: member

    Concept ACK, this is clearly an improvement.

    However, I feel that this issue is also an indication against using explicit types in this situation. Usually the intent with explicit types (in places where auto would be sufficient) is making sure relevant code fails to compile when a type is changed. This however is an example where an incorrect type results in degraded but otherwise undetectable degradation.

  7. Empact commented at 0:21 am on May 16, 2018: member

    @sipa Fair enough, an alternative would be to switch to auto or ::value_type for the associated collections.

    Main argument for explicit types in this case is that they are locally self-documenting. I find first and second pretty weak on that front. So I’d prefer value_type over auto.

    More arguments re auto in #12120.

  8. Empact force-pushed on May 16, 2018
  9. practicalswift commented at 7:42 am on May 16, 2018: contributor
    Concept ACK
  10. promag commented at 12:20 pm on May 16, 2018: member

    This may improve current code but the problem can come again in future code. I’m +1 in following a simple convention that prevents this problem. Personally for range based loops I prefer auto& or const auto& for any kind of container.

    utACK a631e95.

  11. Empact force-pushed on Jun 1, 2018
  12. Empact commented at 12:06 pm on June 1, 2018: member
    Rebased to accomodate #13194
  13. Empact commented at 1:11 pm on June 1, 2018: member
    See also auto discussion in the prior #12169.
  14. DrahtBot added the label Needs rebase on Jun 11, 2018
  15. MarcoFalke removed the label Needs rebase on Jun 11, 2018
  16. DrahtBot commented at 3:03 pm on June 11, 2018: member
  17. DrahtBot added the label Needs rebase on Jun 11, 2018
  18. Empact force-pushed on Jun 11, 2018
  19. scripted-diff: Avoid temporary copies when looping over std::map
    The ::value_type of the std::map/std::multimap/std::unordered_map containers is
    std::pair<const Key, T>. Dropping the const results in an unnecessary copy,
    for example in C++11 range-based loops.
    
    For this I started with a more general scripted diff, then narrowed it down
    based on the inspection showing that all actual map/multimap/unordered_map
    variables used in loops start with m or have map in the name.
    
    -BEGIN VERIFY SCRIPT-
    sed -i -E 's/for \(([^<]*)std::pair<([^c])(.+) : m/for (\1std::pair<const \2\3 : m/' src/*.cpp src/**/*.cpp
    sed -i -E 's/for \(([^<]*)std::pair<([^c])(.+) : (.*)map/for (\1std::pair<const \2\3 : \4map/' src/*.cpp src/**/*.cpp
    -END VERIFY SCRIPT-
    9b72c988a0
  20. Empact force-pushed on Jun 11, 2018
  21. Empact commented at 8:17 pm on June 11, 2018: member
    Rebased for #13060 and #13194
  22. ken2812221 commented at 12:30 pm on June 12, 2018: contributor
    utACK 9b72c98
  23. DrahtBot removed the label Needs rebase on Jun 12, 2018
  24. sipa commented at 11:23 pm on June 14, 2018: member

    utACK 9b72c988a0050d8932275c74c60928918ee7ef71

    Some of these look like they might have a performance impact too.

    The discussion on auto/::value_type/explicit type is probably one to hold elsewhere; this is a clear improvement.

  25. MarcoFalke commented at 4:31 pm on June 15, 2018: member
    utACK 9b72c988a0
  26. MarcoFalke merged this on Jun 15, 2018
  27. MarcoFalke closed this on Jun 15, 2018

  28. MarcoFalke referenced this in commit be27048a18 on Jun 15, 2018
  29. Empact deleted the branch on Jun 15, 2018
  30. laanwj referenced this in commit 31145a3d7c on Jun 24, 2018
  31. PastaPastaPasta referenced this in commit 371e213067 on Jul 7, 2020
  32. PastaPastaPasta referenced this in commit 60e0ee4a99 on Jul 7, 2020
  33. PastaPastaPasta referenced this in commit 85f633e26b on Jul 7, 2020
  34. PastaPastaPasta referenced this in commit c298c1ef77 on Jul 8, 2020
  35. MarcoFalke 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: 2024-10-04 22:12 UTC

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