Skip to content

Commit

Permalink
optimize: improve performance by using comptime
Browse files Browse the repository at this point in the history
rename origin `detect` func in `architecture.zig` to `platform_str` and make it become comptime func
  • Loading branch information
jinzhongjia committed Jul 27, 2024
1 parent c55bc00 commit f598cf0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 47 deletions.
66 changes: 26 additions & 40 deletions src/architecture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,36 @@ fn archToString(arch: std.Target.Cpu.Arch) ?[]const u8 {
};
}

pub fn detect(allocator: std.mem.Allocator, params: DetectParams) !?[]u8 {
const os_str = osToString(params.os) orelse return error.UnsupportedSystem;
const arch_str = archToString(params.arch) orelse return error.UnsupportedSystem;

const len = os_str.len + arch_str.len + 1; // +1 for the '-'
const result = try allocator.alloc(u8, len);

if (params.reverse) {
@memcpy(result[0..arch_str.len], arch_str);
result[arch_str.len] = '-';
@memcpy(result[arch_str.len + 1 ..], os_str);
} else {
@memcpy(result[0..os_str.len], os_str);
result[os_str.len] = '-';
@memcpy(result[os_str.len + 1 ..], arch_str);
}

return result;
/// get platform str
///
/// in offical zig json
/// use "arch-platform" as key
/// use "platform-arch" as file name
///
/// for performance, we treat this function as comptime-func
pub fn platform_str(comptime params: DetectParams) !?[]const u8 {
const os_str = comptime osToString(params.os) orelse
return error.UnsupportedSystem;

const arch_str = comptime archToString(params.arch) orelse
return error.UnsupportedSystem;

if (params.reverse)
return arch_str ++ "-" ++ os_str;

return os_str ++ "-" ++ arch_str;
}

test "detect() Test" {
const allocator = std.testing.allocator;

{
const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
defer allocator.free(result);
try std.testing.expectEqualStrings("linux-x86_64", result);
}
const result_1 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
try std.testing.expectEqualStrings("linux-x86_64", result_1);

{
const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.aarch64, .reverse = true }) orelse unreachable;
defer allocator.free(result);
try std.testing.expectEqualStrings("aarch64-linux", result);
}
const result_2 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.aarch64, .reverse = true }) orelse unreachable;
try std.testing.expectEqualStrings("aarch64-linux", result_2);

{
const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.macos, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
defer allocator.free(result);
try std.testing.expectEqualStrings("macos-x86_64", result);
}
const result_3 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.macos, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
try std.testing.expectEqualStrings("macos-x86_64", result_3);

{
const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.windows, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
defer allocator.free(result);
try std.testing.expectEqualStrings("windows-x86_64", result);
}
const result_4 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.windows, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable;
try std.testing.expectEqualStrings("windows-x86_64", result_4);
}
11 changes: 7 additions & 4 deletions src/download.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,19 @@ fn download_and_extract(
var zvm_dir = try open_or_create_zvm_dir();
defer zvm_dir.close();

const platform = try architecture.detect(allocator, architecture.DetectParams{ .os = builtin.os.tag, .arch = builtin.cpu.arch, .reverse = false }) orelse unreachable;
defer allocator.free(platform);
const platform_str = try architecture.platform_str(architecture.DetectParams{
.os = builtin.os.tag,
.arch = builtin.cpu.arch,
.reverse = false,
}) orelse unreachable;

const file_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "zig-", platform, "-", version, ".", archive_ext });
const file_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "zig-", platform_str, "-", version, ".", archive_ext });
defer allocator.free(file_name);

const total_size: usize = @intCast(req.response.content_length orelse 0);
var downloaded_bytes: usize = 0;

const download_message = try std.fmt.allocPrint(allocator, "Downloading Zig version {s} for platform {s}...", .{ version, platform });
const download_message = try std.fmt.allocPrint(allocator, "Downloading Zig version {s} for platform {s}...", .{ version, platform_str });
defer allocator.free(download_message);
var download_node = root_node.start(download_message, total_size);

Expand Down
6 changes: 3 additions & 3 deletions src/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ fn fetch_version_data(allocator: Allocator, requested_version: []const u8, sub_k
}

pub fn from_version(version: []const u8) !void {
var allocator = tools.get_allocator();
const allocator = tools.get_allocator();

const platform_str = try architecture.detect(allocator, architecture.DetectParams{
const platform_str = try architecture.platform_str(architecture.DetectParams{
.os = builtin.os.tag,
.arch = builtin.cpu.arch,
.reverse = true,
}) orelse unreachable;
defer allocator.free(platform_str);

var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();

const version_data = try fetch_version_data(arena.allocator(), version, platform_str);
if (version_data) |data| {
std.debug.print("Install {s}\n", .{data.name});
Expand Down

0 comments on commit f598cf0

Please sign in to comment.