mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 06:23:45 +00:00
There are two layers here. The first is that, on startup, a v8 SnapshotCreator is created, and a snapshot-specific isolate/context is setup with our browser environment. This contains most of what was in Env.init and a good chunk of what was in ExecutionWorld.createContext. From this, we create a v8.StartupData which is used for the creation of all subsequent contexts. The snapshot sits at the application level, above the Env - it's re-used for all envs/isolates, so this gives a nice performance boost for both 1 connection opening multiple pages or multiple connections opening 1 page. The second layer is that the Snapshot data can be embedded into the binary, so that it doesn't have to be created on startup, but rather created at build-time. This improves the startup time (though, I'm not really sure how to measure that accurately...). The first layer is the big win (and just works as-is without any build / usage changes). with snapshot total runs 1000 total duration (ms) 7527 avg run duration (ms) 7 min run duration (ms) 5 max run duration (ms) 41 without snapshot total runs 1000 total duration (ms) 9350 avg run duration (ms) 9 min run duration (ms) 8 max run duration (ms) 42 To embed a snapshot into the binary, we first need to create the snapshot file: zig build -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin And then build using the new snapshot_path argument: zig build -Dsnapshot_path=../../snapshot.bin -Doptimize=ReleaseFast The paths are weird, I know...since it's embedded, it needs to be inside the project path, hence we put it in src/snapshot.bin. And since it's embedded relative to the embedder (src/browser/js/Snapshot.zig) the path has to be relative to that, hence ../../snapshot.bin. I'm open to suggestions on improving this.
48 lines
1.6 KiB
Zig
48 lines
1.6 KiB
Zig
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
|
|
//
|
|
// Francis Bouvier <francis@lightpanda.io>
|
|
// Pierre Tachoire <pierre@lightpanda.io>
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
const std = @import("std");
|
|
const lp = @import("lightpanda");
|
|
|
|
pub fn main() !void {
|
|
const allocator = std.heap.c_allocator;
|
|
|
|
var platform = try lp.js.Platform.init();
|
|
defer platform.deinit();
|
|
|
|
const snapshot = try lp.js.Snapshot.create(allocator);
|
|
defer snapshot.deinit(allocator);
|
|
|
|
var is_stdout = true;
|
|
var file = std.fs.File.stdout();
|
|
var args = try std.process.argsWithAllocator(allocator);
|
|
_ = args.next(); // executable name
|
|
if (args.next()) |n| {
|
|
is_stdout = false;
|
|
file = try std.fs.cwd().createFile(n, .{});
|
|
}
|
|
defer if (!is_stdout) {
|
|
file.close();
|
|
};
|
|
|
|
var buffer: [4096]u8 = undefined;
|
|
var writer = file.writer(&buffer);
|
|
try snapshot.write(&writer.interface);
|
|
try writer.end();
|
|
}
|