Skip to content

Commit 5cc8b85

Browse files
committed
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.
1 parent 5a63610 commit 5cc8b85

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Tags.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ allocator: std.mem.Allocator,
8181
entries: EntryList,
8282
visited: std.StringHashMap(void),
8383

84+
/// Static buffer for writing full paths.
85+
///
86+
/// Data in this buffer is duplicated when passed to findTags.
87+
var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
88+
8489
pub fn init(allocator: std.mem.Allocator) Tags {
8590
return Tags{
8691
.allocator = allocator,
@@ -141,8 +146,6 @@ pub fn findTags(self: *Tags, fname: []const u8) anyerror!void {
141146
var ast = try std.zig.Ast.parse(allocator, source, .zig);
142147
defer ast.deinit(allocator);
143148

144-
var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
145-
146149
const tags = ast.nodes.items(.tag);
147150
const tokens = ast.nodes.items(.main_token);
148151
const datas = ast.nodes.items(.data);

0 commit comments

Comments
 (0)