Skip to content

Commit

Permalink
Use static storage for path_buf
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gpanders committed Jun 25, 2024
1 parent 5a63610 commit 5cc8b85
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Tags.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5cc8b85

Please # to comment.