From aa6b61fc9f61e6a874192830cc39510c30e77e11 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 20 Jun 2026 01:22:46 -0500 Subject: [PATCH] Fixed a couple minor issues Added benchmark.sh --- benchmark.sh | 22 ++++++++++++++++++++++ build.zig | 18 +++++++++++++++--- run_tests.sh | 10 ---------- src/bin/unsquashfs.zig | 10 ++++++---- src/extract-multi.zig | 6 +----- 5 files changed, 44 insertions(+), 22 deletions(-) create mode 100755 benchmark.sh delete mode 100755 run_tests.sh diff --git a/benchmark.sh b/benchmark.sh new file mode 100755 index 0000000..40fc4f2 --- /dev/null +++ b/benchmark.sh @@ -0,0 +1,22 @@ +#! /usr/bin/env bash + +ARCHIVE="testing/LinuxPATest.sfs" + +REF_EXT_LOC="testing/LinuxPAReference" +PROG_EXT_LOC="testing/LinuxPABinTest" + +echo "Building with ReleaseFast" +echo "" + +zig build -Doptimize=ReleaseFast + +echo "Testing Multi-threaded Performance" +echo "" + +hyperfine --warmup 5 --prepare "rm -rf $REF_EXT_LOC && rm -rf $PROG_EXT_LOC" "unsquashfs -d $REF_EXT_LOC $ARCHIVE" "zig-out/bin/unsquashfs -d $PROG_EXT_LOC $ARCHIVE" + +echo "" +echo "Testing Single-threaded Performance" +echo "" + +hyperfine --warmup 5 --prepare "rm -rf $REF_EXT_LOC && rm -rf $PROG_EXT_LOC" "unsquashfs -p 1 -d $REF_EXT_LOC $ARCHIVE" "zig-out/bin/unsquashfs -p 1 -d $PROG_EXT_LOC $ARCHIVE" diff --git a/build.zig b/build.zig index 91f7be8..9223b87 100644 --- a/build.zig +++ b/build.zig @@ -90,24 +90,36 @@ pub fn build(b: *std.Build) !void { b.installArtifact(exe); - const mod_tests = b.addTest(.{ + const lib_tests = b.addTest(.{ .root_module = lib.root_module, .use_llvm = debug, }); - const run_mod_tests = b.addRunArtifact(mod_tests); + const run_lib_tests = b.addRunArtifact(lib_tests); + const exe_tests = b.addTest(.{ + .root_module = exe.root_module, + .use_llvm = debug, + }); + const run_exe_tests = b.addRunArtifact(exe_tests); + const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); + test_step.dependOn(&run_lib_tests.step); + test_step.dependOn(&run_exe_tests.step); // zls build check steps const lib_check = b.addLibrary(.{ .name = "squashfs", .root_module = lib.root_module, + .use_llvm = debug, }); const exe_check = b.addExecutable(.{ .name = "unsquashfs", .root_module = exe.root_module, + .use_llvm = debug, }); const check = b.step("check", "Check if unsquashfs compiles"); check.dependOn(&lib_check.step); check.dependOn(&exe_check.step); + + test_step.dependOn(&lib_check.step); + test_step.dependOn(&exe_check.step); } diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index b3cbdf4..0000000 --- a/run_tests.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -zig test \ - -lc \ - -lz \ - -llzma \ - -lminilzo \ - -llz4 \ - -lzstd \ - src/test.zig diff --git a/src/bin/unsquashfs.zig b/src/bin/unsquashfs.zig index 7f32994..795ac9f 100644 --- a/src/bin/unsquashfs.zig +++ b/src/bin/unsquashfs.zig @@ -2,8 +2,8 @@ const std = @import("std"); const Io = std.Io; const Writer = Io.Writer; const builtin = @import("builtin"); -const build = @import("build"); +const build = @import("build"); const squashfs = @import("squashfs"); //TODO: Add more options @@ -52,14 +52,16 @@ pub fn main(init: std.process.Init) !void { var out = stdout.writer(io, &[0]u8{}); defer out.interface.flush() catch {}; - try handleArgs(init.minimal.args, &out.interface); + try handleArgs(alloc, init.minimal.args, &out.interface); if (archive.len == 0) { try out.interface.print("You must provide a squashfs archive\n", .{}); try out.interface.print(help_mgs, .{}); return; } + var fil = try Io.Dir.cwd().openFile(io, archive, .{}); //TODO: Handle error gracefully. defer fil.close(io); + var arc: squashfs.Archive = try .init(io, fil, offset); //TODO: Update when memory size matters. //TODO: Handle error gracefully. defer arc.deinit(io); const options: squashfs.ExtractionOptions = .{ @@ -89,7 +91,7 @@ pub fn main(init: std.process.Init) !void { defer sfs_fil.deinit(); const last_ind = std.mem.lastIndexOf(u8, files[i], "/"); - const file_name = if (last_ind != null) files[i][last_ind..] else files[i]; + const file_name = if (last_ind != null) files[i][last_ind.?..] else files[i]; const fil_ext_loc = if (ext_loc.len > 0) try std.mem.concat(alloc, u8, &.{ ext_loc, "/", file_name }) @@ -128,7 +130,7 @@ fn handleArgs(alloc: std.mem.Allocator, main_args: std.process.Args, out: *Write return errors.InvalidArguments; } if (!alloc.resize(files, files.len + 1)) { - const new_alloc = alloc.alloc([]const u8, files.len + 1); + const new_alloc = try alloc.alloc([:0]const u8, files.len + 1); @memcpy(new_alloc[0..files.len], files); alloc.free(files); files = new_alloc; diff --git a/src/extract-multi.zig b/src/extract-multi.zig index ac0165d..a1eeafb 100644 --- a/src/extract-multi.zig +++ b/src/extract-multi.zig @@ -74,11 +74,7 @@ pub fn extract( ), } - // try loop.await(io); - loop.await(io) catch |err| { - std.debug.print("err: {}\n", .{err}); - return err; - }; + try loop.await(io); } fn dirOrder(_: void, a: PathReturn, b: PathReturn) std.math.Order {