From e69f0517369e2a9933d66be63fd471e8a1553ecd Mon Sep 17 00:00:00 2001 From: jpark37 Date: Sun, 19 Sep 2021 22:35:29 -0700 Subject: [PATCH] libobs/util: Improve os_sleepto_ns on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid nanosecond abstraction to reduce math operations. Replace Sleep(0) with YieldProcessor(). We want the thread to remain scheduled, the current CPU core to do less work, and the hyperthread sibling to perform better. Hacky profiling showed maybe 10-25 µs skid reduction per function call. I think power/performance gains would be hard to measure, so I haven't tried, but it would be shocking if they got worse. --- libobs/util/platform-windows.c | 35 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 5b5fba9f5..e1d95577f 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -330,27 +330,30 @@ void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info) bool os_sleepto_ns(uint64_t time_target) { - uint64_t t = os_gettime_ns(); - uint32_t milliseconds; + const double freq = (double)get_clockfreq(); + const LONGLONG count_target = + (LONGLONG)((double)time_target * freq / 1000000000.0); - if (t >= time_target) - return false; + LARGE_INTEGER count; + QueryPerformanceCounter(&count); - milliseconds = (uint32_t)((time_target - t) / 1000000); - if (milliseconds > 1) - Sleep(milliseconds - 1); + const bool stall = count.QuadPart < count_target; + if (stall) { + const DWORD milliseconds = (DWORD)( + ((count_target - count.QuadPart) * 1000.0) / freq); + if (milliseconds > 1) + Sleep(milliseconds - 1); - for (;;) { - t = os_gettime_ns(); - if (t >= time_target) - return true; + for (;;) { + QueryPerformanceCounter(&count); + if (count.QuadPart >= count_target) + break; -#if 0 - Sleep(1); -#else - Sleep(0); -#endif + YieldProcessor(); + } } + + return stall; } void os_sleep_ms(uint32_t duration)