Merge pull request #3615 from lightpanda-io/reduce-v8-background-execution-delay

perf: reduce delay in running v8 background jobs
This commit is contained in:
Karl Seguin authored and GitHub committed 2026-09-24 06:12:41 +08:00
commit 5112323b58
4 files changed
+22 -7

No files matched your search

+3 -2
View File
@@ -262,14 +262,15 @@ pub fn runMicrotasks(self: *Browser) void {
self.env.runMicrotasks();
}
pub fn runMacrotasks(self: *Browser) !void {
pub fn runMacrotasks(self: *Browser) !bool {
const env = &self.env;
try self.env.runMacrotasks();
env.pumpMessageLoop();
const ran_platform_task = env.pumpMessageLoop();
// either of the above could have queued more microtasks
env.runMicrotasks();
return ran_platform_task;
}
pub fn hasBackgroundTasks(self: *Browser) bool {
+12 -2
View File
@@ -35,6 +35,7 @@ const Runner = @This();
session: *Session,
browser: *Browser,
http_client: *HttpClient,
background_poll_ms: u32 = 0,
pub const Opts = struct {};
@@ -223,8 +224,9 @@ fn _tick(self: *Runner, comptime is_cdp: bool, timeout_ms: u32, conditions: []Wa
const has_runnable_page = hasRunnablePage(session);
var ran_platform_task = false;
if (has_runnable_page) {
try browser.runMacrotasks();
ran_platform_task = try browser.runMacrotasks();
}
const activity = http_client.activity();
@@ -318,8 +320,16 @@ fn _tick(self: *Runner, comptime is_cdp: bool, timeout_ms: u32, conditions: []Wa
break :blk 200;
}
if (browser.hasBackgroundTasks()) {
// if our last runMacrotasks() ran something and we now have
// a background, then don't linger in the http client waiting
// for I/O, instead, hurry back to run more tasks.
// Else, backoff to 10ms between runs.
// TODO: this is a temporary solution to ensuring background
// tasks are run promptly.The better solution is to have v8
// wakeup the http client when there's work to do.
self.background_poll_ms = if (ran_platform_task) 0 else @min(10, @max(1, self.background_poll_ms * 2));
// msToNextTask could be less than this, but 10ms drift is ok
break :blk 10;
break :blk self.background_poll_ms;
}
break :blk browser.msToNextTask() orelse 200;
};
+6 -2
View File
@@ -567,14 +567,18 @@ pub fn msToNextTask(self: *Env) ?u64 {
return if (next_task == std.math.maxInt(u64)) null else next_task;
}
pub fn pumpMessageLoop(self: *const Env) void {
pub fn pumpMessageLoop(self: *const Env) bool {
var hs: v8.HandleScope = undefined;
v8.v8__HandleScope__CONSTRUCT(&hs, self.isolate.handle);
defer v8.v8__HandleScope__DESTRUCT(&hs);
const isolate = self.isolate.handle;
const platform = self.platform.handle;
while (v8.v8__Platform__PumpMessageLoop(platform, isolate, false)) {}
var ran = false;
while (v8.v8__Platform__PumpMessageLoop(platform, isolate, false)) {
ran = true;
}
return ran;
}
pub fn hasBackgroundTasks(self: *const Env) bool {
+1 -1
View File
@@ -121,7 +121,7 @@ pub fn newCallback(
pub fn runMacrotasks(self: *const Local) void {
const env = self.ctx.env;
env.pumpMessageLoop();
_ = env.pumpMessageLoop();
env.runMicrotasks(); // macrotasks can cause microtasks to queue
}