summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 23:04:05 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit8ce27cfdbab6d576db36403afbcef9659cb6dbdc (patch)
treed9c05dac053b17761e0d139cf14a192d49b4a46d /src
parent1b59d3e8eca0037d5c9fcb412e7c63cbe508f4ac (diff)
downloadsketchbook-8ce27cfdbab6d576db36403afbcef9659cb6dbdc.tar.gz
sketchbook-8ce27cfdbab6d576db36403afbcef9659cb6dbdc.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/host/Host.zig26
-rw-r--r--src/host/library.zig27
2 files changed, 31 insertions, 22 deletions
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 {