net processing: Move peer_map to PeerManager#19910
Conversation
|
Requested by @MarcoFalke @sdaftuar and @theuni . Review very much appreciated 🙏 |
src/rpc/net.cpp
Outdated
There was a problem hiding this comment.
It is currently not possible to not have node.peerman. I think an Ensure..() like EnsureMempool would be good. It doesn't really make sense to call getpeerinfo without a peerman anyway?
There was a problem hiding this comment.
I've done something slightly different and checked existence at the top of this function. Let me know what you think.
e4488c3 to
322e87b
Compare
|
Concept ACK. |
|
Concept ACK: decoupling is good! |
hebasto
left a comment
There was a problem hiding this comment.
ACK 322e87b6039877ed3457baea93cc87d78f3f4d96, I have reviewed the code and it looks OK.
322e87b to
d350feb
Compare
hebasto
left a comment
There was a problem hiding this comment.
re-ACK d350feb2013e8db8a7464ec40670ef609d77e778
|
Concept ACK. |
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
b28f866 to
658ca70
Compare
658ca70 to
1b25b5c
Compare
1b25b5c to
2ce2ec1
Compare
3bf5917 to
aedd418
Compare
|
rebased |
src/net_processing.cpp
Outdated
There was a problem hiding this comment.
6bfcef3797d4d105180e325033929684f84cfc08
Why are these changes required? What are benefits to return an empty shared pointer instead nullptr-constructed one? Btw, in all caller places the return value is checked for nullptr, not being empty.
There was a problem hiding this comment.
This makes is clear that NRVO can be used, since there's only one return statement. I'm not sure if the previous version would have used copy elision.
There was a problem hiding this comment.
+1 for considering this!
I'd be curious to know if compilers are allowed to rearrange the code before deciding how many return statements there are. I could see this going either way.
There was a problem hiding this comment.
Considering the before-change code, to be precise, in C++17 in a return statement, when the operand is a prvalue (the result of the ternary conditional expression), elision of copy/move operations is mandatory.
OTOH, NRVO is non-mandatory.
There was a problem hiding this comment.
Thanks @hebasto! Yes, I think you're right. If I'm reading it correctly, the result of the conditional operator is a prvalue (item 5 in https://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator since 4 doesn't apply), and copy elision is mandatory for returning a prvalue.
I've reverted this.
There was a problem hiding this comment.
... and copy elision is mandatory for returning a prvalue.
Good to have C++17 :)
|
Code Review ACK aedd418 Private members >> Anon-namespaced statics/functions Especially when the functionality is strongly intertwined with the class of which it's being made a member. |
aedd418 to
5a1f08c
Compare
|
Rebased and addressed outstanding review comments. |
promag
left a comment
There was a problem hiding this comment.
Core review ACK 4fe77ae9aab73d8de6c40d504e079e30eff45f91.
Could squash last commit or fix the comment in 5a1f08ce1f4d49fe85b8571cd5da1b1096339a89.
This allows us to avoid repeated locking in FinalizeNode()
4fe77ae to
3025ca9
Compare
| PeerRef PeerManager::RemovePeer(NodeId id) | ||
| { | ||
| PeerRef ret; | ||
| LOCK(m_peer_mutex); | ||
| auto it = m_peer_map.find(id); | ||
| if (it != m_peer_map.end()) { | ||
| ret = std::move(it->second); | ||
| m_peer_map.erase(it); | ||
| } | ||
| return ret; | ||
| } |
There was a problem hiding this comment.
3025ca9
I'm curios is it possible to force the mandatory copy/move elision for the return value here as well?
|
Re-ACK 3025ca9. |
|
Re-ACK 3025ca9
|
| { | ||
| PeerRef peer = GetPeerRef(nodeid); | ||
| PeerRef peer = RemovePeer(nodeid); | ||
| assert(peer != nullptr); |
There was a problem hiding this comment.
I'm getting bitcoind crashing due to this assert. It appears that sometimes FinalizeNode gets called twice (from DeleteNode()). Perhaps nRefCount is being increased between the two occurances. This only seems to happen though when there's a delay between them due to UpdateTip(). It's probably a bug I've introduced somewhere, but just leaving this comment as a "heads up".
There was a problem hiding this comment.
Thanks. I expect if the bug was present in master we'd have seen it in CI or from a user report.
This moves
g_peer_mapfrom a global in net_processing.cpp's unnamed namespace to being a memberm_peer_mapofPeerManager.