From 8ce27cfdbab6d576db36403afbcef9659cb6dbdc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:04:05 -0300 Subject: Load sketch libraries through a symlink and identify builds by stat A load copied the whole library to $XDG_RUNTIME_DIR (a read/write loop across filesystems) and then hashed all of it for the build id: 40 ms for a 10 MB Debug library, 200 ms for drat-meout's 58 MB. dlopen only needs a fresh name, so the host links one to an fd open on the library, which also pins the file a build may rename over meanwhile. The build id recordings carry is a hash of the file's inode, size and modification time: Zig's own linker writes no build-id note in Debug. --- src/host/Host.zig | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/host/Host.zig') diff --git a/src/host/Host.zig b/src/host/Host.zig index 035a004..70d6285 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -83,8 +83,9 @@ sketch: abi.Sketch, library: ?Library = null, watch: ?Watch = null, reloads: usize = 0, -/// Identifies the running build of the sketch (a hash of its library or -/// executable file): recordings carry it, and `play` warns on a mismatch. +/// Identifies the running build of the sketch (its library or executable +/// file's inode, size and modification time): recordings carry it, and +/// `play` warns on a mismatch. build: u64 = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, @@ -128,7 +129,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var library: ?Library = null; const sketch = linked orelse blk: { const path = options.library orelse return error.NoSketch; - library = try Library.openCopy(io, path, scratchDir(environ), 0); + library = try Library.openFresh(io, path, scratchDir(environ), 0); break :blk library.?.sketch; }; errdefer if (library) |*l| l.close(); @@ -205,7 +206,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std const size = options.size orelse [2]u32{ info.width, info.height }; self.setCanvas(size[0], size[1]); _ = c.SDL_StartTextInput(self.window); - self.build = fileHash(options.library orelse "/proc/self/exe"); + self.build = fileId(io, options.library orelse "/proc/self/exe"); try self.adopt(sketch); if (options.fresh) { self.state.header().used = 0; @@ -684,13 +685,13 @@ pub fn reload(self: *Host) !void { /// Replaces the running sketch with the library at `path`, keeping state. pub fn load(self: *Host, path: []const u8) !void { - var library = try Library.openCopy(self.io, path, scratchDir(self.environ), self.reloads + 1); + var library = try Library.openFresh(self.io, path, scratchDir(self.environ), self.reloads + 1); errdefer library.close(); try self.adopt(library.sketch); if (self.library) |*old| old.close(); self.library = library; self.reloads += 1; - self.build = fileHash(path); + self.build = fileId(self.io, path); self.print("reload {d}: {s}", .{ self.reloads, path }); } @@ -868,12 +869,13 @@ fn consoleKey(e: abi.Event) ?Console.Key { }; } -/// A hash of a file's bytes; 0 when it cannot be read. -fn fileHash(path: []const u8) u64 { - const bytes = memory.mapReadOnly(path); - if (bytes.len == 0) return 0; - defer std.posix.munmap(bytes); - return std.hash.Wyhash.hash(0, bytes) | 1; +/// Identifies the file at `path` without reading it (a Debug library is tens +/// of megabytes): a hash of its inode, size and modification time; 0 when +/// there is none. A build replaces the file, so each build gets a new one. +fn fileId(io: std.Io, path: []const u8) u64 { + const stat = std.Io.Dir.cwd().statFile(io, path, .{}) catch return 0; + const id = [3]u64{ stat.inode, stat.size, @truncate(@as(u96, @bitCast(stat.mtime.nanoseconds))) }; + return std.hash.Wyhash.hash(0, std.mem.asBytes(&id)) | 1; } fn scratchDir(environ: *const std.process.Environ.Map) []const u8 { -- cgit v1.3