Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
57b674874e fix(gc): reduce idle CPU by transitioning GC timer to slow mode faster
The GC repeating timer previously required 30 consecutive ticks (30 seconds)
of stable heap size before switching from fast mode (1s interval) to slow mode
(30s interval). This caused unnecessary event loop wakeups and CPU usage during
idle periods, especially in applications with large module registries.

Reduce the threshold from 30 to 5 ticks, so the GC timer transitions to slow
mode after just 5 seconds of stable heap. This significantly reduces idle CPU
consumption while still providing responsive GC during active allocation periods.

Closes #27365

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-23 06:28:23 +00:00

View File

@@ -96,7 +96,7 @@ pub fn onGCTimer(timer: *uws.Timer) callconv(.c) void {
// - Slow: GC runs every 30 seconds
//
// When the heap size is increasing, we always switch to fast mode
// When the heap size has been the same or less for 30 seconds, we switch to slow mode
// When the heap size has been the same for 5 consecutive ticks, we switch to slow mode
pub fn updateGCRepeatTimer(this: *GarbageCollectionController, comptime setting: @Type(.enum_literal)) void {
if (setting == .fast and !this.gc_repeating_timer_fast) {
this.gc_repeating_timer_fast = true;
@@ -116,7 +116,7 @@ pub fn onGCRepeatingTimer(timer: *uws.Timer) callconv(.c) void {
this.gc_last_heap_size_on_repeating_timer = this.gc_last_heap_size;
if (prev_heap_size == this.gc_last_heap_size_on_repeating_timer) {
this.heap_size_didnt_change_for_repeating_timer_ticks_count +|= 1;
if (this.heap_size_didnt_change_for_repeating_timer_ticks_count >= 30) {
if (this.heap_size_didnt_change_for_repeating_timer_ticks_count >= 5) {
// make the timer interval longer
this.updateGCRepeatTimer(.slow);
}