summaryrefslogtreecommitdiff
path: root/src/layout.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/layout.zig')
-rw-r--r--src/layout.zig66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/layout.zig b/src/layout.zig
index 3fb381b..c585d71 100644
--- a/src/layout.zig
+++ b/src/layout.zig
@@ -13,23 +13,28 @@
//! against a leaf's pattern at lookup time.
//!
//! What each kind of sketch memory may hold:
-//! - State is saved to disk and snapshotted: no pointers, and no host
-//! handles (images, sounds, buffers), which only mean something in the
-//! process that made them.
+//! - State is saved, the one thing a sketch keeps, in keyframes of its
+//! save: no pointers, and no host handles (images, sounds, buffers),
+//! which only mean something in the process that made them; at most
+//! `max_state` bytes.
//! - Session lives in this process: anything, as long as pointers go into
//! memory the host owns (its heaps, mapped files), never into the sketch
//! library, which a reload unmaps.
-//! - Store is a file mapped at the same address in every run: pointers into
-//! the store itself, but no host handles.
const std = @import("std");
const abi = @import("abi.zig");
+/// The most a State may be, in bytes: half of the 64 MiB a save holds, so
+/// history has room (Sessions.max_save).
+pub const max_state = 32 << 20;
+
pub fn of(comptime T: type, comptime memory: abi.Memory) abi.Layout {
return comptime blk: {
@setEvalBranchQuota(1_000_000);
switch (memory) {
- .state => check(T, "State", @typeName(T), false),
- .store => check(T, "Store", @typeName(T), true),
+ .state => {
+ check(T, @typeName(T));
+ if (@sizeOf(T) > max_state) @compileError(std.fmt.comptimePrint("State is {d} bytes, and may be {d} MiB, half of a save, so history has room: lower a capacity (an array, an sk.List), or keep what need not be saved in Session.", .{ @sizeOf(T), max_state >> 20 }));
+ },
.session => {},
}
const Leaves = struct {
@@ -101,7 +106,7 @@ fn leaf(comptime T: type, comptime path: []const u8, comptime offset: usize, com
};
const stored = dims[0..dims.len].*;
return .{
- .path = std.fmt.comptimePrint("{s}", .{path}),
+ .path = (path ++ "\x00")[0..path.len :0],
.offset = offset,
.size = @sizeOf(T),
.kind = kind,
@@ -155,17 +160,16 @@ pub fn resolve(fields: []const abi.Field, path: []const u8) ?Resolved {
return null;
}
-fn check(comptime T: type, comptime memory: []const u8, comptime path: []const u8, comptime pointers: bool) void {
+fn check(comptime T: type, comptime path: []const u8) void {
comptime {
- if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError(memory ++ " cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session.");
+ if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError("State cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session.");
switch (@typeInfo(T)) {
- // Not followed: types can point to themselves.
- .pointer => if (!pointers) @compileError(memory ++ " cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Store or Session."),
- .array => |a| check(a.child, memory, path ++ "[]", pointers),
- .vector => |v| check(v.child, memory, path ++ "[]", pointers),
- .optional => |o| check(o.child, memory, path, pointers),
- .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers),
- .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers),
+ .pointer => @compileError("State cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Session."),
+ .array => |a| check(a.child, path ++ "[]"),
+ .vector => |v| check(v.child, path ++ "[]"),
+ .optional => |o| check(o.child, path),
+ .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, path ++ "." ++ name),
+ .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, path ++ "." ++ name),
else => {},
}
}
@@ -176,9 +180,9 @@ fn tags(comptime E: type) [:0]const u8 {
var text: []const u8 = "";
const e = @typeInfo(E).@"enum";
for (e.field_names, e.field_values, 0..) |name, value, i| {
- text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ name, value });
+ text = text ++ (if (i == 0) "" else ",") ++ name ++ "=" ++ decimal(value);
}
- return std.fmt.comptimePrint("{s}", .{text});
+ return (text ++ "\x00")[0..text.len :0];
}
}
@@ -191,32 +195,40 @@ pub fn hash(comptime T: type) u64 {
};
}
+/// `n` in decimal, as `{d}` prints it: `std.fmt.comptimePrint` costs about a
+/// millisecond of comptime a call, and a signature makes hundreds.
+fn decimal(comptime n: comptime_int) []const u8 {
+ if (n < 0) return "-" ++ decimal(-n);
+ if (n < 10) return &[_]u8{'0' + n};
+ return decimal(n / 10) ++ &[_]u8{'0' + n % 10};
+}
+
pub fn signature(comptime T: type) []const u8 {
comptime {
return switch (@typeInfo(T)) {
- .int => |i| std.fmt.comptimePrint("{c}{d}", .{ if (i.signedness == .signed) 'i' else 'u', i.bits }),
- .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}),
+ .int => |i| (if (i.signedness == .signed) "i" else "u") ++ decimal(i.bits),
+ .float => |f| "f" ++ decimal(f.bits),
.bool => "b",
.void => "v",
.@"enum" => |e| "e(" ++ signature(e.tag_type) ++ ":" ++ tags(T) ++ ")",
- .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ signature(a.child),
- .vector => |v| std.fmt.comptimePrint("@{d}", .{v.len}) ++ signature(v.child),
+ .array => |a| "[" ++ decimal(a.len) ++ "]" ++ signature(a.child),
+ .vector => |v| "@" ++ decimal(v.len) ++ signature(v.child),
.optional => |o| "?" ++ signature(o.child),
.@"struct" => |s| blk: {
- var text: []const u8 = std.fmt.comptimePrint("s{d}{{", .{@sizeOf(T)});
+ var text: []const u8 = "s" ++ decimal(@sizeOf(T)) ++ "{";
for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| {
if (attrs.@"comptime") continue;
- text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ name, @offsetOf(T, name) }) ++ signature(F) ++ ";";
+ text = text ++ name ++ "@" ++ decimal(@offsetOf(T, name)) ++ ":" ++ signature(F) ++ ";";
}
break :blk text ++ "}";
},
.@"union" => |u| blk: {
- var text: []const u8 = std.fmt.comptimePrint("u{d}{{", .{@sizeOf(T)});
+ var text: []const u8 = "u" ++ decimal(@sizeOf(T)) ++ "{";
if (u.tag_type) |tag| text = text ++ signature(tag) ++ "|";
for (u.field_names, u.field_types) |name, F| text = text ++ name ++ ":" ++ signature(F) ++ ";";
break :blk text ++ "}";
},
- .pointer => |p| std.fmt.comptimePrint("*{s}:{s}", .{ @tagName(p.size), @typeName(p.child) }),
+ .pointer => |p| "*" ++ @tagName(p.size) ++ ":" ++ @typeName(p.child),
.@"opaque" => "opaque",
.@"fn" => @typeName(T),
else => @compileError("unsupported type in sketch memory: " ++ @typeName(T)),