Skip to content

Commit e700cd0

Browse files
JeremyRubintheuni
authored andcommitted
Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
1 parent d1a2295 commit e700cd0

File tree

2 files changed

+82
-81
lines changed

2 files changed

+82
-81
lines changed

src/net.cpp

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,79 +2697,6 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
26972697
return found != nullptr && func(found);
26982698
}
26992699

2700-
bool CConnman::ForEachNodeContinueIf(std::function<bool(CNode* pnode)> func)
2701-
{
2702-
LOCK(cs_vNodes);
2703-
for (auto&& node : vNodes)
2704-
if(!func(node))
2705-
return false;
2706-
return true;
2707-
}
2708-
2709-
bool CConnman::ForEachNodeContinueIf(std::function<bool(const CNode* pnode)> func) const
2710-
{
2711-
LOCK(cs_vNodes);
2712-
for (const auto& node : vNodes)
2713-
if(!func(node))
2714-
return false;
2715-
return true;
2716-
}
2717-
2718-
bool CConnman::ForEachNodeContinueIfThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post)
2719-
{
2720-
bool ret = true;
2721-
LOCK(cs_vNodes);
2722-
for (auto&& node : vNodes)
2723-
if(!pre(node)) {
2724-
ret = false;
2725-
break;
2726-
}
2727-
post();
2728-
return ret;
2729-
}
2730-
2731-
bool CConnman::ForEachNodeContinueIfThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const
2732-
{
2733-
bool ret = true;
2734-
LOCK(cs_vNodes);
2735-
for (const auto& node : vNodes)
2736-
if(!pre(node)) {
2737-
ret = false;
2738-
break;
2739-
}
2740-
post();
2741-
return ret;
2742-
}
2743-
2744-
void CConnman::ForEachNode(std::function<void(CNode* pnode)> func)
2745-
{
2746-
LOCK(cs_vNodes);
2747-
for (auto&& node : vNodes)
2748-
func(node);
2749-
}
2750-
2751-
void CConnman::ForEachNode(std::function<void(const CNode* pnode)> func) const
2752-
{
2753-
LOCK(cs_vNodes);
2754-
for (const auto& node : vNodes)
2755-
func(node);
2756-
}
2757-
2758-
void CConnman::ForEachNodeThen(std::function<void(CNode* pnode)> pre, std::function<void()> post)
2759-
{
2760-
LOCK(cs_vNodes);
2761-
for (auto&& node : vNodes)
2762-
pre(node);
2763-
post();
2764-
}
2765-
2766-
void CConnman::ForEachNodeThen(std::function<void(const CNode* pnode)> pre, std::function<void()> post) const
2767-
{
2768-
LOCK(cs_vNodes);
2769-
for (const auto& node : vNodes)
2770-
pre(node);
2771-
post();
2772-
}
27732700
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
27742701
return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
27752702
}

src/net.h

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,88 @@ class CConnman
129129
bool CheckIncomingNonce(uint64_t nonce);
130130

131131
bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func);
132-
bool ForEachNodeContinueIf(std::function<bool(CNode* pnode)> func);
133-
bool ForEachNodeContinueIf(std::function<bool(const CNode* pnode)> func) const;
134-
bool ForEachNodeContinueIfThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post);
135-
bool ForEachNodeContinueIfThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const;
136-
void ForEachNode(std::function<void(CNode* pnode)> func);
137-
void ForEachNode(std::function<void(const CNode* pnode)> func) const;
138-
void ForEachNodeThen(std::function<void(CNode* pnode)> pre, std::function<void()> post);
139-
void ForEachNodeThen(std::function<void(const CNode* pnode)> pre, std::function<void()> post) const;
132+
133+
template<typename Callable>
134+
bool ForEachNodeContinueIf(Callable&& func)
135+
{
136+
LOCK(cs_vNodes);
137+
for (auto&& node : vNodes)
138+
if(!func(node))
139+
return false;
140+
return true;
141+
};
142+
143+
template<typename Callable>
144+
bool ForEachNodeContinueIf(Callable&& func) const
145+
{
146+
LOCK(cs_vNodes);
147+
for (const auto& node : vNodes)
148+
if(!func(node))
149+
return false;
150+
return true;
151+
};
152+
153+
template<typename Callable, typename CallableAfter>
154+
bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post)
155+
{
156+
bool ret = true;
157+
LOCK(cs_vNodes);
158+
for (auto&& node : vNodes)
159+
if(!pre(node)) {
160+
ret = false;
161+
break;
162+
}
163+
post();
164+
return ret;
165+
};
166+
167+
template<typename Callable, typename CallableAfter>
168+
bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post) const
169+
{
170+
bool ret = true;
171+
LOCK(cs_vNodes);
172+
for (const auto& node : vNodes)
173+
if(!pre(node)) {
174+
ret = false;
175+
break;
176+
}
177+
post();
178+
return ret;
179+
};
180+
181+
template<typename Callable>
182+
void ForEachNode(Callable&& func)
183+
{
184+
LOCK(cs_vNodes);
185+
for (auto&& node : vNodes)
186+
func(node);
187+
};
188+
189+
template<typename Callable>
190+
void ForEachNode(Callable&& func) const
191+
{
192+
LOCK(cs_vNodes);
193+
for (const auto& node : vNodes)
194+
func(node);
195+
};
196+
197+
template<typename Callable, typename CallableAfter>
198+
void ForEachNodeThen(Callable&& pre, CallableAfter&& post)
199+
{
200+
LOCK(cs_vNodes);
201+
for (auto&& node : vNodes)
202+
pre(node);
203+
post();
204+
};
205+
206+
template<typename Callable, typename CallableAfter>
207+
void ForEachNodeThen(Callable&& pre, CallableAfter&& post) const
208+
{
209+
LOCK(cs_vNodes);
210+
for (const auto& node : vNodes)
211+
pre(node);
212+
post();
213+
};
140214

141215
void RelayTransaction(const CTransaction& tx);
142216

0 commit comments

Comments
 (0)