From 630bbfca91de1c5568fd4bd5cd18e1c5d4ef55ef Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 18 Sep 2023 00:11:18 -0700 Subject: [PATCH] Add a way to disable the GC timer (#5656) Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/event_loop.zig | 12 ++++++++++-- src/env_loader.zig | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig index 724839ebc6..7f175a1a11 100644 --- a/src/bun.js/event_loop.zig +++ b/src/bun.js/event_loop.zig @@ -455,6 +455,7 @@ pub const GarbageCollectionController = struct { gc_repeating_timer: *uws.Timer = undefined, gc_timer_interval: i32 = 0, gc_repeating_timer_fast: bool = true, + disabled: bool = false, pub fn init(this: *GarbageCollectionController, vm: *VirtualMachine) void { var actual = vm.event_loop_handle.?; @@ -469,8 +470,12 @@ pub const GarbageCollectionController = struct { } } else |_| {} } - this.gc_repeating_timer.set(this, onGCRepeatingTimer, gc_timer_interval, gc_timer_interval); this.gc_timer_interval = gc_timer_interval; + + this.disabled = vm.bundler.env.has("BUN_GC_TIMER_DISABLE"); + + if (!this.disabled) + this.gc_repeating_timer.set(this, onGCRepeatingTimer, gc_timer_interval, gc_timer_interval); } pub fn scheduleGCTimer(this: *GarbageCollectionController) void { @@ -484,6 +489,7 @@ pub const GarbageCollectionController = struct { pub fn onGCTimer(timer: *uws.Timer) callconv(.C) void { var this = timer.as(*GarbageCollectionController); + if (this.disabled) return; this.gc_timer_state = .run_on_next_tick; } @@ -528,11 +534,12 @@ pub const GarbageCollectionController = struct { } pub fn processGCTimer(this: *GarbageCollectionController) void { + if (this.disabled) return; var vm = this.bunVM().global.vm(); this.processGCTimerWithHeapSize(vm, vm.blockBytesAllocated()); } - pub fn processGCTimerWithHeapSize(this: *GarbageCollectionController, vm: *JSC.VM, this_heap_size: usize) void { + fn processGCTimerWithHeapSize(this: *GarbageCollectionController, vm: *JSC.VM, this_heap_size: usize) void { const prev = this.gc_last_heap_size; switch (this.gc_timer_state) { @@ -568,6 +575,7 @@ pub const GarbageCollectionController = struct { } pub fn performGC(this: *GarbageCollectionController) void { + if (this.disabled) return; var vm = this.bunVM().global.vm(); vm.collectAsync(); this.gc_last_heap_size = vm.blockBytesAllocated(); diff --git a/src/env_loader.zig b/src/env_loader.zig index 2d467371ae..b8fa90d453 100644 --- a/src/env_loader.zig +++ b/src/env_loader.zig @@ -37,6 +37,13 @@ pub const Loader = struct { const empty_string_value: string = "\"\""; + pub fn has(this: *const Loader, input: []const u8) bool { + const value = this.map.get(input) orelse return false; + if (value.len == 0) return false; + + return !strings.eqlComptime(value, "\"\"") and !strings.eqlComptime(value, "''") and !strings.eqlComptime(value, "0") and !strings.eqlComptime(value, "false"); + } + pub fn isProduction(this: *const Loader) bool { const env = this.map.get("BUN_ENV") orelse this.map.get("NODE_ENV") orelse return false; return strings.eqlComptime(env, "production");