perf: improve perf of fill and Screen.init

Use @memset for initializing and filling a Screen.
This commit is contained in:
Tim Culverhouse 2024-04-30 12:33:46 -05:00
parent 31b97f73d2
commit fc9f55d6fa
2 changed files with 15 additions and 9 deletions

View file

@ -34,7 +34,7 @@ cursor_shape: Cell.CursorShape = .default,
pub fn init(alloc: std.mem.Allocator, winsize: Winsize, unicode: *const Unicode) !Screen { pub fn init(alloc: std.mem.Allocator, winsize: Winsize, unicode: *const Unicode) !Screen {
const w = winsize.cols; const w = winsize.cols;
const h = winsize.rows; const h = winsize.rows;
var self = Screen{ const self = Screen{
.buf = try alloc.alloc(Cell, w * h), .buf = try alloc.alloc(Cell, w * h),
.width = w, .width = w,
.height = h, .height = h,
@ -42,9 +42,8 @@ pub fn init(alloc: std.mem.Allocator, winsize: Winsize, unicode: *const Unicode)
.height_pix = winsize.y_pixel, .height_pix = winsize.y_pixel,
.unicode = unicode, .unicode = unicode,
}; };
for (self.buf, 0..) |_, i| { const base_cell: Cell = .{};
self.buf[i] = .{}; @memset(self.buf, base_cell);
}
return self; return self;
} }
pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void { pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void {

View file

@ -206,11 +206,18 @@ pub fn gwidth(self: Window, str: []const u8) usize {
/// fills the window with the provided cell /// fills the window with the provided cell
pub fn fill(self: Window, cell: Cell) void { pub fn fill(self: Window, cell: Cell) void {
var row: usize = self.y_off; if (self.x_off == 0 and self.width == self.screen.width) {
while (row < (self.height + self.y_off)) : (row += 1) { // we have a full width window, therefore contiguous memory.
var col: usize = self.x_off; const start = self.y_off * self.width;
while (col < (self.width + self.x_off)) : (col += 1) { const end = start + (self.height * self.width);
self.screen.writeCell(col, row, cell); @memset(self.screen.buf[start..end], cell);
} else {
// Non-contiguous. Iterate over rows an memset
var row: usize = self.y_off;
while (row < (self.height + self.y_off)) : (row += 1) {
const start = self.x_off + (row * self.screen.width);
const end = start + self.width;
@memset(self.screen.buf[start..end], cell);
} }
} }
} }