Skip to content

Lightpanda: the headless browser designed for AI and automation

perf: add compiled script bytes to http cache

Karl Seguin823c3c515e62
diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zig
index 42be5547b..ddfed36fc 100644
--- a/src/browser/ScriptManager.zig
+++ b/src/browser/ScriptManager.zig
@@ -377,6 +377,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
consumed_preload = pre;
script.source = pre.source;
script.encoding = pre.encoding;
+ script.script_cache = pre.script_cache;
script.status = pre.status;
script.complete = true;
} else {
@@ -403,6 +404,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
script.source_arena = response.arena;
script.source = .{ .remote = response.body };
script.setContentType(response.content_type);
+ script.script_cache = response.script_cache;
script.status = response.status;
script.complete = true;
}
diff --git a/src/browser/ScriptManagerBase.zig b/src/browser/ScriptManagerBase.zig
index 60babdde3..c0d5fb85a 100644
--- a/src/browser/ScriptManagerBase.zig
+++ b/src/browser/ScriptManagerBase.zig
@@ -607,6 +607,9 @@ pub const Script = struct {
// The encoding a remote script's Content-Type charset names.
encoding: ?h5e.EncodingInfo = null,
+ // Set when the body is an http cache entry. Only classic scripts use it (for now).
+ script_cache: ?HttpClient.ScriptCache = null,
+
extra: Extra,
node: std.DoublyLinkedList.Node,
manager: *ScriptManagerBase,
@@ -792,6 +795,14 @@ pub const Script = struct {
var buffer: std.ArrayList(u8) = .empty;
try buffer.ensureTotalCapacityPrecise(self.sourceAllocator(), body_len);
self.source = .{ .remote = buffer };
+
+ if (transfer.script_cache) |sc| {
+ const allocator = self.sourceAllocator();
+ self.script_cache = .{
+ .key = try allocator.dupe(u8, sc.key),
+ .bytes = if (sc.bytes) |b| try allocator.dupe(u8, b) else null,
+ };
+ }
return .proceed;
}
@@ -987,7 +998,7 @@ pub const Script = struct {
switch (fe.kind) {
.javascript => {
const text = self.classicText(fe) catch break :blk false;
- _ = local.eval(text, url) catch break :blk false;
+ self.runClassic(local, text) catch break :blk false;
},
.module => {
// We don't care about waiting for the evaluation here.
@@ -1041,6 +1052,50 @@ pub const Script = struct {
});
}
+ // currently only classic scripts participate in script caching
+ fn runClassic(self: *Script, local: *const js.Local, text: []const u8) !void {
+ const sc = self.script_cache orelse {
+ // wasn't in the cache and isn't cacheable
+ return local.eval(text, self.url);
+ };
+
+ if (text.ptr != self.source.content().ptr) {
+ // Cacheable from a pure HTTP point of view, but classicText()
+ // decoded the content (it wasn't UTF-8). You might think: the
+ // cached body will go through the same classicText() next time, so
+ // wouldn't bytes we generate now still apply? In most cases, yes.
+ // But when the response doesn't name its charset (BOM or
+ // Content-Type), classicText() falls back to <script charset="...">
+ // and then the document's encoding. If either differs next time,
+ // the text changes without the cache entry being invalidated.
+ // That's unlikely, but treating the script as uncacheable avoids
+ // the complexity for a rare edge case.
+ return local.eval(text, self.url);
+ }
+
+ const result = try local.compileWithCache(text, self.url, sc.bytes);
+ _ = try result.script.run();
+
+ if (sc.bytes != null) {
+ log.debug(.browser, "script cache", .{ .url = self.url, .rejected = result.cache_rejected });
+ if (result.cache_rejected == false) {
+ // We already have cached bytes and v8 didn't rectect them.
+ // Nothing else to do
+ return;
+ }
+ }
+
+ // Created after the run so it includes the functions compiled lazily
+ // while running.
+ const bytes = result.script.getUnboundScript().createCodeCache(self.sourceAllocator()) catch |err| {
+ log.debug(.browser, "create script cache", .{ .url = self.url, .err = err });
+ return;
+ };
+ self.manager.client.cache.putScriptCache(sc.key, text, bytes) catch |err| {
+ log.warn(.cache, "put script cache", .{ .url = self.url, .err = err });
+ };
+ }
+
// Frame-only: fires load/error on the <script> element itself,
// synchronously. Hint <link> events go through queueHintEvent instead.
pub fn executeCallback(self: *const Script, typ: String) void {
diff --git a/src/browser/js/Script.zig b/src/browser/js/Script.zig
index 8c0d3fd85..b3887f341 100644
--- a/src/browser/js/Script.zig
+++ b/src/browser/js/Script.zig
@@ -63,7 +63,7 @@ pub const Unbound = struct {
// Serialize the script. The returned bytes can be persisted and passed back
// into local.compileWithCache
- fn createCodeCache(self: Unbound, allocator: Allocator) ![]u8 {
+ pub fn createCodeCache(self: Unbound, allocator: Allocator) ![]u8 {
const cached = v8.v8__ScriptCompiler__CreateCodeCache(self.handle) orelse return error.CodeCacheFailed;
defer v8.v8__ScriptCompiler__CachedData__DELETE(cached);
const len: usize = @intCast(cached.*.length);
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 3c7c5446c..b3cc57ae2 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -1281,6 +1281,7 @@ pub fn findHeader(headers: []const http.Header, name: []const u8) ?[]const u8 {
// the response; on an expired-with-validators entry the request becomes a
// conditional revalidation.
fn cacheLookup(self: *Client, transfer: *Transfer) !bool {
+ transfer.script_cache = null;
const cache = self.cache.active() orelse return false;
const req = &transfer.req;
@@ -1441,7 +1442,9 @@ fn cacheStore(self: *Client, transfer: *Transfer) void {
}
cache.put(req, transfer.res.buffer.items) catch |err| {
log.warn(.http, "cache put failed", .{ .err = err });
+ return;
};
+ transfer.script_cache = .{ .key = transfer._cache_key, .bytes = null };
}
pub const SyncOptions = struct {
@@ -1465,6 +1468,7 @@ const SyncContext = struct {
body: std.ArrayList(u8),
headers: std.ArrayList(http.Header) = .empty,
content_type: ?[]const u8 = null,
+ script_cache: ?ScriptCache = null,
// Acquired on the first byte we have to buffer.
// Ownership moves to the SyncResponse.
@@ -1490,6 +1494,15 @@ const SyncContext = struct {
self.content_type = try allocator.dupe(u8, content_type);
}
+ if (transfer.req.resource_type == .script) {
+ if (transfer.script_cache) |sc| {
+ self.script_cache = .{
+ .key = try allocator.dupe(u8, sc.key),
+ .bytes = if (sc.bytes) |b| try allocator.dupe(u8, b) else null,
+ };
+ }
+ }
+
if (self.options.copy_headers) {
for (transfer.responseHeaders()) |hdr| {
try self.headers.append(allocator, .{
@@ -2267,6 +2280,7 @@ const SyncResponse = struct {
status_text: http.StatusText,
headers: []const http.Header, // only captured when copy_headers = true
content_type: ?[]const u8, // always captured (most callers need it)
+ script_cache: ?ScriptCache, // see Transfer.script_cache
arena: ?*lp.Arena, // need only if there's a body, or we're copying the headers
pub fn deinit(self: *SyncResponse) void {
@@ -2672,6 +2686,10 @@ pub const Transfer = struct {
// What the cache wants from this transfer's response.
_cache_intent: CacheIntent = .none,
+ // Set when the response body is the http cache entry under `key`, so a
+ // script consumer can use, and store, V8's compiled code for it.
+ script_cache: ?ScriptCache = null,
+
// What a response-stage CDP pause (.intercept_response) is holding, and so
// what releasing it unchanged does: deliver the response, follow the
// redirect, or fail with the network error.
@@ -2964,6 +2982,7 @@ pub const Transfer = struct {
.body = sync_ctx.body,
.headers = sync_ctx.headers.items,
.content_type = sync_ctx.content_type,
+ .script_cache = sync_ctx.script_cache,
.arena = sync_ctx.arena,
},
.err => |e| return e,
@@ -3637,6 +3656,7 @@ pub const Transfer = struct {
self.setResponseHead(cached.status, cached.content_type);
self.setResponseHeaders(cached.headers);
self._from_cache = true;
+ self.script_cache = .{ .key = self._cache_key, .bytes = cached.script_cache };
self._timing.cache = cache_state;
self._content_length = body.len;
// A revalidated entry keeps the size of the 304 that crossed the wire.
@@ -3672,6 +3692,7 @@ pub const Transfer = struct {
self.setResponseHeaders(headers);
// It may replace a cached response paused at the response stage.
self._from_cache = false;
+ self.script_cache = null;
self._content_length = owned_body.len;
self._encoded_data_length = owned_body.len;
try self.bufferEvents(owned_body);
@@ -4750,6 +4771,15 @@ const CacheIntent = union(enum) {
revalidate: Cache.CachedResponse,
};
+pub const ScriptCache = struct {
+ // any update to the cache is aimed here
+ key: []const u8,
+
+ // existing cached script bytes. When null, ScriptManagerBase will
+ // potentially create it and then use `key` to update the database
+ bytes: ?[]const u8,
+};
+
// Non-network URL schemes whose response is synthesized in-process rather than
// fetched, think blob data URLs.
const Synthetic = struct {
diff --git a/src/network/cache/Cache.zig b/src/network/cache/Cache.zig
index b136ac8f7..02523cf15 100644
--- a/src/network/cache/Cache.zig
+++ b/src/network/cache/Cache.zig
@@ -87,6 +87,13 @@ pub fn put(self: *Cache, req: CachePutRequest, body: []const u8) !void {
};
}
+pub fn putScriptCache(self: *Cache, url: []const u8, body: []const u8, bytes: []const u8) !void {
+ return switch (self.kind) {
+ .noop => {},
+ inline else => |*c| c.putScriptCache(url, body, bytes),
+ };
+}
+
pub fn evict(self: *Cache, url: []const u8) void {
return switch (self.kind) {
.noop => {},
@@ -289,6 +296,7 @@ pub const CachedResponse = struct {
etag: ?[]const u8,
last_modified: ?[]const u8,
headers: []const Http.Header,
+ script_cache: ?[]const u8 = null,
data: CachedData,
pub fn format(self: *const CachedResponse, writer: *std.Io.Writer) !void {
diff --git a/src/network/cache/SqliteCache.zig b/src/network/cache/SqliteCache.zig
index 66c424ea6..b111cf343 100644
--- a/src/network/cache/SqliteCache.zig
+++ b/src/network/cache/SqliteCache.zig
@@ -41,7 +41,7 @@ entry_limit: u32,
// When the schema changes, change this value, and the DB will be deleted and
// re-created with the schema
-const schema_version = 1;
+const schema_version = 2;
// body stays last so that reading columns before it hopefully doesn't visit
// overflow pages.
@@ -58,6 +58,7 @@ const schema = [_][:0]const u8{
\\ last_modified text,
\\ vary_headers blob not null,
\\ headers blob not null,
+ \\ script_cache blob,
\\ body blob not null
\\ ) strict
,
@@ -231,7 +232,8 @@ pub fn get(self: *SqliteCache, arena: std.mem.Allocator, req: CacheGetRequest) !
fn readEntry(conn: Sqlite.Conn, arena: std.mem.Allocator, req: CacheGetRequest) !CacheGetResult {
var entry = try conn.row(
\\ select status, stored_at, age_at_store, max_age, must_revalidate,
- \\ content_type, etag, last_modified, vary_headers, headers, body
+ \\ content_type, etag, last_modified, vary_headers, headers,
+ \\ script_cache, body
\\ from cache where url = $1
, .{req.url}) orelse {
log.debug(.cache, "miss", .{ .url = req.url, .reason = "missing" });
@@ -297,7 +299,8 @@ fn readEntry(conn: Sqlite.Conn, arena: std.mem.Allocator, req: CacheGetRequest)
.content_type = try arena.dupe(u8, raw_content_type),
.etag = if (raw_etag) |v| try arena.dupe(u8, v) else null,
.last_modified = if (raw_last_modified) |v| try arena.dupe(u8, v) else null,
- .data = .{ .buffer = try arena.dupe(u8, entry.get(Blob, 10).data) },
+ .script_cache = if (entry.get(?Blob, 10)) |b| try arena.dupe(u8, b.data) else null,
+ .data = .{ .buffer = try arena.dupe(u8, entry.get(Blob, 11).data) },
};
return if (expired) .{ .revalidate = resp } else .{ .hit = resp };
}
@@ -342,6 +345,20 @@ pub fn put(self: *SqliteCache, req: CachePutRequest, body: []const u8) !void {
log.debug(.cache, "put", .{ .url = req.url, .body_len = body.len });
}
+pub fn putScriptCache(self: *SqliteCache, url: []const u8, body: []const u8, bytes: []const u8) !void {
+ const conn = try self.pool.acquire();
+ defer self.pool.release(conn);
+
+ // We match on the body to make sure we're setting the script_cache for
+ // the right body. It is possible, though unlikely, that this row has been
+ // updated since we read it with a new body. If so, our cached script would
+ // no longer be valid.
+ try conn.exec(
+ "update cache set script_cache = $1 where url = $2 and body = $3",
+ .{ Blob{ .data = bytes }, url, Blob{ .data = body } },
+ );
+}
+
pub fn clear(self: *SqliteCache) !void {
const conn = try self.pool.acquire();
defer self.pool.release(conn);
@@ -1017,6 +1034,47 @@ test "SqliteCache: renew preserves body" {
try testing.expectEqual("original body", result.hit.data.buffer);
}
+test "SqliteCache: script cache is kept by renew and cleared by put" {
+ var cache = try setupCache(testing.allocator);
+ defer cache.deinit();
+
+ const now: u64 = @intCast(std.Io.Timestamp.now(testing.io, .boot).toSeconds());
+ const req: CachePutRequest = .{
+ .url = "https://example.com/a.js",
+ .content_type = "text/javascript",
+ .status = 200,
+ .stored_at = now,
+ .age_at_store = 0,
+ .cache_control = .{ .max_age = 600 },
+ .headers = &.{},
+ .vary_headers = &.{},
+ };
+ try cache.put(req, "var a = 1;");
+ try testing.expectEqual(null, try getScriptCache(&cache, now));
+
+ // Not written when the stored body isn't the one the bytes came from.
+ try cache.putScriptCache(req.url, "var a = 2;", "stale");
+ try testing.expectEqual(null, try getScriptCache(&cache, now));
+
+ try cache.putScriptCache(req.url, "var a = 1;", "compiled");
+ try testing.expectEqual("compiled", (try getScriptCache(&cache, now)).?);
+
+ try cache.renew(testing.arena_allocator, .{ .url = req.url, .timestamp = now + 100, .headers = &.{} });
+ try testing.expectEqual("compiled", (try getScriptCache(&cache, now + 100)).?);
+
+ try cache.put(req, "var a = 1;");
+ try testing.expectEqual(null, try getScriptCache(&cache, now));
+}
+
+fn getScriptCache(cache: *Cache, now: u64) !?[]const u8 {
+ const result = try cache.get(testing.arena_allocator, .{
+ .url = "https://example.com/a.js",
+ .timestamp = now,
+ .request_headers = &.{},
+ });
+ return result.hit.script_cache;
+}
+
test "SqliteCache: renew merges the 304's headers into the stored ones" {
var cache = try setupCache(testing.allocator);
defer cache.deinit();