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.
Empact
commented at 11:22 pm on May 15, 2018:
member
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.
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.
practicalswift
commented at 7:42 am on May 16, 2018:
contributor
Concept ACK
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.
utACKa631e95.
Empact force-pushed
on Jun 1, 2018
Empact
commented at 12:06 pm on June 1, 2018:
member
DrahtBot added the label
Needs rebase
on Jun 11, 2018
MarcoFalke removed the label
Needs rebase
on Jun 11, 2018
DrahtBot
commented at 3:03 pm on June 11, 2018:
member
DrahtBot added the label
Needs rebase
on Jun 11, 2018
Empact force-pushed
on Jun 11, 2018
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
Empact force-pushed
on Jun 11, 2018
Empact
commented at 8:17 pm on June 11, 2018:
member
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-11-17 12:12 UTC
This site is hosted by @0xB10C More mirrored repositories can be found on mirror.b10c.me