Skip to content

Commit

Permalink
Fixes #14398 (#14401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Oct 12, 2024
1 parent c77fc5d commit 4c26a25
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
31 changes: 31 additions & 0 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3939,6 +3939,37 @@ pub fn indexOfPointerInSlice(comptime T: type, slice: []const T, item: *const T)
return index;
}

pub fn getThreadCount() u16 {
const max_threads = 1024;
const min_threads = 2;
const ThreadCount = struct {
pub var cached_thread_count: u16 = 0;
var cached_thread_count_once = std.once(getThreadCountOnce);
fn getThreadCountFromUser() ?u16 {
inline for (.{ "UV_THREADPOOL_SIZE", "GOMAXPROCS" }) |envname| {
if (getenvZ(envname)) |env| {
if (std.fmt.parseInt(u16, env, 10) catch null) |parsed| {
if (parsed >= min_threads) {
if (bun.logger.Log.default_log_level.atLeast(.debug)) {
Output.note("Using {d} threads from {s}={d}", .{ parsed, envname, parsed });
Output.flush();
}
return @min(parsed, max_threads);
}
}
}
}

return null;
}
fn getThreadCountOnce() void {
cached_thread_count = @min(max_threads, @max(min_threads, getThreadCountFromUser() orelse std.Thread.getCpuCount() catch 0));
}
};
ThreadCount.cached_thread_count_once.call();
return ThreadCount.cached_thread_count;
}

/// Copied from zig std. Modified to accept arguments.
pub fn once(comptime f: anytype) Once(f) {
return Once(f){};
Expand Down
10 changes: 1 addition & 9 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,7 @@ pub const ThreadPool = struct {
if (existing_thread_pool) |pool| {
this.pool = pool;
} else {
var cpu_count = @as(u32, @truncate(@max(std.Thread.getCpuCount() catch 2, 2)));

if (v2.bundler.env.get("GOMAXPROCS")) |max_procs| {
if (std.fmt.parseInt(u32, max_procs, 10)) |cpu_count_| {
cpu_count = cpu_count_;
} else |_| {}
}

cpu_count = @max(@min(cpu_count, @as(u32, @truncate(128 - 1))), 2);
const cpu_count = bun.getThreadCount();
this.pool = try v2.graph.allocator.create(ThreadPoolLib);
this.pool.* = ThreadPoolLib.init(.{
.max_threads = cpu_count,
Expand Down
16 changes: 2 additions & 14 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8636,13 +8636,7 @@ pub const PackageManager = struct {
".npmrc",
);

var cpu_count = @as(u32, @truncate(((try std.Thread.getCpuCount()) + 1)));

if (env.get("GOMAXPROCS")) |max_procs| {
if (std.fmt.parseInt(u32, max_procs, 10)) |cpu_count_| {
cpu_count = @min(cpu_count, cpu_count_);
} else |_| {}
}
const cpu_count = bun.getThreadCount();

const options = Options{
.global = cli.global,
Expand Down Expand Up @@ -8779,13 +8773,7 @@ pub const PackageManager = struct {
PackageManager.verbose_install = true;
}

var cpu_count = @as(u32, @truncate(((try std.Thread.getCpuCount()) + 1)));

if (env.get("GOMAXPROCS")) |max_procs| {
if (std.fmt.parseInt(u32, max_procs, 10)) |cpu_count_| {
cpu_count = @min(cpu_count, cpu_count_);
} else |_| {}
}
const cpu_count = bun.getThreadCount();

var manager = &instance;
var root_dir = try Fs.FileSystem.instance.fs.readDirectory(
Expand Down
9 changes: 1 addition & 8 deletions src/work_pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ pub fn NewWorkPool(comptime max_threads: ?usize) type {
@setCold(true);

pool = ThreadPool.init(.{
.max_threads = max_threads orelse @max(2, max_threads: {
if (bun.getenvZ("GOMAXPROCS")) |max_procs| try_override: {
break :max_threads std.fmt.parseInt(u32, max_procs, 10) catch
break :try_override;
}

break :max_threads @as(u32, @truncate(std.Thread.getCpuCount() catch 0));
}),
.max_threads = max_threads orelse bun.getThreadCount(),
.stack_size = ThreadPool.default_thread_stack_size,
});
return &pool;
Expand Down

0 comments on commit 4c26a25

Please sign in to comment.