Files
DucielPerf/docs/loading-screen-analysis.md
T
DucielandClaude Opus 5.5 8d74f1274f Loader thread: Sleep(0) instead of Sleep(1) after each file (0.9.3)
The client's single file-loader thread slept 1 ms after every file
(0x4434D2), so zone-change loading screens spent 55-75% of their time
asleep at ~2 ms per texture. loadsleep (default on, live) patches the
immediate of `mov ecx,1` to 0, only when the 10 bytes are the client's
(logged and left alone otherwise); switching it off restores the byte.
test_loadsleep checks the byte logic on a fake buffer. Not yet tested in
game.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CmDrvE1QTJT4No3PkwJwv4
2026-11-18 01:23:53 +01:00

8.4 KiB

Loading screens: where the time goes

Analysis only (no code change yet). WoW.exe 1.12.1 build 5875, image base 0x400000, disassembled with tools/dis.sh (ndisasm). Samples come from Logs\ducielperf_freezes.log (15-18 Nov 2026, 33 zone-change loading screens).

Summary

The main thread does not sleep because there is nothing to do: it waits for the game's single async file loader thread, and that thread calls Sleep(1) after every file it loads (0x4434D2). A loading screen loads 300-1900 textures/models one by one, so it spends about 1-2 ms per file sleeping, i.e. 1-3 s per loading screen. Disk reads and decompression are not the limit (fully cached loads are just as slow). Changing that one Sleep(1) into Sleep(0) (a one-byte patch) should cut a typical 2-4.5 s loading screen to roughly 1-1.5 s.

The code

0x429D50: OsSleep(ms)

00429D50  push ecx
00429D51  call [Sleep]        ; IAT 0x7FF2A0 = KERNEL32!Sleep
00429D57  ret

__fastcall wrapper: ECX = milliseconds. The watchdog's "WoW.exe 0x429D57" frame is the return address inside it. Every caller passes ECX = 1.

0x443D30: wait for all async loads (loading screen)

Called from 0x694920 (world load: call 0x698390 to queue the map's files, then call 0x443D30, with 0x86A2D0 = 1 meanwhile; 0x694951 is the return address of that call). Loop at 0x443D80-0x443DF2:

loop:
  call 0x443E70          ; ProcessCompleted (return 0x443D89)
  if [0x885668]          ; progress-bar callback
      call it with (total - pending) / total
  mov ecx,1
  call 0x429D50          ; Sleep(1)            (return 0x443DEB)
  call 0x443E20          ; anything pending / in flight / done?
  jnz loop
  • 0x443E20 (HasPending) takes the loader lock (critical section 0x88569C) and returns true while the request list (0x885690) or the done list (0x8856CC) is non-empty or an item is in flight (0x8856D0).
  • 0x443E70 (ProcessCompleted) first does SetEvent(0x885698) (wakes the loader thread), then pops done items and calls each one's callback (call [esi+0x10] at 0x443EF9, return 0x443EFC; for textures this is TextureLoadCallback 0x44A500), decrementing the pending counter 0x88567C, for at most 20 ms per call (cmp eax,0x14 at 0x443F0A on the ms clock 0x42C010).

So 0x443DEB = main thread asleep in the loop; 0x443D89 / 0x443EFC = main thread running completion callbacks. There is no event for "an item finished": the main thread polls every Sleep(1).

A second, simpler wait (0x443CF0-0x443D1C: Sleep(1) until the in-flight item 0x885684 is done) exists but never shows in the samples.

0x443360: the loader thread (one thread)

Created at 0x443356 through the thread helper 0x659AC0 / 0x64BD20 (the watchdog sees its start address as the generic thread entry 0x64BC20).

outer:
  WaitForSingleObject(event 0x885698, 500 ms)
inner:
  lock 0x88569C; pop one request from 0x885690; 0x8856D0 = item; unlock
  (if none: unlock, check the stop event 0x8856DC, back to outer)
  read the file: ReadFileFromMultipleSources 0x648460
      (up to 10 tries, Sleep(1) between failed tries: 0x44343A)
  lock; append to the done list; 0x8856D0 = 0; item+0x28 = 1; unlock
  mov ecx,1
  call 0x429D50          ; Sleep(1) after EVERY item   <-- 0x4434D2
  jmp inner

It does one file at a time and sleeps after each one, even when the request list is full. This is a 2004-era "yield to the render thread" on single-core machines.

The numbers

Per loading screen (33 samples; frame = the loading screen frame):

range typical
frame 0.4-5.0 s 2-4.5 s
textures completed (textureDone) 180-1874 900-1900
frame time per texture 1.4-2.7 ms ~2.2 ms
main thread asleep at 0x443DEB 39-76% 55-70%
loader thread asleep (NtDelayExecution) 57-85% 70-80%
loader thread busy (file read, mostly in ducielperf's cache/inflate) 15-40% 20-30%

Examples: 18/11 00:22:58, 2213 ms, 1099 textures, main 55% asleep, loader 76% asleep; 17/11 09:32:56, 4288 ms, 1869 textures, 67% / 76%; 15/11 20:52:58, 4964 ms, 1835 textures, 57% / 68%. The two loads with real disk reads (17/11 10:56:17 and 10:57:55: 150 MB read + decompressed in ~0.35 s) were no slower per texture than cached ones.

The loader thread is asleep (NtDelayExecution, not NtWaitForSingleObject: it is the Sleep(1), not the event wait) 70-80% of the time while the main thread waits for it. ~2 ms per file is one Sleep(1) at a 1 ms timer resolution (~1-2 ms per call) plus 0.3-0.5 ms of real work. WoW.exe does not import timeBeginPeriod, so the 1 ms resolution comes from another module or Windows; at the default 15.6 ms resolution the same loop would take ~15 ms per file (30 s per loading screen), so the client is very sensitive to it.

Conclusion: the wait is not idle in the sense of "nothing to do". The request list is full the whole time; the main thread waits on the loader, and the loader is slow only because it sleeps after each file. Disk and decompression account for under 0.4 s.

Patch options

  1. Loader: Sleep(1) -> Sleep(0) after each item (recommended). One byte: 0x4434D3 01 -> 00 (mov ecx,1 -> mov ecx,0 before call 0x429D50). Sleep(0) gives the CPU to a ready thread of the same priority and otherwise returns at once. Alternative: NOP the 5-byte call at 0x4434D7, or detour 0x429D50 and turn Sleep(1) into SwitchToThread() only when the return address is 0x4434DC.

    • Gain: the loader's per-file time drops from ~2 ms to its real work (~0.3-0.5 ms), so the loading screen becomes bound by the main thread's own work (its non-sleep share: 0.6-1.4 s for a 2-4.5 s load). Estimated 1-3 s saved per loading screen (about the main thread's sleep share, 55-70%), assuming the main thread's callbacks and progress-bar drawing keep up; they ran only 25-45% of the frame.
    • Risks: low. The lists stay under the same lock; nothing in the loop depends on the delay. The loader now competes with the main thread for a core (no issue on 2+ cores; Sleep(0) still yields). More lock traffic on 0x88569C (short critical sections). The loader is also used outside loading screens (streaming textures in the world): files arrive faster there too, i.e. more completion callbacks per frame on the main thread; ProcessCompleted already caps itself at 20 ms per call. Can be a live switch (one byte, written atomically) like the other perf switches.
  2. Main-thread wait: Sleep(1) at 0x443DE1. Once the loader is fast, each main iteration processes every completed item (up to 20 ms) and then sleeps ~1-2 ms; the sleep costs at most a few percent. Waking on an event would need a new "item done" event set by the loader (code in our DLL at the loader's unlock point). Small gain, more code: only worth it after option 1 is measured.

  3. More loader threads. The loader keeps one in-flight item in a global (0x8856D0, also read by the cancel path 0x4439C0-0x4439E4 and by HasPending). Several threads would need that bookkeeping rewritten. Disk and decompression are not the bottleneck, so no gain worth the risk.

  4. Finer timer resolution (NtSetTimerResolution 0.5 ms): halves the cost of every Sleep(1) in the process, but option 1 removes it entirely and has no process-wide side effects (power use).

Cross-checks

  • No WeirdUtils / vanilla-fixes notes on this loop are in this repository (third_party holds only MinHook and libdeflate). VanillaHelpers.dll is loaded in this client; whether it touches 0x4434D2 is not known; a patch should check the original bytes B9 01 00 00 00 E8 before writing.
  • Sample counts in the log (reports in which the frame appears on the main thread's stack): 0x429D57 45, 0x443DEB 33, 0x443D89 28, 0x443EFC 28, 0x694951 17, loader thread reports (started at 0x64BC20) 102.
  • The other stack frames listed in the reports (0x4246B0, 0x617FA7, 0x616504, 0x616765, 0x615B6F, 0x616840, 0x420FDA, 0x630BF8, 0x58ACF3/0x58ACA8, 0x694473/0x69447F, 0x6983C7) are the callers above this loop (main loop, world/zone change handling, map load at 0x698390) and the completion callbacks; none of them sleeps.

Next step

Done in 0.9.3: option 1 as the loadsleep perf switch (default on, not yet measured in game). To measure with the watchdog on the same zone changes: expect the main thread's "waiting / sleeping" share during loading screens to drop from 55-70% to under 10% and textureDone per second to rise from ~450 to 1500+.