Skip to content

Commit 4a50ca4

Browse files
committed
feat: attempt to release the GIL while in the c++ processing to speed up /metrics
1 parent da97e88 commit 4a50ca4

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

eth_validator_watcher/mod.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,27 +321,30 @@ PYBIND11_MODULE(eth_validator_watcher_ext, m) {
321321
std::size_t chunk = (vals.size() / n) + 1;
322322
std::vector<std::thread> threads;
323323
std::vector<std::map<std::string, MetricsByLabel>> thread_metrics(n);
324+
std::map<std::string, MetricsByLabel> metrics;
324325

325-
for (size_t i = 0; i < n; i++) {
326-
threads.push_back(std::thread([slot, i, chunk, &vals, &thread_metrics] {
327-
std::size_t from = i * chunk;
328-
std::size_t to = std::min(from + chunk, vals.size());
329-
process(slot, from, to, vals, thread_metrics[i]);
330-
}));
331-
}
326+
{
327+
py::gil_scoped_release release;
328+
for (size_t i = 0; i < n; i++) {
329+
threads.push_back(std::thread([slot, i, chunk, &vals, &thread_metrics] {
330+
std::size_t from = i * chunk;
331+
std::size_t to = std::min(from + chunk, vals.size());
332+
process(slot, from, to, vals, thread_metrics[i]);
333+
}));
334+
}
332335

333-
for (auto& thread: threads) {
334-
thread.join();
335-
}
336+
for (auto& thread: threads) {
337+
thread.join();
338+
}
336339

337-
std::map<std::string, MetricsByLabel> metrics;
338-
merge(thread_metrics, &metrics);
340+
merge(thread_metrics, &metrics);
341+
}
339342

340343
py::dict pymetrics;
341344
for (const auto& [label, metric]: metrics) {
342345
pymetrics[py::str(label)] = metric;
343346
}
344-
347+
345348
return pymetrics;
346349
});
347350
}

0 commit comments

Comments
 (0)