From 5cc8b85624a18254790762d82336ceb40f30bf5f Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 25 Jun 2024 11:17:45 -0500 Subject: [PATCH] Use static storage for path_buf It is safe to re-use this buffer since the data in the buffer is duplicated in recursive calls to findTags. So there is no need to blow up our stack (which could be quite large with recursive invocations) by allocating this large array on each call. --- src/Tags.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Tags.zig b/src/Tags.zig index 4f10da9..913e295 100644 --- a/src/Tags.zig +++ b/src/Tags.zig @@ -81,6 +81,11 @@ allocator: std.mem.Allocator, entries: EntryList, visited: std.StringHashMap(void), +/// Static buffer for writing full paths. +/// +/// Data in this buffer is duplicated when passed to findTags. +var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + pub fn init(allocator: std.mem.Allocator) Tags { return Tags{ .allocator = allocator, @@ -141,8 +146,6 @@ pub fn findTags(self: *Tags, fname: []const u8) anyerror!void { var ast = try std.zig.Ast.parse(allocator, source, .zig); defer ast.deinit(allocator); - var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const tags = ast.nodes.items(.tag); const tokens = ast.nodes.items(.main_token); const datas = ast.nodes.items(.data);