Skip to content

Commit d24489b

Browse files
committed
refactor(cmd): improve file handling and reduce allocations
- Use openFile instead of access for genesis file existence check - Replace allocPrint with bufPrint for genesis URL to avoid allocation - Use makeOpenPath to simplify directory creation logic - Remove unnecessary min cap on decompression buffer size - Add TODO for future path allocation improvements
1 parent 702847e commit d24489b

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/cmd.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,10 +1341,19 @@ fn ensureGenesis(
13411341
&.{ cfg.validator_dir, "genesis.bin" },
13421342
);
13431343
errdefer allocator.free(existing_path);
1344-
if (!std.meta.isError(std.fs.cwd().access(existing_path, .{}))) {
1344+
const maybe_genesis_file: ?std.fs.File = std.fs.cwd().openFile(
1345+
existing_path,
1346+
.{},
1347+
) catch |err| switch (err) {
1348+
error.FileNotFound => null,
1349+
else => return err,
1350+
};
1351+
if (maybe_genesis_file != null) {
13451352
logger.info().logf("Using existing genesis file: {s}", .{existing_path});
1353+
maybe_genesis_file.?.close();
13461354
return existing_path;
13471355
}
1356+
allocator.free(existing_path);
13481357

13491358
// Determine cluster for genesis
13501359
const cluster = try cfg.gossip.getCluster() orelse {

src/config.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ pub const Cmd = struct {
4343
.localnet => return error.MustProvideGenesisFileForLocalHost,
4444
} else return null;
4545

46-
std.fs.cwd().access(local_path, .{ .read = true }) catch {
46+
const genesis_file = std.fs.cwd().openFile(local_path, .{}) catch {
4747
return null;
4848
};
49+
genesis_file.close();
50+
51+
return local_path;
4952
}
5053

5154
/// Derives a path relative to validator_dir if the param equals the default value.
5255
/// This is used to allow paths like snapshot_dir and geyser.pipe_path to be relative
5356
/// to validator_dir when using their default values, while still allowing explicit
5457
/// overrides.
58+
/// TODO:
59+
/// - remove alloc by using fixed-size buffers for paths
60+
/// - https://github.com/orgs/Syndica/projects/2/views/10?pane=issue&itemId=156092227)
5561
pub fn derivePathFromValidatorDir(
5662
self: Cmd,
5763
allocator: std.mem.Allocator,

src/core/genesis_download.zig

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ pub fn downloadAndExtractGenesis(
3636
output_dir: []const u8,
3737
logger: Logger,
3838
) DownloadError![]const u8 {
39-
const genesis_url = try std.fmt.allocPrint(
40-
allocator,
39+
var genesis_url_buf: [std.fs.max_path_bytes]u8 = @splat(0);
40+
const genesis_url = try std.fmt.bufPrint(
41+
&genesis_url_buf,
4142
"{s}/{s}",
4243
.{ cluster_url, DEFAULT_GENESIS_ARCHIVE },
4344
);
44-
defer allocator.free(genesis_url);
4545

4646
logger.info().logf("Downloading genesis from {s}...", .{genesis_url});
4747
const archive_data = try downloadGenesisArchive(allocator, genesis_url, logger);
@@ -91,7 +91,7 @@ fn downloadGenesisArchive(
9191
/// Decompresses bz2 data using libbz2
9292
fn decompressBz2(allocator: Allocator, compressed_data: []const u8) DownloadError![]u8 {
9393
// Start with an estimate of 10x compression ratio
94-
var dest_len: u32 = @intCast(@min(compressed_data.len * 10, 500 * 1024 * 1024));
94+
var dest_len: u32 = @intCast(compressed_data.len * 10);
9595
var decompressed = allocator.alloc(u8, dest_len) catch return error.OutOfMemory;
9696
errdefer allocator.free(decompressed);
9797

@@ -135,13 +135,8 @@ fn extractGenesisFromTar(
135135
output_dir: []const u8,
136136
logger: Logger,
137137
) DownloadError![]const u8 {
138-
std.fs.cwd().makePath(output_dir) catch |err| {
139-
logger.err().logf("Failed to create output directory: {}", .{err});
140-
return error.TarExtractError;
141-
};
142-
143-
var dir = std.fs.cwd().openDir(output_dir, .{}) catch |err| {
144-
logger.err().logf("Failed to open output directory: {}", .{err});
138+
var dir = std.fs.cwd().makeOpenPath(output_dir, .{}) catch |err| {
139+
logger.err().logf("Failed to open/create output directory: {}", .{err});
145140
return error.TarExtractError;
146141
};
147142
defer dir.close();

0 commit comments

Comments
 (0)