Skip to content

Commit 78ce6b4

Browse files
committed
Use less allocations
1 parent a059519 commit 78ce6b4

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = "zigd",
3-
.version = "0.0.6",
3+
.version = "0.0.7",
44
.minimum_zig_version = "0.13.0",
55
.paths = .{
66
"build.zig",

src/utils.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,17 @@ pub fn exec(allocator: std.mem.Allocator, zig_binary: []const u8, args: [][:0]u8
9999
var proc = std.process.Child.init(argv, allocator);
100100
return try proc.spawnAndWait();
101101
}
102+
103+
// Starts by 0! Clears the previouzs buffer
104+
pub fn join_path(buf: []u8, joins: []const []const u8) []const u8 {
105+
var idx: usize = 0;
106+
107+
for (joins) |j| {
108+
@memcpy(buf[idx .. idx + j.len], j);
109+
idx += j.len;
110+
buf[idx] = std.fs.path.sep;
111+
idx += 1; // sep is usually one char
112+
}
113+
114+
return buf[0..idx];
115+
}

src/zigd.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.6
1+
0.0.7

src/zigdcli.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ fn setup(allocator: std.mem.Allocator, args: []const []const u8, zigd_path: []co
103103
return;
104104
}
105105

106-
const config_path = try std.fs.path.join(allocator, &.{ zigd_path, "config" });
107-
defer allocator.free(config_path);
106+
var config_path_buf: zigdcore.PathBuf = undefined;
107+
const config_path = utils.join_path(&config_path_buf, &.{ zigd_path, "config" });
108108

109109
if (try utils.isFile(config_path)) {
110110
o: while (true) {
@@ -170,8 +170,8 @@ fn exists(allocator: std.mem.Allocator, args: []const []const u8, zigd_path: []c
170170
var zig_version = try zigdcore.ZigVersion.parse(allocator, args[2], &user_arg, false, zigd_path, true);
171171
defer zig_version.deinitIfMightBeAllocated(allocator);
172172

173-
const version_path = try std.fs.path.join(allocator, &.{ zigd_path, "versions", zig_version.as_string });
174-
defer allocator.free(version_path);
173+
var version_path_buf: zigdcore.PathBuf = undefined;
174+
const version_path = utils.join_path(&version_path_buf, &.{ zigd_path, zigdcore.VersionsByZigd, zig_version.as_string });
175175

176176
if (try utils.isDirectory(version_path)) {
177177
try std.io.getStdOut().writer().writeAll("Yes!\n");
@@ -184,8 +184,8 @@ fn recache_master(allocator: std.mem.Allocator, zigd_path: []const u8) !void {
184184
const master = try zigdcore.fetchMaster(allocator, zigd_path, false);
185185
defer allocator.free(master);
186186

187-
const cache_path = try zigdcore.getCachePath(allocator, zigd_path);
188-
defer allocator.free(cache_path);
187+
var cache_path_buf: zigdcore.PathBuf = undefined;
188+
const cache_path = utils.join_path(&cache_path_buf, &.{ zigd_path, zigdcore.CacheByZigd });
189189

190190
const cache_file = try zigdcore.getCacheFile(cache_path);
191191
defer cache_file.close();

src/zigdcore.zig

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const std = @import("std");
22
const utils = @import("utils.zig");
33

4+
pub const PathBuf = [std.fs.MAX_PATH_BYTES]u8;
5+
6+
pub const TempByZigd = "tmp";
7+
pub const CacheByZigd = "cached_master";
8+
pub const VersionsByZigd = "versions";
9+
410
/// Version cannot be master!
511
/// Returns a bool if it installed/reinstalled zig
612
pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download_url: []const u8, zig_version: ZigVersion) !bool {
7-
const final_destination = try std.fs.path.join(allocator, &.{ zigd_path, "versions", zig_version.as_string });
8-
defer allocator.free(final_destination);
13+
var final_dest_buf: PathBuf = undefined;
14+
const final_destination = utils.join_path(&final_dest_buf, &.{ zigd_path, VersionsByZigd, zig_version.as_string });
915

1016
if (try utils.isDirectory(final_destination)) {
1117
o: while (true) {
@@ -50,8 +56,8 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
5056
return error.ResponseWasNotOk;
5157
}
5258

53-
const temp_dir_path = try std.fs.path.join(allocator, &.{ zigd_path, "tmp" });
54-
defer allocator.free(temp_dir_path);
59+
var temp_dir_buf: PathBuf = undefined;
60+
const temp_dir_path = utils.join_path(&temp_dir_buf, &.{ zigd_path, "tmp" });
5561

5662
try utils.createDirectoryIgnoreExist(temp_dir_path);
5763

@@ -63,7 +69,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
6369

6470
var temp_storage_closed: bool = false;
6571
var temporary_storage = try temp_dir.makeOpenPath(temp_name, .{
66-
.iterate = true,
72+
.iterate = utils.os_tag == .windows,
6773
});
6874
errdefer if (!temp_storage_closed) temporary_storage.close();
6975

@@ -88,7 +94,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
8894
try temporary_storage.rename(w_path_duped, final_destination);
8995
temporary_storage.close();
9096
temp_storage_closed = true;
91-
try std.fs.cwd().deleteDir(temp_name);
97+
try temp_dir.deleteTree(temp_name);
9298
} else {
9399
var xz_decompressor = try std.compress.xz.decompress(allocator, req.reader());
94100
defer xz_decompressor.deinit();
@@ -104,7 +110,7 @@ pub fn install_zig(allocator: std.mem.Allocator, zigd_path: []const u8, download
104110
}
105111

106112
pub fn getCachePath(allocator: std.mem.Allocator, zigd_path: []const u8) ![]u8 {
107-
return try std.fs.path.join(allocator, &.{ zigd_path, "cached_master" });
113+
return try std.fs.path.join(allocator, &.{ zigd_path, CacheByZigd });
108114
}
109115

110116
pub fn getCacheFile(cache_path: []const u8) !std.fs.File {
@@ -292,14 +298,6 @@ pub fn getZigdPath(allocator: std.mem.Allocator) ![]u8 {
292298

293299
// I'm bored, that's why im gonna do it the messy style
294300
pub fn garbage_collect_tempdir(zigd_path: []const u8) !void {
295-
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
296-
var idx: usize = 0;
297-
@memcpy(buf[0 .. idx + zigd_path.len], zigd_path);
298-
idx += zigd_path.len;
299-
buf[idx] = std.fs.path.sep;
300-
idx += 1;
301-
@memcpy(buf[idx .. idx + "tmp".len], "tmp");
302-
idx += "tmp".len;
303-
304-
try std.fs.deleteTreeAbsolute(buf[0..idx]);
301+
var buf: PathBuf = undefined;
302+
try std.fs.deleteTreeAbsolute(utils.join_path(&buf, &.{ zigd_path, "tmp" }));
305303
}

src/zigdemu.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub fn main() !void {
1717

1818
try zigdcore.garbage_collect_tempdir(zigd_path);
1919

20-
const config_path = try std.fs.path.join(allocator, &.{ zigd_path, "config" });
21-
defer allocator.free(config_path);
20+
var config_path_dir: zigdcore.PathBuf = undefined;
21+
const config_path = utils.join_path(&config_path_dir, &.{ zigd_path, "config" });
2222

2323
var config = try Config.load_from(allocator, config_path);
2424
defer config.deinit();
@@ -67,8 +67,8 @@ pub fn main() !void {
6767
zig_version = try zigdcore.ZigVersion.parse(allocator, zig_version.as_string, &zig_version.source, true, zigd_path, true);
6868
defer zig_version.deinitIfMightBeAllocated(allocator);
6969

70-
const zig_binary_path = try std.fs.path.join(allocator, &.{ zigd_path, "versions", zig_version.as_string, "zig" ++ utils.binary_ext });
71-
defer allocator.free(zig_binary_path);
70+
var bin_path: zigdcore.PathBuf = undefined;
71+
const zig_binary_path = utils.join_path(&bin_path, &.{ zigd_path, zigdcore.VersionsByZigd, zig_version.as_string, "zig" ++ utils.binary_ext });
7272

7373
if (!(try utils.isFile(zig_binary_path))) o: {
7474
const zig_version_path = std.mem.lastIndexOfScalar(u8, zig_binary_path, std.fs.path.sep) orelse unreachable;

0 commit comments

Comments
 (0)