diff options
| author | 2022-01-02 15:28:20 -0800 | |
|---|---|---|
| committer | 2022-01-02 15:28:20 -0800 | |
| commit | ad0834bedb6aecf0545ecf707f6d98a8cb7cd942 (patch) | |
| tree | ca55e4788c96d6c08b0098a0239d710f74b6f054 /src | |
| parent | b53c63910de3239099ed685527c16db6217ebe3d (diff) | |
| download | bun-ad0834bedb6aecf0545ecf707f6d98a8cb7cd942.tar.gz bun-ad0834bedb6aecf0545ecf707f6d98a8cb7cd942.tar.zst bun-ad0834bedb6aecf0545ecf707f6d98a8cb7cd942.zip | |
Fix bug where bun uses ~15% of CPU idly
Diffstat (limited to 'src')
| -rw-r--r-- | src/thread_pool.zig | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/thread_pool.zig b/src/thread_pool.zig index 1b39f2606..a0cf10633 100644 --- a/src/thread_pool.zig +++ b/src/thread_pool.zig @@ -189,6 +189,7 @@ noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool { var is_idle = false; var is_waking = _is_waking; var sync = @bitCast(Sync, self.sync.load(.Monotonic)); + var idle_network_ticks: u16 = 0; while (true) { if (sync.state == .shutdown) return error.Shutdown; @@ -252,6 +253,17 @@ noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool { while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > HTTP.AsyncHTTP.max_simultaneous_requests) { io.tick() catch {}; } + + idle_network_ticks += @as(u16, @boolToInt(HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) == 0)); + + // If it's been roughly 2ms since the last network request, go to sleep! + // this is 2ms because run_for_ns runs for 10 microseconds + // 10 microseconds * 200 == 2ms + if (idle_network_ticks > 200) { + self.idle_event.wait(); + idle_network_ticks = 0; + } + sync = @bitCast(Sync, self.sync.load(.Monotonic)); continue; } |
