From b48032bbba1c3214b776e873296cfeaac8462dfd Mon Sep 17 00:00:00 2001 From: Ben Buhse Date: Thu, 12 Feb 2026 13:48:33 -0600 Subject: [PATCH] Change two uses of alloc to use stack buffers I was looking for places where it might have made sense to use something like an arena or a fba instead of the C allocator, but almost all of the allocations are for Wayland interfaces that are fairly long lived (and, since they're using libwayland, need the C allocator). Instead, I just found two places that could use buffers on the stack instead. In Config.zig, we *were* allocating the config path with fmt.AllocPrint, but std.fs.max_path_bytes exists, so we can just make a buf of that size and save a heap allocation. This is only at start up and on config reload so it doesn't do too much, but I'd like to remove allocations when possible. The other change is for utils.parseModifiers(). It was using std.ascii.allocLowerString(), but we clamp the length of the string to 3-5 characters, so we can just make a 5 character buffer and then use ascii.lowerString() instead. Again, not super helpful since the function is (currently) only called when creating Configs, but it's still nice to get rid of a heap alloc. --- src/Config.zig | 4 ++-- src/utils.zig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Config.zig b/src/Config.zig index 2b6a2b1..8ac3d59 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -143,8 +143,8 @@ pub fn create() !*Config { if (try known_folders.getPath(utils.gpa, .local_configuration)) |config_dir| blk: { defer utils.gpa.free(config_dir); - const config_path = try std.fmt.allocPrint(utils.gpa, "{s}/{s}", .{ config_dir, CONFIG_FILE }); - defer utils.gpa.free(config_path); + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const config_path = std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ config_dir, CONFIG_FILE }) catch return config; const file = fs.openFileAbsolute(config_path, .{}) catch break :blk; diff --git a/src/utils.zig b/src/utils.zig index d53a126..7691461 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -78,8 +78,8 @@ pub fn parseModifiers(s: []const u8) !?river.SeatV1.Modifiers { if (part.len < 3 or part.len > 5) return null; // Case-insensitive comparison by lowercasing - const lower = try std.ascii.allocLowerString(utils.gpa, part); - defer utils.gpa.free(lower); + var lower_buf: [5]u8 = undefined; + const lower = std.ascii.lowerString(lower_buf[0..part.len], part); if (mem.eql(u8, lower, "none")) { // No modifier bits to set