aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-02 16:29:04 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-02 16:29:04 -0800
commit8a9e81d51481f0705ff858d9034c2a9cb6f85c6f (patch)
tree7141a8f081515dfacccbf8558c123cb949d96f72 /src
parentb17df61043d154e06e290640320a55ec275445d7 (diff)
downloadbun-8a9e81d51481f0705ff858d9034c2a9cb6f85c6f.tar.gz
bun-8a9e81d51481f0705ff858d9034c2a9cb6f85c6f.tar.zst
bun-8a9e81d51481f0705ff858d9034c2a9cb6f85c6f.zip
Be a little smarter about sleeping the netwrok thread
Diffstat (limited to 'src')
-rw-r--r--src/install/install.zig10
-rw-r--r--src/thread_pool.zig29
2 files changed, 31 insertions, 8 deletions
diff --git a/src/install/install.zig b/src/install/install.zig
index 5f8beb5f8..b96cf8a1f 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -6387,6 +6387,10 @@ pub const PackageManager = struct {
};
const cwd = std.fs.cwd();
+
+ // sleep goes off, only need to set it once because it will have an impact on the next network request
+ NetworkThread.global.pool.sleep_on_idle_network_thread = false;
+
while (iterator.nextNodeModulesFolder()) |node_modules| {
try cwd.makePath(std.mem.span(node_modules.relative_path));
// We deliberately do not close this folder.
@@ -6529,6 +6533,9 @@ pub const PackageManager = struct {
package_json_contents: string,
comptime log_level: Options.LogLevel,
) !void {
+ // sleep off for maximum network throughput
+ NetworkThread.global.pool.sleep_on_idle_network_thread = false;
+
var load_lockfile_result: Lockfile.LoadFromDiskResult = if (manager.options.do.load_lockfile)
manager.lockfile.loadFromDisk(
ctx.allocator,
@@ -6791,6 +6798,9 @@ pub const PackageManager = struct {
std.os.exit(1);
}
+ // sleep on since we might not need it anymore
+ NetworkThread.global.pool.sleep_on_idle_network_thread = true;
+
if (had_any_diffs or needs_new_lockfile or manager.package_json_updates.len > 0) {
manager.lockfile = try manager.lockfile.clean(&manager.summary.deduped, manager.package_json_updates, &manager.options);
}
diff --git a/src/thread_pool.zig b/src/thread_pool.zig
index a0cf10633..92443e9f1 100644
--- a/src/thread_pool.zig
+++ b/src/thread_pool.zig
@@ -10,6 +10,7 @@ const assert = std.debug.assert;
const Atomic = std.atomic.Atomic;
io: ?*AsyncIO = null,
+sleep_on_idle_network_thread: bool = true,
stack_size: u32,
max_threads: u32,
@@ -186,10 +187,20 @@ noinline fn notifySlow(self: *ThreadPool, is_waking: bool) void {
}
noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool {
+ if (self.sleep_on_idle_network_thread and self.io != null) {
+ return _wait(self, _is_waking, true);
+ }
+
+ return _wait(self, _is_waking, false);
+}
+
+// sleep_on_idle seems to impact `bun install` performance negatively
+// so we can just not sleep for that
+fn _wait(self: *ThreadPool, _is_waking: bool, comptime sleep_on_idle: 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;
+ var idle_network_ticks: if (sleep_on_idle) u16 else void = if (comptime sleep_on_idle) 0 else void{};
while (true) {
if (sync.state == .shutdown) return error.Shutdown;
@@ -254,14 +265,16 @@ noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool {
io.tick() catch {};
}
- idle_network_ticks += @as(u16, @boolToInt(HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) == 0));
+ if (sleep_on_idle) {
+ 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;
+ // If it's been roughly 2ms since the last network request, go to sleep!
+ // this is 4ms because run_for_ns runs for 10 microseconds
+ // 10 microseconds * 400 == 4ms
+ if (idle_network_ticks > 400) {
+ self.idle_event.wait();
+ idle_network_ticks = 0;
+ }
}
sync = @bitCast(Sync, self.sync.load(.Monotonic));