From e700cd0bc885340563df9e6b7a5b6c6603b8c984 Mon Sep 17 00:00:00 2001 From: Jeremy Rubin Date: Sun, 19 Jun 2016 21:42:15 -0400 Subject: Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead --- src/net.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 8 deletions(-) (limited to 'src/net.h') diff --git a/src/net.h b/src/net.h index 6ee0430f22..97604ca56e 100644 --- a/src/net.h +++ b/src/net.h @@ -129,14 +129,88 @@ public: bool CheckIncomingNonce(uint64_t nonce); bool ForNode(NodeId id, std::function func); - bool ForEachNodeContinueIf(std::function func); - bool ForEachNodeContinueIf(std::function func) const; - bool ForEachNodeContinueIfThen(std::function pre, std::function post); - bool ForEachNodeContinueIfThen(std::function pre, std::function post) const; - void ForEachNode(std::function func); - void ForEachNode(std::function func) const; - void ForEachNodeThen(std::function pre, std::function post); - void ForEachNodeThen(std::function pre, std::function post) const; + + template + bool ForEachNodeContinueIf(Callable&& func) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + if(!func(node)) + return false; + return true; + }; + + template + bool ForEachNodeContinueIf(Callable&& func) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + if(!func(node)) + return false; + return true; + }; + + template + 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 + 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 + void ForEachNode(Callable&& func) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + func(node); + }; + + template + void ForEachNode(Callable&& func) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + func(node); + }; + + template + void ForEachNodeThen(Callable&& pre, CallableAfter&& post) + { + LOCK(cs_vNodes); + for (auto&& node : vNodes) + pre(node); + post(); + }; + + template + void ForEachNodeThen(Callable&& pre, CallableAfter&& post) const + { + LOCK(cs_vNodes); + for (const auto& node : vNodes) + pre(node); + post(); + }; void RelayTransaction(const CTransaction& tx); -- cgit v1.2.3