scheduler: Shorten the clock period on Windows while waiting for a short timer
Windows wakes a thread from a timed wait on its clock interrupt, which by
default arrives every 15.625 ms. A libuv timer therefore fires up to that much
late: `sleep(0.001)` and `Timer(0.001)` take 16 ms, and `sleep(0.02)` takes
32 ms.
Before the event loop blocks in `uv_run`, look at how long it is going to wait.
If a timer is due within 50 ms, request a 1 ms clock period with
`timeBeginPeriod` for the duration of that wait. Longer waits, and waits
without a timer, are left alone, since a higher interrupt rate costs power: a
program that polls with `sleep(0.1)` should not hold the request permanently.
| median, ms | before | after | Linux (WSL2) |
|-----------------------------------|--------|-------|--------------|
| `sleep(0.001)` | 16.09 | 2.12 | 2.1 |
| `sleep(0.005)` | 16.06 | 6.15 | 6.1 |
| `sleep(0.02)` | 32.09 | 21.25 | 21.1 |
| `sleep(0.04)` | 48.13 | 41.19 | |
| `sleep(0.1)` | 112.1 | 112.8 | |
| 1000 tasks in `sleep(0.001)` | 15.7 | 4.6 | 3.3 |
| 50 ticks of `Timer(0; interval=0.01)` | 792 | 502 | |
A blocking `Sleep(1)` made after `sleep(0.001)` has returned still takes
16 ms, so the request does not outlive the wait.
Assisted-by: Claude Code (Fable 5.2)