Skip to content

Fix LUA_GCTIMEOUT timeout being 100x too long on Windows - #10

Open
tmarenko wants to merge 1 commit into
OpenXRay:v2.1from
tmarenko:fix-gc-windows
Open

tmarenko wants to merge 1 commit into
OpenXRay:v2.1from
tmarenko:fix-gc-windows

Conversation

@tmarenko

@tmarenko tmarenko commented Nov 2, 2026

Copy link
Copy Markdown

gc_step_timeout() treats get_high_resolution_time() as nanoseconds.
On Windows, though, it returned raw QueryPerformanceCounter ticks (usually 10 MHz), so the limit was 100× too long.
In xray-16, lua_gc_timeout 1000 gave each CLevel::script_gc() call about 100 ms instead of 1 ms, which ran a full GC cycle every frame.

Fix

  • Convert the QPC ticks to nanoseconds: (t / f) * 1e9 + (t % f) * 1e9 / f. The plain t * 1e9 / f would overflow uint64_t after ~30 minutes of uptime at 10 MHz. The split form gives the same result exactly and stays in range for any counter frequency below ~18 GHz.
  • The frequency is read on every call rather than cached in a static, so there is no shared state between threads. QueryPerformanceFrequency costs ~1.6 ns, against ~17 ns for QueryPerformanceCounter.
  • Compute timeout_usec * 1000 in 64 bits. It was a 32-bit multiply, which wrapped for timeouts over ~4.3 s.

Only the Windows branch changes. Linux and macOS already returned nanoseconds, so Windows now behaves the same as they do.

Testing

Windows 11 x64, MSVC 2022:

  • lua_gc(L, LUA_GCTIMEOUT, 1000) on a ~300 MB heap: worst call 100.01 → 1.14 ms, average 71.56 → 1.01 ms
  • In game with lua_gc_method gc_timeout, the main thread's wait on this task dropped from ~44 % to ~0.1 % of the frame, and the Lua heap stayed flat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant