Skip to content

Conversation

@theStack
Copy link
Contributor

@theStack theStack commented Sep 5, 2021

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

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 11632dfaaba98378dd10905549251590b321452f.

Copy link
Contributor

@klementtan klementtan left a 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.

Copy link
Contributor

@LarryRuane LarryRuane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crtACK 11632dfaaba98378dd10905549251590b321452f
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).

Copy link
Member

@jonatack jonatack left a 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)

@theStack theStack force-pushed the 202109-refactor-net-avoid_duplicate_mapLocalHost_lookups branch from 11632df to fa75c59 Compare September 12, 2021 21:24
@theStack
Copy link
Contributor Author

Thanks to all reviewers! Force-pushed with all the suggestions proposed by @promag, @LarryRuane and @jonatack.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK fa75c590f3e26d675d5e5506aba49e159fe20221.

@klementtan
Copy link
Contributor

crAck fa75c590f3e26d675d5e5506aba49e159fe20221

@theStack theStack force-pushed the 202109-refactor-net-avoid_duplicate_mapLocalHost_lookups branch from fa75c59 to 330d3aa Compare September 14, 2021 16:25
@theStack
Copy link
Contributor Author

Force-pushed with suggestions of @MarcoFalke (#22896 (comment), #22896 (comment)).

@naumenkogs
Copy link
Contributor

ACK 330d3aa

Note, PR description is no longer accurate
insert() 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/insert/

You're using emplace, not insert.

@theStack
Copy link
Contributor Author

Note, PR description is no longer accurate
insert() 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/insert/

You're using emplace, not insert.

Thanks for your review and the hint, I updated the PR description accordingly.

Copy link
Member

@jonatack jonatack left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😛

Suggested change
info.nScore = nScore + (is_newly_added ? 0 : 1);
info.nScore = nScore + !is_newly_added;

@maflcko maflcko merged commit e69cbac into bitcoin:master Sep 17, 2021
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;
Copy link
Member

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;

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Sep 19, 2021
…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
@theStack theStack deleted the 202109-refactor-net-avoid_duplicate_mapLocalHost_lookups branch September 21, 2021 13:37
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 13, 2022
…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
gades pushed a commit to cosanta/cosanta-core that referenced this pull request May 16, 2022
…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
@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants