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 {
const w = winsize.cols;
const h = winsize.rows;
var self = Screen{
const self = Screen{
.buf = try alloc.alloc(Cell, w * h),
.width = w,
.height = h,
@ -42,9 +42,8 @@ pub fn init(alloc: std.mem.Allocator, winsize: Winsize, unicode: *const Unicode)
.height_pix = winsize.y_pixel,
.unicode = unicode,
};
for (self.buf, 0..) |_, i| {
self.buf[i] = .{};
}
const base_cell: Cell = .{};
@memset(self.buf, base_cell);
return self;
}
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
pub fn fill(self: Window, cell: Cell) void {
if (self.x_off == 0 and self.width == self.screen.width) {
// we have a full width window, therefore contiguous memory.
const start = self.y_off * self.width;
const end = start + (self.height * self.width);
@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) {
var col: usize = self.x_off;
while (col < (self.width + self.x_off)) : (col += 1) {
self.screen.writeCell(col, row, cell);
const start = self.x_off + (row * self.screen.width);
const end = start + self.width;
@memset(self.screen.buf[start..end], cell);
}
}
}