perf(tracker): multi-thread ONNX inference (was single-threaded, the dominant latency; now ~half the cores clamped 2-4) + replace the capture->infer Sleep(1) poll with a condition-variable wake and a buffer swap (no fixed poll latency, no per-frame copy under lock). User rebuilds the tracker.

This commit is contained in:
kinect-bot committed 2026-11-27 01:01:25 -05:00
1 parent 10dd2859ca
commit 86722b0379
2 files changed
+33 -19

No files matched your search

+8 -1
View File
@@ -7,6 +7,7 @@
#include <cmath>
#include <cstring>
#include <string>
#include <thread>
namespace kinect_tracker {
namespace {
@@ -51,7 +52,13 @@ Affine2x3 crop_to_image(const Roi& roi) {
PoseEstimator::PoseEstimator(const char* landmark_model_path) {
env_ = std::make_unique<Ort::Env>(ORT_LOGGING_LEVEL_WARNING, "pose");
Ort::SessionOptions so;
so.SetIntraOpNumThreads(1);
// Parallelise inference across a few cores (was single-threaded, the dominant
// latency cost). Use about half the cores, clamped to [2,4], so the game
// (which runs in a separate process on the same machine) keeps enough CPU.
// The biggest remaining lever is a GPU execution provider (DirectML) - see
// docs/game-internals.md; that needs the onnxruntime-directml binary.
const unsigned hw = std::thread::hardware_concurrency();
so.SetIntraOpNumThreads(std::clamp<int>(static_cast<int>(hw) / 2, 2, 4));
so.SetGraphOptimizationLevel(ORT_ENABLE_ALL);
#ifdef _WIN32
const std::string p(landmark_model_path);
+25 -18
View File
@@ -5,6 +5,8 @@
#include <wincodec.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
@@ -246,6 +248,7 @@ void WorkerLoop(bool image_mode, std::wstring symlink, int req_w, int req_h) {
// end-to-end latency to a single inference instead of a growing FIFO backlog
// (the source reader queues frames when inference is slower than the camera).
std::mutex cap_mutex;
std::condition_variable cap_cv;
std::vector<uint8_t> cap_bgra;
int cap_w = 0, cap_h = 0;
std::atomic<uint64_t> cap_seq{0};
@@ -258,11 +261,14 @@ void WorkerLoop(bool image_mode, std::wstring symlink, int req_w, int req_h) {
int tw = 0, th = 0;
while (cap_running.load() && g_app.running) {
if (camera.read_frame(tmp, tw, th)) {
std::lock_guard<std::mutex> lk(cap_mutex);
cap_bgra.swap(tmp);
cap_w = tw;
cap_h = th;
cap_seq.fetch_add(1, std::memory_order_release);
{
std::lock_guard<std::mutex> lk(cap_mutex);
cap_bgra.swap(tmp);
cap_w = tw;
cap_h = th;
cap_seq.fetch_add(1, std::memory_order_release);
}
cap_cv.notify_one();
}
}
CoUninitialize();
@@ -277,19 +283,20 @@ void WorkerLoop(bool image_mode, std::wstring symlink, int req_w, int req_h) {
h = src_h;
Sleep(33);
} else {
// Grab the freshest captured frame; skip if none newer has arrived.
const uint64_t s = cap_seq.load(std::memory_order_acquire);
if (s == last_seq) {
Sleep(1);
continue;
}
{
std::lock_guard<std::mutex> lk(cap_mutex);
bgra = cap_bgra;
w = cap_w;
h = cap_h;
}
last_seq = s;
// Block until the capture thread delivers a fresher frame (woken by the
// condition variable, so there's no fixed poll latency), then take
// ownership of its buffer by swapping instead of copying.
std::unique_lock<std::mutex> lk(cap_mutex);
cap_cv.wait_for(lk, std::chrono::milliseconds(100), [&] {
return cap_seq.load(std::memory_order_acquire) != last_seq ||
!g_app.running;
});
if (!g_app.running) break;
if (cap_seq.load(std::memory_order_acquire) == last_seq) continue;
bgra.swap(cap_bgra);
w = cap_w;
h = cap_h;
last_seq = cap_seq.load(std::memory_order_acquire);
}
// Mirror horizontally. Verified against real Kinect v1 recordings (UTKinect
// waveHands + MSR arm-waves): on a real Kinect the right hand reads as