From 4f9f7468ee55c3f580eee4923225f08db0db08cf Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Thu, 5 Sep 2024 15:26:57 -0400 Subject: [PATCH] use const refs for std::function callbacks --- src/ActiveMonitors.h | 12 ++++++------ src/DBQuery.h | 6 +++--- src/ThreadPool.h | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ActiveMonitors.h b/src/ActiveMonitors.h index dbc8c66..633728a 100644 --- a/src/ActiveMonitors.h +++ b/src/ActiveMonitors.h @@ -85,7 +85,7 @@ struct ActiveMonitors : NonCopyable { conns.erase(connId); } - void process(lmdb::txn &txn, defaultDb::environment::View_Event &ev, std::function cb) { + void process(lmdb::txn &txn, defaultDb::environment::View_Event &ev, const std::function &cb) { RecipientList recipients; auto processMonitorSet = [&](MonitorSet &ms){ @@ -101,7 +101,7 @@ struct ActiveMonitors : NonCopyable { } }; - auto processMonitorsExact = [&](btree_map &m, const T &key, std::function matches){ + auto processMonitorsExact = [&](btree_map &m, const T &key, const std::function &matches){ auto it = m.upper_bound(key); if (it == m.begin()) return; @@ -118,21 +118,21 @@ struct ActiveMonitors : NonCopyable { { Bytes32 id(packed.id()); - processMonitorsExact(allIds, id, static_cast>([&](const Bytes32 &val){ + processMonitorsExact(allIds, id, static_cast &>([&](const Bytes32 &val){ return id == val; })); } { Bytes32 pubkey(packed.pubkey()); - processMonitorsExact(allAuthors, pubkey, static_cast>([&](const Bytes32 &val){ + processMonitorsExact(allAuthors, pubkey, static_cast &>([&](const Bytes32 &val){ return pubkey == val; })); } packed.foreachTag([&](char tagName, std::string_view tagVal){ auto &tagSpec = getTagSpec(tagName, tagVal); - processMonitorsExact(allTags, tagSpec, static_cast>([&](const std::string &val){ + processMonitorsExact(allTags, tagSpec, static_cast &>([&](const std::string &val){ return tagSpec == val; })); return true; @@ -140,7 +140,7 @@ struct ActiveMonitors : NonCopyable { { auto kind = packed.kind(); - processMonitorsExact(allKinds, kind, static_cast>([&](const uint64_t &val){ + processMonitorsExact(allKinds, kind, static_cast &>([&](const uint64_t &val){ return kind == val; })); } diff --git a/src/DBQuery.h b/src/DBQuery.h index 6749982..7e91f4b 100644 --- a/src/DBQuery.h +++ b/src/DBQuery.h @@ -226,7 +226,7 @@ struct DBScan : NonCopyable { refillScanDepth = 10 * initialScanDepth; } - bool scan(lmdb::txn &txn, std::function handleEvent, std::function doPause) { + bool scan(lmdb::txn &txn, const std::function &handleEvent, const std::function &doPause) { auto cmp = [](auto &a, auto &b){ return a.created() == b.created() ? a.levId() > b.levId() : a.created() > b.created(); }; @@ -299,7 +299,7 @@ struct DBQuery : NonCopyable { DBQuery(const tao::json::value &filter, uint64_t maxLimit = MAX_U64) : sub(Subscription(1, ".", NostrFilterGroup::unwrapped(filter, maxLimit))) {} // If scan is complete, returns true - bool process(lmdb::txn &txn, std::function cb, uint64_t timeBudgetMicroseconds = MAX_U64, bool logMetrics = false) { + bool process(lmdb::txn &txn, const std::function &cb, uint64_t timeBudgetMicroseconds = MAX_U64, bool logMetrics = false) { while (filterGroupIndex < sub.filterGroup.size()) { const auto &f = sub.filterGroup.filters[filterGroupIndex]; @@ -370,7 +370,7 @@ struct DBQuery : NonCopyable { }; -inline void foreachByFilter(lmdb::txn &txn, const tao::json::value &filter, std::function cb) { +inline void foreachByFilter(lmdb::txn &txn, const tao::json::value &filter, const std::function &cb) { DBQuery query(filter); query.process(txn, [&](const auto &, uint64_t levId){ diff --git a/src/ThreadPool.h b/src/ThreadPool.h index 573ce92..cb1fbe5 100644 --- a/src/ThreadPool.h +++ b/src/ThreadPool.h @@ -19,7 +19,7 @@ struct ThreadPool { join(); } - void init(std::string name, uint64_t numThreads_, std::function cb) { + void init(std::string name, uint64_t numThreads_, const std::function &cb) { if (numThreads_ == 0) throw herr("must have more than 0 threads"); numThreads = numThreads_; @@ -49,7 +49,7 @@ struct ThreadPool { pool[who].inbox.push_move_all(m); } - void dispatchToAll(std::function cb) { + void dispatchToAll(const std::function &cb) { for (size_t i = 0; i < numThreads; i++) pool[i].inbox.push_move(cb()); }