diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:00:19 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 5b8cf6f66e367192090350c9129fa341edfc9343 (patch) | |
| tree | 48a5e6229aca31c4a44473343d47fb7fcc63a32f | |
| parent | 88c04dd6c392c6d8fc3a02408506e85d86f476d5 (diff) | |
| download | sketchbook-5b8cf6f66e367192090350c9129fa341edfc9343.tar.gz sketchbook-5b8cf6f66e367192090350c9129fa341edfc9343.zip | |
Compute abi.hash's layout half once per abi change, not in every compile
The comptime walk over the shared types cost about half a second of every
sketch library's compile. abi_hash.zig runs it at build time, once per
change of the abi sources (watched like any input), and build.zig hands the
number to host and sketches as the module abi_hash; abi.hash folds in the
build as before. The value is the walk's own, computed for the build
machine in Debug.
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | build.zig | 9 | ||||
| -rw-r--r-- | src/abi.zig | 17 | ||||
| -rw-r--r-- | src/abi_hash.zig | 9 |
4 files changed, 38 insertions, 13 deletions
@@ -873,15 +873,19 @@ renderer migration work and the optional addon boundary. ## The boundary Host and sketch are built by the same `zig build`, so ordinary Zig types cross -it (slices, tagged unions, `std.mem.Allocator`). `abi.hash`, a comptime hash -of every shared type's layout and of the zig version, backend and optimize +it (slices, tagged unions, `std.mem.Allocator`). `abi.hash`, a hash of +every shared type's layout and of the zig version, backend and optimize mode, guards against the real hazards: a host refuses a library built against different definitions, or by another compiler or mode (`zig build run` beside `zig build lib --watch -Doptimize=fast`: Zig's own calling -convention differs between them), and says what differs. Everything that outlives -a call lives in host memory, since host code is never unloaded. Only the -recorded `Event` and the reflected `Layout` are `extern`, because they are -written to disk. +convention differs between them), and says what differs. The layout half +costs half a second of comptime, so a small program (`src/abi_hash.zig`) +computes it once per change of the abi sources, at build time, and host and +sketches import the number; it is computed for the build machine in Debug, +so a layout change seen only by another target or mode is not caught. +Everything that outlives a call lives in host memory, since host code is +never unloaded. Only the recorded `Event` and the reflected `Layout` are +`extern`, because they are written to disk. `zig build test` in this directory runs the unit tests, a headless reload through `dlopen`, where a counter sketch changes its layout mid-run and keeps @@ -44,7 +44,14 @@ pub fn build(b: *Build) void { b.installArtifact(gltf_generator); // Target-free: they compile for whatever imports them, native or wasm. - const abi = b.addModule("abi", .{ .root_source_file = b.path("src/abi.zig") }); + // abi.hash's layout half, computed by a program once per change of the + // abi sources rather than at comptime in every sketch's compile. + const abi_hash = b.addRunArtifact(b.addExecutable(.{ .name = "abi-hash", .root_module = b.createModule(.{ + .root_source_file = b.path("src/abi_hash.zig"), + .target = b.graph.host, + .imports = &.{.{ .name = "abi", .module = b.createModule(.{ .root_source_file = b.path("src/abi.zig") }) }}, + }) })).captureStdOut(.{ .basename = "abi_hash.zig" }); + const abi = b.addModule("abi", .{ .root_source_file = b.path("src/abi.zig"), .imports = &.{.{ .name = "abi_hash", .module = b.createModule(.{ .root_source_file = abi_hash }) }} }); const gpu = b.addModule("gpu", .{ .root_source_file = b.path("src/gpu/Executor.zig"), .imports = &.{.{ .name = "abi", .module = abi }} }); _ = b.addModule("gpu_scene", .{ .root_source_file = b.path("src/gpu/scene.zig"), .imports = &.{.{ .name = "abi", .module = abi }} }); // Embedders provide their existing SDL translation and device. diff --git a/src/abi.zig b/src/abi.zig index 33f7cd6..9154539 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -815,17 +815,22 @@ pub const Sketch = struct { }; }; -/// Layout hash of everything above, and of `build`. Changes whenever a shared -/// type does, or the compiler, backend or optimize mode: Zig's own calling -/// convention, which `Host.VTable` and every `std.mem.Allocator` use, differs -/// between backends and modes. -pub const hash: u64 = blk: { +/// `layout_hash` and `build`: changes whenever a shared type does, or the +/// compiler, backend or optimize mode, since Zig's own calling convention +/// (`Host.VTable`, every `std.mem.Allocator`) differs between them. +/// `layout_hash` costs half a second of comptime, so build.zig has +/// `abi_hash.zig` compute it once per change of these files and hands it +/// over as the module `abi_hash`, instead of every sketch's compile doing it. +pub const hash: u64 = std.hash.Wyhash.hash(@import("abi_hash").layout, build); + +/// Hash of every shared type's name and layout. +pub const layout_hash: u64 = blk: { @setEvalBranchQuota(1_000_000); var h: u64 = 0; for (.{ Rect, Color, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { h = std.hash.Wyhash.hash(h, @typeName(T) ++ layout.signature(T)); } - break :blk std.hash.Wyhash.hash(h, build); + break :blk h; }; /// The compiler, backend and mode of this build, exported by libraries as diff --git a/src/abi_hash.zig b/src/abi_hash.zig new file mode 100644 index 0000000..9cd01cf --- /dev/null +++ b/src/abi_hash.zig @@ -0,0 +1,9 @@ +//! Prints `abi.layout_hash` as Zig source: build.zig runs this once per change +//! of the abi sources and hands the result to host and sketches as the module +//! `abi_hash`. +const std = @import("std"); +const abi = @import("abi"); + +pub fn main(init: std.process.Init) !void { + try std.Io.File.stdout().writeStreamingAll(init.io, std.fmt.comptimePrint("pub const layout: u64 = 0x{x};\n", .{abi.layout_hash})); +} |
