Skip to content

Do not idle in the event loop if there are pending immediate tasks #17901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/bun.js/api/Timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,18 @@ pub const All = struct {
if (this.active_timer_count == 0) {
return false;
}
if (vm.event_loop.immediate_tasks.count > 0 or vm.event_loop.next_immediate_tasks.count > 0) {
spec.* = .{ .nsec = 0, .sec = 0 };
return true;
}

var now: timespec = undefined;
var has_set_now: bool = false;
var maybe_now: ?timespec = null;
while (this.timers.peek()) |min| {
if (!has_set_now) {
now = timespec.now();
has_set_now = true;
}
const now = maybe_now orelse now: {
const real_now = timespec.now();
maybe_now = real_now;
break :now real_now;
};

switch (now.order(&min.next)) {
.gt, .eq => {
Expand All @@ -202,8 +206,6 @@ pub const All = struct {
return true;
},
}

unreachable;
}

return false;
Expand Down
32 changes: 21 additions & 11 deletions test/js/node/timers/node-timers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it, test, mock } from "bun:test";
import { clearInterval, clearTimeout, promises, setInterval, setTimeout, setImmediate } from "node:timers";
import { promisify } from "util";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, isWindows } from "harness";
import jsc from "bun:jsc";
import path from "node:path";

Expand Down Expand Up @@ -229,14 +229,24 @@ describe("clear", () => {
});
});

describe("setImmediate", () => {
it("has reasonable performance when nested with no other timers running", async () => {
const process = Bun.spawn({
cmd: [bunExe(), path.join(__dirname, "setImmediate-fixture.ts")],
stdout: "pipe",
env: bunEnv,
});

expect(await new Response(process.stdout).text()).toBe("callback\n".repeat(5000));
}, 5000);
describe.each(["with", "without"])("setImmediate %s timers running", mode => {
// TODO(@190n) #17901 did not fix this for Windows
it.todoIf(isWindows && mode == "with")(
"has reasonable performance when nested",
async () => {
const process = Bun.spawn({
cmd: [bunExe(), path.join(__dirname, "setImmediate-fixture.ts"), mode + "-interval"],
stdout: "pipe",
env: bunEnv,
});

await process.exited;
const out = await new Response(process.stdout).text();
expect(process.exitCode).toBe(0);
// if this fails, there will be a nicer error than printing out the entire string
expect((out.match(/\n/g) ?? []).length).toBe(5000);
expect(out).toBe("callback\n".repeat(5000));
},
5000,
);
});
20 changes: 19 additions & 1 deletion test/js/node/timers/setImmediate-fixture.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.