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. --- README.md | 10 ++++++---- src/host/Host.zig | 26 ++++++++++++++------------ src/host/library.zig | 27 +++++++++++++++++---------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3a4384a..626a4e4 100644 --- a/README.md +++ b/README.md @@ -596,7 +596,8 @@ object. ## The host - **Hot reload.** It watches the library file and loads each new build through - a private copy. + a symlink with a fresh name (a build must replace the file, as `zig build` + does, not rewrite it in place). - **State file.** `.state` holds the live state plus 7 snapshot slots, shared via `MAP_SHARED`. Its header carries the reflected layout, so a layout change migrates every slot field by field, with enums matched by tag @@ -619,9 +620,10 @@ object. button and key state, and a recording begins with events that bring back what was held when it started (a pointer move, a button or key down), so a replay starts from the same input whatever the live pointer did since. - A recording carries a hash of the build that made it; `play` warns when - the running build differs (edited `update`, a new compiler), since the - replay may then diverge. + A recording carries an identity of the build that made it (its file's + inode, size and modification time); `play` warns when the running build + differs (edited `update`, a new compiler), since the replay may then + diverge. - **Console and overlays.** `get`/`set ` (`get` of a vector, array or struct shows its values, in memory order), snapshots, recordings and the memory, scope and frame timing overlays come from the host; unknown lines go to the 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 { diff --git a/src/host/library.zig b/src/host/library.zig index f8bc4e8..91b2296 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -1,9 +1,12 @@ //! Loading sketch libraries, and noticing when a build replaces one. //! //! `dlopen` returns the handle it already has for a path it loaded before, so -//! every load goes through a private copy with a fresh name. The copy is -//! deleted right after loading: the mapping keeps the code alive, and nothing -//! is left behind if the host dies. +//! every load goes through a symlink with a fresh name to an fd open on the +//! library: what loads is the file that was opened, whatever a build renames +//! over it meanwhile. The link is deleted right after loading: the mapping +//! keeps the code alive, and nothing is left behind if the host dies. A +//! library rewritten in place instead (same inode) would not load again, and +//! would change the code under the running host. const std = @import("std"); const linux = std.os.linux; const c = @import("c"); @@ -40,14 +43,18 @@ pub const Library = struct { return .{ .object = object, .sketch = sketch }; } - /// Loads `source` through a uniquely named copy in `scratch_dir`. - pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + /// Loads `source` through a uniquely named symlink in `scratch_dir`. + pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { const cwd = std.Io.Dir.cwd(); - try cwd.copyFile(source, cwd, copy, io, .{}); - defer cwd.deleteFile(io, copy) catch {}; - return open(copy, source); + const file = try cwd.openFile(io, source, .{}); + defer file.close(io); + var target_buf: [32]u8 = undefined; + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{file.handle}); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const link = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + try cwd.symLink(io, target, link, .{}); + defer cwd.deleteFile(io, link) catch {}; + return open(link, source); } pub fn close(self: *Library) void { -- cgit v1.3