1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

flann: use per-thread storage for heap pool to remove lock contention

This commit is contained in:
Ijtihed Kilani
2026-06-19 20:41:33 +03:00
parent 4f17d30997
commit fbd4dc6ca0
4 changed files with 115 additions and 3 deletions
@@ -532,7 +532,11 @@ public:
const bool explore_all_trees = get_param(searchParams,"explore_all_trees",false);
// Priority queue storing intermediate branches in the best-bin-first search
const cv::Ptr<Heap<BranchSt>>& heap = Heap<BranchSt>::getPooledInstance(cv::utils::getThreadID(), (int)size_);
// Kept in thread_local storage so each thread owns an independent heap
// and no process-wide lock is taken on the search hot path (issue #25281).
thread_local cv::Ptr<Heap<BranchSt>> heap = cv::makePtr<Heap<BranchSt>>((int)size_);
heap->clear();
heap->reserve((int)size_);
std::vector<bool> checked(size_,false);
int checks = 0;
@@ -449,7 +449,11 @@ private:
DynamicBitset checked(size_);
// Priority queue storing intermediate branches in the best-bin-first search
const cv::Ptr<Heap<BranchSt>>& heap = Heap<BranchSt>::getPooledInstance(cv::utils::getThreadID(), (int)size_);
// Kept in thread_local storage so each thread owns an independent heap
// and no process-wide lock is taken on the search hot path (issue #25281).
thread_local cv::Ptr<Heap<BranchSt>> heap = cv::makePtr<Heap<BranchSt>>((int)size_);
heap->clear();
heap->reserve((int)size_);
/* Search once through each tree down to root. */
for (i = 0; i < trees_; ++i) {
@@ -528,7 +528,11 @@ public:
}
else {
// Priority queue storing intermediate branches in the best-bin-first search
const cv::Ptr<Heap<BranchSt>>& heap = Heap<BranchSt>::getPooledInstance(cv::utils::getThreadID(), (int)size_);
// Kept in thread_local storage so each thread owns an independent heap
// and no process-wide lock is taken on the search hot path (issue #25281).
thread_local cv::Ptr<Heap<BranchSt>> heap = cv::makePtr<Heap<BranchSt>>((int)size_);
heap->clear();
heap->reserve((int)size_);
int checks = 0;
for (int i=0; i<trees_; ++i) {