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.
This commit is contained in:
Ben Buhse 2026-02-12 13:48:33 -06:00
commit b48032bbba
No known key found for this signature in database
GPG key ID: 7916ACFCD38FD0B4
2 changed files with 4 additions and 4 deletions

View file

@ -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;

View file

@ -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