-
Notifications
You must be signed in to change notification settings - Fork 36.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: net: avoid duplicate map lookups to mapLocalHost
#22896
refactor: net: avoid duplicate map lookups to mapLocalHost
#22896
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 11632df.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crAck 11632df
Verified that refactoring count
and operator[]
to find()
and insert()
are the only changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crtACK 11632df
Ran mainnet and the functions tests. Verified that there are no additional similar changes that could be made in net.cpp
(you found them all).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK (laptop is chugging away building bitcoind on another branch so these are drive-by untested comments, feel free to ignore)
11632df
to
fa75c59
Compare
Thanks to all reviewers! Force-pushed with all the suggestions proposed by @promag, @LarryRuane and @jonatack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK fa75c59.
crAck fa75c59 |
fa75c59
to
330d3aa
Compare
Force-pushed with suggestions of @MarcoFalke (#22896 (comment), #22896 (comment)). |
ACK 330d3aa Note, PR description is no longer accurate You're using emplace, not insert. |
Thanks for your review and the hint, I updated the PR description accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 330d3aa plus rebase to master + debug build
const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo()); | ||
LocalServiceInfo &info = it->second; | ||
if (is_newly_added || nScore >= info.nScore) { | ||
info.nScore = nScore + (is_newly_added ? 0 : 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😛
info.nScore = nScore + (is_newly_added ? 0 : 1); | |
info.nScore = nScore + !is_newly_added; |
if (mapLocalHost.count(addr) == 0) return 0; | ||
return mapLocalHost[addr].nScore; | ||
const auto it = mapLocalHost.find(addr); | ||
return (it != mapLocalHost.end()) ? it->second.nScore : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obviously doesn't matter here, but if there is an else
to the if
, I prefer to not invert the condition:
return it == mapLocalHost.end() ? 0 : it->second.nScore;
…apLocalHost` 330d3aa refactor: net: avoid duplicate map lookups to `mapLocalHost` (Sebastian Falbesoner) Pull request description: This simple refactoring PR aims to avoid duplicate lookups to `mapLocalHost`: instead of calling `count()` (to first find out whether a key is in the map) and then `operator[]` (to get the value to the passed key, or default-construct one if not found), use either * `find()` and dereference the returned iterator (for simple lookups), see https://www.cplusplus.com/reference/map/map/find/ * `emplace()` and use the returned <iterator, inserted> pair (for lookups where a new element should be inserted if the key isn't found), see https://www.cplusplus.com/reference/map/map/emplace/ ACKs for top commit: naumenkogs: ACK 330d3aa jonatack: Code review ACK 330d3aa plus rebase to master + debug build Tree-SHA512: d13da6a927ff561eee8ac6b093bf3586dfe31d6c94173a5a6d8f3698e0ee224fb394d3635155d5141c165da59d2c2c37260122eb4f2e8bcda3e8a29b901d213e
…apLocalHost` 330d3aa refactor: net: avoid duplicate map lookups to `mapLocalHost` (Sebastian Falbesoner) Pull request description: This simple refactoring PR aims to avoid duplicate lookups to `mapLocalHost`: instead of calling `count()` (to first find out whether a key is in the map) and then `operator[]` (to get the value to the passed key, or default-construct one if not found), use either * `find()` and dereference the returned iterator (for simple lookups), see https://www.cplusplus.com/reference/map/map/find/ * `emplace()` and use the returned <iterator, inserted> pair (for lookups where a new element should be inserted if the key isn't found), see https://www.cplusplus.com/reference/map/map/emplace/ ACKs for top commit: naumenkogs: ACK 330d3aa jonatack: Code review ACK 330d3aa plus rebase to master + debug build Tree-SHA512: d13da6a927ff561eee8ac6b093bf3586dfe31d6c94173a5a6d8f3698e0ee224fb394d3635155d5141c165da59d2c2c37260122eb4f2e8bcda3e8a29b901d213e
…apLocalHost` 330d3aa refactor: net: avoid duplicate map lookups to `mapLocalHost` (Sebastian Falbesoner) Pull request description: This simple refactoring PR aims to avoid duplicate lookups to `mapLocalHost`: instead of calling `count()` (to first find out whether a key is in the map) and then `operator[]` (to get the value to the passed key, or default-construct one if not found), use either * `find()` and dereference the returned iterator (for simple lookups), see https://www.cplusplus.com/reference/map/map/find/ * `emplace()` and use the returned <iterator, inserted> pair (for lookups where a new element should be inserted if the key isn't found), see https://www.cplusplus.com/reference/map/map/emplace/ ACKs for top commit: naumenkogs: ACK 330d3aa jonatack: Code review ACK 330d3aa plus rebase to master + debug build Tree-SHA512: d13da6a927ff561eee8ac6b093bf3586dfe31d6c94173a5a6d8f3698e0ee224fb394d3635155d5141c165da59d2c2c37260122eb4f2e8bcda3e8a29b901d213e
This simple refactoring PR aims to avoid duplicate lookups to
mapLocalHost
: instead of callingcount()
(to first find out whether a key is in the map) and thenoperator[]
(to get the value to the passed key, or default-construct one if not found), use eitherfind()
and dereference the returned iterator (for simple lookups), see https://www.cplusplus.com/reference/map/map/find/emplace()
and use the returned <iterator, inserted> pair (for lookups where a new element should be inserted if the key isn't found), see https://www.cplusplus.com/reference/map/map/emplace/