From 08e05398069bb8e480c7a0a4d0b4630f89cb3157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Anic=CC=81?= Date: Sat, 20 Jan 2024 22:45:40 +0100 Subject: [PATCH] add more cases to the deflate benchmark --- bin/deflate_bench.zig | 33 ++++++++++++++++++++++++++------- bin/gzip.zig | 22 ++++++++++++++++++++++ build.zig | 26 ++++++++++++++++++-------- src/deflate.zig | 2 +- 4 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 bin/gzip.zig diff --git a/bin/deflate_bench.zig b/bin/deflate_bench.zig index 9df095b..5b7f2a3 100644 --- a/bin/deflate_bench.zig +++ b/bin/deflate_bench.zig @@ -1,8 +1,8 @@ const std = @import("std"); const flate = @import("flate"); +//const data = @embedFile("benchdata/large.tar"); //const data = @embedFile("benchdata/2600.txt.utf-8"); - const data = @embedFile("benchdata/bb0f7d55e8c50e379fa9bdcb8758d89d08e0cc1f.tar"); pub fn main() !void { @@ -11,15 +11,19 @@ pub fn main() !void { const arena_allocator = arena.allocator(); const args = try std.process.argsAlloc(arena_allocator); - const output_file_name = "output.gz"; - var output_file = try std.fs.cwd().createFile(output_file_name, .{ .truncate = true }); - const output = output_file.writer(); - defer output_file.close(); + // const output_file_name = "output.zz"; + // var output_file = try std.fs.cwd().createFile(output_file_name, .{ .truncate = true }); + // const output = output_file.writer(); + // defer output_file.close(); + + //const output = std.io.getStdOut().writer(); + + const output = NullWriter.init().writer(); var i: usize = 1; while (i < args.len) : (i += 1) { if (std.mem.eql(u8, args[i], "--std")) { - //try stdZlib(output); + try stdZlib(output); return; } else { std.os.exit(1); @@ -35,9 +39,24 @@ pub fn lib(output: anytype) !void { pub fn stdZlib(writer: anytype) !void { const allocator = std.heap.page_allocator; - var compressor = try std.compress.zlib.compressStream(allocator, writer, .{}); + var compressor = try std.compress.zlib.compressStream(allocator, writer, .{ .level = .default }); defer compressor.deinit(); try compressor.writer().writeAll(data); try compressor.finish(); } + +const NullWriter = struct { + const Self = @This(); + pub const WriteError = error{}; + pub const Writer = std.io.Writer(Self, WriteError, write); + pub fn writer(self: Self) Writer { + return .{ .context = self }; + } + pub fn write(_: Self, bytes: []const u8) WriteError!usize { + return bytes.len; + } + pub fn init() Self { + return .{}; + } +}; diff --git a/bin/gzip.zig b/bin/gzip.zig new file mode 100644 index 0000000..e869a64 --- /dev/null +++ b/bin/gzip.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const flate = @import("flate"); + +// Comparable to standard gzip with -kf flags: +// $ gzip -kf +// +pub fn main() !void { + var args = std.process.args(); + _ = args.next(); + + if (args.next()) |input_file_name| { + var input_file = try std.fs.cwd().openFile(input_file_name, .{}); + defer input_file.close(); + + var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const output_file_name = try std.fmt.bufPrint(&buf, "{s}.gz", .{input_file_name}); + var output_file = try std.fs.cwd().createFile(output_file_name, .{ .truncate = true }); + defer output_file.close(); + + try flate.gzip(input_file.reader(), output_file.writer()); + } +} diff --git a/build.zig b/build.zig index 240a311..599be9b 100644 --- a/build.zig +++ b/build.zig @@ -98,12 +98,22 @@ pub fn build(b: *std.Build) void { .source_file = .{ .path = "src/root.zig" }, }); - const gzip_bench = b.addExecutable(.{ - .name = "deflate_bench", - .root_source_file = .{ .path = "bin/deflate_bench.zig" }, - .target = target, - .optimize = optimize, - }); - gzip_bench.addModule("flate", flate_module); - b.installArtifact(gzip_bench); + const bins = [_]struct { + name: []const u8, + src: []const u8, + }{ + .{ .name = "deflate_bench", .src = "bin/deflate_bench.zig" }, + .{ .name = "gzip", .src = "bin/gzip.zig" }, + }; + + for (bins) |i| { + const bin = b.addExecutable(.{ + .name = i.name, + .root_source_file = .{ .path = i.src }, + .target = target, + .optimize = optimize, + }); + bin.addModule("flate", flate_module); + b.installArtifact(bin); + } } diff --git a/src/deflate.zig b/src/deflate.zig index ca79f64..f1eef3b 100644 --- a/src/deflate.zig +++ b/src/deflate.zig @@ -431,7 +431,7 @@ const Lookup = struct { } // Previous location with the same hash value. - pub fn prev(self: *Lookup, idx: u32) u32 { + pub inline fn prev(self: *Lookup, idx: u32) u32 { const v = self.chain[idx & mask]; return if (v > idx) not_found else v; }