Skip to content

Commit 0da57dc

Browse files
authored
fix: prevent OpenCV from overriding our threading configuration (#700)
## Description We observed activity on all CPU cores despite manually configuring the thread pool. OpenCV's internal threading was activating all available cores, overriding our optimized thread configuration and resulting in worse performance. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [x] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent 73a47a8 commit 0da57dc

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,6 @@ void RnExecutorchInstaller::injectJSIBindings(
108108

109109
threads::utils::unsafeSetupThreadPool();
110110
threads::GlobalThreadPool::initialize();
111-
112-
#if defined(__ANDROID__) && defined(__aarch64__)
113-
auto num_of_perf_cores =
114-
::executorch::extension::cpuinfo::get_num_performant_cores();
115-
log(LOG_LEVEL::Info, "Detected ", num_of_perf_cores, " performant cores");
116-
// setting num_of_cores to floor(num_of_perf_cores / 2) + 1) because depending
117-
// on cpu arch as when possible we want to leave at least 2 performant cores
118-
// for other tasks (setting more actually results in drop of performance). For
119-
// older devices (i.e. samsung s22) resolves to 3 cores, and for newer ones
120-
// (like OnePlus 12) resolves to 4, which when benchamrked gives highest
121-
// throughput.
122-
auto num_of_cores = static_cast<uint32_t>(num_of_perf_cores / 2) + 1;
123-
::executorch::extension::threadpool::get_threadpool()
124-
->_unsafe_reset_threadpool(num_of_cores);
125-
log(LOG_LEVEL::Info, "Configuring xnnpack for ", num_of_cores, " threads");
126-
#endif
127111
}
128112

129113
} // namespace rnexecutorch

packages/react-native-executorch/common/rnexecutorch/threads/GlobalThreadPool.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <executorch/extension/threadpool/cpuinfo_utils.h>
55
#include <memory>
66
#include <mutex>
7+
#include <opencv2/opencv.hpp>
78
#include <optional>
89
#include <rnexecutorch/Log.h>
910
#include <rnexecutorch/threads/HighPerformanceThreadPool.h>
@@ -38,6 +39,9 @@ class GlobalThreadPool {
3839
numThreads, "threads");
3940
instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(),
4041
config);
42+
// Disable OpenCV's internal threading to prevent it from overriding our
43+
// thread pool configuration, which would cause degraded performance
44+
cv::setNumThreads(0);
4145
});
4246
}
4347

0 commit comments

Comments
 (0)