From 73581eee0b13f8dd50ffb606dc01138f434091bf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 1 Sep 2022 18:36:27 -0700 Subject: [PATCH] small optimizations --- src/Window.zig | 11 +++++++---- src/terminal/Screen.zig | 13 +++++++------ src/terminal/stream.zig | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Window.zig b/src/Window.zig index 6b1674dcf..076826e73 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -1291,11 +1291,13 @@ fn ttyRead(t: *libuv.Tty, n: isize, buf: []const u8) void { // C0 execute, let our stream handle this one but otherwise // continue since we're guaranteed to be back in ground. - .execute => win.terminal_stream.next(c) catch |err| + .execute => win.terminal_stream.execute(c) catch |err| log.err("error processing terminal data: {}", .{err}), // Otherwise, break out and go the slow path until we're - // back in ground. + // back in ground. There is a slight optimization here where + // could try to find the next transition to ground but when + // I implemented that it didn't materially change performance. else => break, } @@ -1304,7 +1306,6 @@ fn ttyRead(t: *libuv.Tty, n: isize, buf: []const u8) void { } if (i < end) { - //log.warn("SLOW={}", .{end - i}); win.terminal_stream.nextSlice(buf[i..end]) catch |err| log.err("error processing terminal data: {}", .{err}); } @@ -1418,7 +1419,9 @@ pub fn horizontalTab(self: *Window) !void { } pub fn linefeed(self: *Window) !void { - try self.terminal.linefeed(); + // Small optimization: call index instead of linefeed because they're + // identical and this avoids one layer of function call overhead. + try self.terminal.index(); } pub fn carriageReturn(self: *Window) !void { diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index 169d9171c..74dc614cb 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -319,20 +319,21 @@ pub const RowIndexTag = enum { /// The max length for a given tag. This is a length, not an index, /// so it is 1-indexed. If the value is zero, it means that this /// section of the screen is empty or disabled. - pub fn maxLen(self: RowIndexTag, screen: *const Screen) usize { - const rows_written = screen.rowsWritten(); - + pub inline fn maxLen(self: RowIndexTag, screen: *const Screen) usize { return switch (self) { // Screen can be any of the written rows - .screen => rows_written, + .screen => screen.rowsWritten(), // Viewport can be any of the written rows or the max size // of a viewport. - .viewport => @minimum(screen.rows, rows_written), + .viewport => @minimum(screen.rows, screen.rowsWritten()), // History is all the way up to the top of our active area. If // we haven't filled our active area, there is no history. - .history => if (rows_written > screen.rows) rows_written - screen.rows else 0, + .history => history: { + const rows_written = screen.rowsWritten(); + break :history if (rows_written > screen.rows) rows_written - screen.rows else 0; + }, // Active area can be any number of rows. We ignore rows // written here because this is the only row index that can diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index b8494c8a5..e554c59fe 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -68,7 +68,7 @@ pub fn Stream(comptime Handler: type) type { } } - fn execute(self: *Self, c: u8) !void { + pub fn execute(self: *Self, c: u8) !void { // log.warn("C0: {}", .{c}); switch (@intToEnum(ansi.C0, c)) { .NUL => {},