fix: switch to a shared GraphemeCache and increase it's size to 512kb

This commit is contained in:
CJ van den Berg 2025-12-08 14:32:01 +01:00
parent 4aecc983c0
commit b6b1f02565
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 32 additions and 13 deletions

View file

@ -0,0 +1,23 @@
storage: *Storage,
pub const GraphemeCache = @This();
pub inline fn put(self: *@This(), bytes: []const u8) []u8 {
return self.storage.put(bytes);
}
pub const Storage = struct {
buf: [1024 * 512]u8 = undefined,
idx: usize = 0,
pub fn put(self: *@This(), bytes: []const u8) []u8 {
if (self.idx + bytes.len > self.buf.len) self.idx = 0;
defer self.idx += bytes.len;
@memcpy(self.buf[self.idx .. self.idx + bytes.len], bytes);
return self.buf[self.idx .. self.idx + bytes.len];
}
pub fn cache(self: *@This()) GraphemeCache {
return .{ .storage = self };
}
};

View file

@ -2,6 +2,7 @@ const std = @import("std");
const vaxis = @import("vaxis");
pub const Plane = @import("Plane.zig");
const GraphemeCache = @import("GraphemeCache.zig");
const Layer = @This();
@ -9,6 +10,7 @@ view: View,
y_off: i32 = 0,
x_off: i32 = 0,
plane_: Plane,
cache_storage: GraphemeCache.Storage = .{},
const View = struct {
allocator: std.mem.Allocator,
@ -57,6 +59,7 @@ pub fn init(allocator: std.mem.Allocator, opts: Options) std.mem.Allocator.Error
const name = "layer";
self.plane_ = .{
.window = self.window(),
.cache = self.cache_storage.cache(),
.name_buf = undefined,
.name_len = name.len,
};

View file

@ -8,6 +8,7 @@ const vaxis = @import("vaxis");
const Buffer = @import("Buffer");
const color = @import("color");
const RGB = @import("color").RGB;
const GraphemeCache = @import("GraphemeCache.zig");
const Plane = @This();
@ -18,7 +19,7 @@ row: i32 = 0,
col: i32 = 0,
name_buf: [name_buf_len]u8,
name_len: usize,
cache: GraphemeCache = .{},
cache: GraphemeCache,
style: vaxis.Cell.Style = .{},
style_base: vaxis.Cell.Style = .{},
scrolling: bool = false,
@ -49,6 +50,7 @@ pub fn init(nopts: *const Options, parent_: Plane) !Plane {
const len = @min(nopts.name.len, name_buf_len);
var plane: Plane = .{
.window = parent_.window.child(opts),
.cache = parent_.cache,
.name_buf = undefined,
.name_len = len,
.scrolling = nopts.flags == .VSCROLL,
@ -500,18 +502,6 @@ pub fn metrics(self: *const Plane, tab_width: usize) Buffer.Metrics {
};
}
const GraphemeCache = struct {
buf: [1024 * 32]u8 = undefined,
idx: usize = 0,
pub fn put(self: *GraphemeCache, bytes: []const u8) []u8 {
if (self.idx + bytes.len > self.buf.len) self.idx = 0;
defer self.idx += bytes.len;
@memcpy(self.buf[self.idx .. self.idx + bytes.len], bytes);
return self.buf[self.idx .. self.idx + bytes.len];
}
};
fn to_cell_color(col: ThemeColor) vaxis.Cell.Color {
return .{ .rgb = RGB.to_u8s(RGB.from_u24(col.color)) };
}

View file

@ -16,6 +16,7 @@ pub const CursorShape = vaxis.Cell.CursorShape;
pub const style = @import("style.zig").StyleBits;
pub const styles = @import("style.zig");
const GraphemeCache = @import("GraphemeCache.zig");
const Self = @This();
pub const log_name = "vaxis";
@ -25,6 +26,7 @@ allocator: std.mem.Allocator,
tty: vaxis.Tty,
vx: vaxis.Vaxis,
tty_buffer: []u8,
cache_storage: GraphemeCache.Storage = .{},
no_alternate: bool,
event_buffer: std.Io.Writer.Allocating,
@ -240,6 +242,7 @@ pub fn stdplane(self: *Self) Plane {
const name = "root";
var plane: Plane = .{
.window = self.vx.window(),
.cache = self.cache_storage.cache(),
.name_buf = undefined,
.name_len = name.len,
};