diff --git a/src/build_config.zig b/src/build_config.zig index 1d7231978..57df93a83 100644 --- a/src/build_config.zig +++ b/src/build_config.zig @@ -104,6 +104,19 @@ pub const app_runtime: apprt.Runtime = config.app_runtime; pub const font_backend: font.Backend = config.font_backend; pub const renderer: rendererpkg.Impl = config.renderer; +/// True if we should have "slow" runtime safety checks. The initial motivation +/// for this was terminal page/pagelist integrity checks. These were VERY +/// slow but very thorough. But they made it so slow that the terminal couldn't +/// be used for real work. We'd love to have an option to run a build with +/// safety checks that could be used for real work. This lets us do that. +pub const slow_runtime_safety = std.debug.runtime_safety and switch (builtin.mode) { + .Debug => true, + .ReleaseSafe, + .ReleaseSmall, + .ReleaseFast, + => false, +}; + pub const Artifact = enum { /// Standalone executable exe, diff --git a/src/terminal/PageList.zig b/src/terminal/PageList.zig index 4cbe8f2ef..bbcc384f5 100644 --- a/src/terminal/PageList.zig +++ b/src/terminal/PageList.zig @@ -4,6 +4,7 @@ const PageList = @This(); const std = @import("std"); +const build_config = @import("../build_config.zig"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; const color = @import("color.zig"); @@ -1293,7 +1294,7 @@ fn resizeWithoutReflow(self: *PageList, opts: Resize) !void { }, } - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { assert(self.totalRows() >= self.rows); } } @@ -2301,7 +2302,7 @@ pub fn pin(self: *const PageList, pt: point.Point) ?Pin { /// pin points to is removed completely, the tracked pin will be updated /// to the top-left of the screen. pub fn trackPin(self: *PageList, p: Pin) Allocator.Error!*Pin { - if (comptime std.debug.runtime_safety) assert(self.pinIsValid(p)); + if (build_config.slow_runtime_safety) assert(self.pinIsValid(p)); // Create our tracked pin const tracked = try self.pool.pins.create(); @@ -2963,7 +2964,7 @@ pub fn pageIterator( else self.getBottomRight(tl_pt) orelse return .{ .row = null }; - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { assert(tl_pin.eql(bl_pin) or tl_pin.before(bl_pin)); } @@ -3279,7 +3280,7 @@ pub const Pin = struct { // Note: this is primarily unit tested as part of the Kitty // graphics deletion code. pub fn isBetween(self: Pin, top: Pin, bottom: Pin) bool { - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { if (top.page == bottom.page) { // If top is bottom, must be ordered. assert(top.y <= bottom.y); diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index bb0374e8e..8a9127dd6 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -1,6 +1,7 @@ const Screen = @This(); const std = @import("std"); +const build_config = @import("../build_config.zig"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; const ansi = @import("ansi.zig"); @@ -253,7 +254,7 @@ pub fn deinit(self: *Screen) void { /// tests. This only asserts the screen specific data so callers should /// ensure they're also calling page integrity checks if necessary. pub fn assertIntegrity(self: *const Screen) void { - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { assert(self.cursor.x < self.pages.cols); assert(self.cursor.y < self.pages.rows); @@ -714,7 +715,7 @@ pub fn cursorDownScroll(self: *Screen) !void { // These assertions help catch some pagelist math errors. Our // x/y should be unchanged after the grow. - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { const active = self.pages.pointFromPin( .active, page_pin, diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 0479846dc..12a056664 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1149,10 +1149,8 @@ pub fn index(self: *Terminal) !void { // up by 1, so we need to move it back down. A `cursorReload` // would be better option but this is more efficient and this is // a super hot path so we do this instead. - if (comptime std.debug.runtime_safety) { - assert(self.screen.cursor.x == old_cursor.x); - assert(self.screen.cursor.y == old_cursor.y); - } + assert(self.screen.cursor.x == old_cursor.x); + assert(self.screen.cursor.y == old_cursor.y); self.screen.cursor.y -= 1; self.screen.cursorDown(1); diff --git a/src/terminal/page.zig b/src/terminal/page.zig index 31b973e9b..d970ba3d1 100644 --- a/src/terminal/page.zig +++ b/src/terminal/page.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const build_config = @import("../build_config.zig"); const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const assert = std.debug.assert; @@ -181,8 +182,8 @@ pub const Page = struct { /// If this is true then verifyIntegrity will do nothing. This is /// only present with runtime safety enabled. - pause_integrity_checks: if (std.debug.runtime_safety) usize else void = - if (std.debug.runtime_safety) 0 else {}, + pause_integrity_checks: if (build_config.slow_runtime_safety) usize else void = + if (build_config.slow_runtime_safety) 0 else {}, /// Initialize a new page, allocating the required backing memory. /// The size of the initialized page defaults to the full capacity. @@ -305,7 +306,7 @@ pub const Page = struct { /// doing a lot of operations that would trigger integrity check /// violations but you know the page will end up in a consistent state. pub fn pauseIntegrityChecks(self: *Page, v: bool) void { - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { if (v) { self.pause_integrity_checks += 1; } else { @@ -318,9 +319,10 @@ pub const Page = struct { /// when runtime safety is enabled. This is a no-op when runtime /// safety is disabled. This uses the libc allocator. pub fn assertIntegrity(self: *const Page) void { - if (comptime std.debug.runtime_safety) { - self.verifyIntegrity(std.heap.c_allocator) catch unreachable; - } + self.verifyIntegrity(std.heap.c_allocator) catch |err| { + log.err("page integrity violation, crashing. err={}", .{err}); + @panic("page integrity violation"); + }; } /// Verifies the integrity of the page data. This is not fast, @@ -341,7 +343,7 @@ pub const Page = struct { // used for the same reason as styles above. // - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { if (self.pause_integrity_checks > 0) return; } @@ -737,7 +739,7 @@ pub const Page = struct { // This is an integrity check: if the row claims it doesn't // have managed memory then all cells must also not have // managed memory. - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { for (other_cells) |cell| { assert(!cell.hasGrapheme()); assert(!cell.hyperlink); @@ -764,7 +766,7 @@ pub const Page = struct { if (src_cell.hasGrapheme()) { // To prevent integrity checks flipping. This will // get fixed up when we check the style id below. - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { dst_cell.style_id = style.default_id; } @@ -895,7 +897,7 @@ pub const Page = struct { /// Get the cells for a row. pub fn getCells(self: *const Page, row: *Row) []Cell { - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { const rows = self.rows.ptr(self.memory); const cells = self.cells.ptr(self.memory); assert(@intFromPtr(row) >= @intFromPtr(rows)); @@ -1244,7 +1246,7 @@ pub const Page = struct { pub fn appendGrapheme(self: *Page, row: *Row, cell: *Cell, cp: u21) Allocator.Error!void { defer self.assertIntegrity(); - if (comptime std.debug.runtime_safety) assert(cell.codepoint() != 0); + if (build_config.slow_runtime_safety) assert(cell.codepoint() != 0); const cell_offset = getOffset(Cell, self.memory, cell); var map = self.grapheme_map.map(self.memory); @@ -1317,7 +1319,7 @@ pub const Page = struct { /// there are scenarios where we want to move graphemes without changing /// the content tag. Callers beware but assertIntegrity should catch this. fn moveGrapheme(self: *Page, src: *Cell, dst: *Cell) void { - if (comptime std.debug.runtime_safety) { + if (build_config.slow_runtime_safety) { assert(src.hasGrapheme()); assert(!dst.hasGrapheme()); } @@ -1334,7 +1336,7 @@ pub const Page = struct { /// Clear the graphemes for a given cell. pub fn clearGrapheme(self: *Page, row: *Row, cell: *Cell) void { defer self.assertIntegrity(); - if (comptime std.debug.runtime_safety) assert(cell.hasGrapheme()); + if (build_config.slow_runtime_safety) assert(cell.hasGrapheme()); // Get our entry in the map, which must exist const cell_offset = getOffset(Cell, self.memory, cell);