terminal: disable slow safety tests in releasesafe

This commit is contained in:
Mitchell Hashimoto
2024-08-31 20:55:29 -07:00
parent f1aea10a84
commit 6aab430caf
5 changed files with 38 additions and 23 deletions

View File

@ -104,6 +104,19 @@ pub const app_runtime: apprt.Runtime = config.app_runtime;
pub const font_backend: font.Backend = config.font_backend; pub const font_backend: font.Backend = config.font_backend;
pub const renderer: rendererpkg.Impl = config.renderer; 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 { pub const Artifact = enum {
/// Standalone executable /// Standalone executable
exe, exe,

View File

@ -4,6 +4,7 @@
const PageList = @This(); const PageList = @This();
const std = @import("std"); const std = @import("std");
const build_config = @import("../build_config.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const color = @import("color.zig"); 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); 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 /// pin points to is removed completely, the tracked pin will be updated
/// to the top-left of the screen. /// to the top-left of the screen.
pub fn trackPin(self: *PageList, p: Pin) Allocator.Error!*Pin { 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 // Create our tracked pin
const tracked = try self.pool.pins.create(); const tracked = try self.pool.pins.create();
@ -2963,7 +2964,7 @@ pub fn pageIterator(
else else
self.getBottomRight(tl_pt) orelse return .{ .row = null }; 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)); 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 // Note: this is primarily unit tested as part of the Kitty
// graphics deletion code. // graphics deletion code.
pub fn isBetween(self: Pin, top: Pin, bottom: Pin) bool { 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.page == bottom.page) {
// If top is bottom, must be ordered. // If top is bottom, must be ordered.
assert(top.y <= bottom.y); assert(top.y <= bottom.y);

View File

@ -1,6 +1,7 @@
const Screen = @This(); const Screen = @This();
const std = @import("std"); const std = @import("std");
const build_config = @import("../build_config.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const ansi = @import("ansi.zig"); 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 /// tests. This only asserts the screen specific data so callers should
/// ensure they're also calling page integrity checks if necessary. /// ensure they're also calling page integrity checks if necessary.
pub fn assertIntegrity(self: *const Screen) void { 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.x < self.pages.cols);
assert(self.cursor.y < self.pages.rows); 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 // These assertions help catch some pagelist math errors. Our
// x/y should be unchanged after the grow. // 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( const active = self.pages.pointFromPin(
.active, .active,
page_pin, page_pin,

View File

@ -1149,10 +1149,8 @@ pub fn index(self: *Terminal) !void {
// up by 1, so we need to move it back down. A `cursorReload` // 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 // would be better option but this is more efficient and this is
// a super hot path so we do this instead. // 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.x == old_cursor.x); assert(self.screen.cursor.y == old_cursor.y);
assert(self.screen.cursor.y == old_cursor.y);
}
self.screen.cursor.y -= 1; self.screen.cursor.y -= 1;
self.screen.cursorDown(1); self.screen.cursorDown(1);

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const build_config = @import("../build_config.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
const assert = std.debug.assert; const assert = std.debug.assert;
@ -181,8 +182,8 @@ pub const Page = struct {
/// If this is true then verifyIntegrity will do nothing. This is /// If this is true then verifyIntegrity will do nothing. This is
/// only present with runtime safety enabled. /// only present with runtime safety enabled.
pause_integrity_checks: if (std.debug.runtime_safety) usize else void = pause_integrity_checks: if (build_config.slow_runtime_safety) usize else void =
if (std.debug.runtime_safety) 0 else {}, if (build_config.slow_runtime_safety) 0 else {},
/// Initialize a new page, allocating the required backing memory. /// Initialize a new page, allocating the required backing memory.
/// The size of the initialized page defaults to the full capacity. /// 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 /// doing a lot of operations that would trigger integrity check
/// violations but you know the page will end up in a consistent state. /// violations but you know the page will end up in a consistent state.
pub fn pauseIntegrityChecks(self: *Page, v: bool) void { pub fn pauseIntegrityChecks(self: *Page, v: bool) void {
if (comptime std.debug.runtime_safety) { if (build_config.slow_runtime_safety) {
if (v) { if (v) {
self.pause_integrity_checks += 1; self.pause_integrity_checks += 1;
} else { } else {
@ -318,9 +319,10 @@ pub const Page = struct {
/// when runtime safety is enabled. This is a no-op when runtime /// when runtime safety is enabled. This is a no-op when runtime
/// safety is disabled. This uses the libc allocator. /// safety is disabled. This uses the libc allocator.
pub fn assertIntegrity(self: *const Page) void { pub fn assertIntegrity(self: *const Page) void {
if (comptime std.debug.runtime_safety) { self.verifyIntegrity(std.heap.c_allocator) catch |err| {
self.verifyIntegrity(std.heap.c_allocator) catch unreachable; log.err("page integrity violation, crashing. err={}", .{err});
} @panic("page integrity violation");
};
} }
/// Verifies the integrity of the page data. This is not fast, /// 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. // 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; 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 // This is an integrity check: if the row claims it doesn't
// have managed memory then all cells must also not have // have managed memory then all cells must also not have
// managed memory. // managed memory.
if (comptime std.debug.runtime_safety) { if (build_config.slow_runtime_safety) {
for (other_cells) |cell| { for (other_cells) |cell| {
assert(!cell.hasGrapheme()); assert(!cell.hasGrapheme());
assert(!cell.hyperlink); assert(!cell.hyperlink);
@ -764,7 +766,7 @@ pub const Page = struct {
if (src_cell.hasGrapheme()) { if (src_cell.hasGrapheme()) {
// To prevent integrity checks flipping. This will // To prevent integrity checks flipping. This will
// get fixed up when we check the style id below. // 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; dst_cell.style_id = style.default_id;
} }
@ -895,7 +897,7 @@ pub const Page = struct {
/// Get the cells for a row. /// Get the cells for a row.
pub fn getCells(self: *const Page, row: *Row) []Cell { 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 rows = self.rows.ptr(self.memory);
const cells = self.cells.ptr(self.memory); const cells = self.cells.ptr(self.memory);
assert(@intFromPtr(row) >= @intFromPtr(rows)); 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 { pub fn appendGrapheme(self: *Page, row: *Row, cell: *Cell, cp: u21) Allocator.Error!void {
defer self.assertIntegrity(); 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); const cell_offset = getOffset(Cell, self.memory, cell);
var map = self.grapheme_map.map(self.memory); 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 /// there are scenarios where we want to move graphemes without changing
/// the content tag. Callers beware but assertIntegrity should catch this. /// the content tag. Callers beware but assertIntegrity should catch this.
fn moveGrapheme(self: *Page, src: *Cell, dst: *Cell) void { 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(src.hasGrapheme());
assert(!dst.hasGrapheme()); assert(!dst.hasGrapheme());
} }
@ -1334,7 +1336,7 @@ pub const Page = struct {
/// Clear the graphemes for a given cell. /// Clear the graphemes for a given cell.
pub fn clearGrapheme(self: *Page, row: *Row, cell: *Cell) void { pub fn clearGrapheme(self: *Page, row: *Row, cell: *Cell) void {
defer self.assertIntegrity(); 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 // Get our entry in the map, which must exist
const cell_offset = getOffset(Cell, self.memory, cell); const cell_offset = getOffset(Cell, self.memory, cell);