diff options
author | Jeremy Rubin <jeremy.l.rubin@gmail.com> | 2016-06-19 21:42:15 -0400 |
---|---|---|
committer | Cory Fields <cory-nospam-@coryfields.com> | 2016-09-08 13:06:05 -0400 |
commit | e700cd0bc885340563df9e6b7a5b6c6603b8c984 (patch) | |
tree | b8891fceb520ab61ffd67a2b5c9fa5c5bf892a4f | |
parent | d1a2295f0d58423652b124b48fc887a9721b765c (diff) | |
download | bitcoin-e700cd0bc885340563df9e6b7a5b6c6603b8c984.tar.xz |
Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
-rw-r--r-- | src/net.cpp | 73 | ||||
-rw-r--r-- | src/net.h | 90 |
2 files changed, 82 insertions, 81 deletions
diff --git a/src/net.cpp b/src/net.cpp index bf5cc07db1..19c14e105e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2697,79 +2697,6 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func) return found != nullptr && func(found); } -bool CConnman::ForEachNodeContinueIf(std::function<bool(CNode* pnode)> func) -{ - LOCK(cs_vNodes); - for (auto&& node : vNodes) - if(!func(node)) - return false; - return true; -} - -bool CConnman::ForEachNodeContinueIf(std::function<bool(const CNode* pnode)> func) const -{ - LOCK(cs_vNodes); - for (const auto& node : vNodes) - if(!func(node)) - return false; - return true; -} - -bool CConnman::ForEachNodeContinueIfThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post) -{ - bool ret = true; - LOCK(cs_vNodes); - for (auto&& node : vNodes) - if(!pre(node)) { - ret = false; - break; - } - post(); - return ret; -} - -bool CConnman::ForEachNodeContinueIfThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const -{ - bool ret = true; - LOCK(cs_vNodes); - for (const auto& node : vNodes) - if(!pre(node)) { - ret = false; - break; - } - post(); - return ret; -} - -void CConnman::ForEachNode(std::function<void(CNode* pnode)> func) -{ - LOCK(cs_vNodes); - for (auto&& node : vNodes) - func(node); -} - -void CConnman::ForEachNode(std::function<void(const CNode* pnode)> func) const -{ - LOCK(cs_vNodes); - for (const auto& node : vNodes) - func(node); -} - -void CConnman::ForEachNodeThen(std::function<void(CNode* pnode)> pre, std::function<void()> post) -{ - LOCK(cs_vNodes); - for (auto&& node : vNodes) - pre(node); - post(); -} - -void CConnman::ForEachNodeThen(std::function<void(const CNode* pnode)> pre, std::function<void()> post) const -{ - LOCK(cs_vNodes); - for (const auto& node : vNodes) - pre(node); - post(); -} int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) { return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5); } @@ -129,14 +129,88 @@ public: bool CheckIncomingNonce(uint64_t nonce); bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func); - bool ForEachNodeContinueIf(std::function<bool(CNode* pnode)> func); - bool ForEachNodeContinueIf(std::function<bool(const CNode* pnode)> func) const; - bool ForEachNodeContinueIfThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post); - bool ForEachNodeContinueIfThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const; - void ForEachNode(std::function<void(CNode* pnode)> func); - void ForEachNode(std::function<void(const CNode* pnode)> func) const; - void ForEachNodeThen(std::function<void(CNode* pnode)> pre, std::function<void()> post); - void ForEachNodeThen(std::function<void(const CNode* pnode)> pre, std::function<void()> post) const; + + template<typename Callable> + bool ForEachNodeContinueIf(Callable&& func) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + if(!func(node)) + return false; + return true; + }; + + template<typename Callable> + bool ForEachNodeContinueIf(Callable&& func) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + if(!func(node)) + return false; + return true; + }; + + template<typename Callable, typename CallableAfter> + bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post) + { + bool ret = true; + LOCK(cs_vNodes); + for (auto&& node : vNodes) + if(!pre(node)) { + ret = false; + break; + } + post(); + return ret; + }; + + template<typename Callable, typename CallableAfter> + bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post) const + { + bool ret = true; + LOCK(cs_vNodes); + for (const auto& node : vNodes) + if(!pre(node)) { + ret = false; + break; + } + post(); + return ret; + }; + + template<typename Callable> + void ForEachNode(Callable&& func) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + func(node); + }; + + template<typename Callable> + void ForEachNode(Callable&& func) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + func(node); + }; + + template<typename Callable, typename CallableAfter> + void ForEachNodeThen(Callable&& pre, CallableAfter&& post) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + pre(node); + post(); + }; + + template<typename Callable, typename CallableAfter> + void ForEachNodeThen(Callable&& pre, CallableAfter&& post) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + pre(node); + post(); + }; void RelayTransaction(const CTransaction& tx); |