HomePhabricator

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

Description

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

Summary:

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-

Backport of core PR13241
https://github.com/bitcoin/bitcoin/pull/13241/files

Note to reviewers:
There is a couple of changes not backported from the PR, due to missing
dependencies regarding the wallet label API. A follow-up diff will
backport PR13480 and introduce a clang warning to detect copies in
loops, so I think this is safe to leave it for now.

Test Plan:

make check

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D3795

Details

Provenance
Ben Woosley <ben.woosley@gmail.com>Authored on May 15 2018, 22:41
FabienCommitted on Aug 5 2019, 07:20
FabienPushed on Aug 5 2019, 07:30
Reviewer
Restricted Project
Differential Revision
D3795: scripted-diff: Avoid temporary copies when looping over std::map
Parents
rSTAGING4f0e8a5a102a: macOS: Prevent Xcode 9.3 build warnings
Branches
Unknown
Tags
Unknown