Skip to content

Commit

Permalink
add more cases to the deflate benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Jan 20, 2024
1 parent e526241 commit 08e0539
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
33 changes: 26 additions & 7 deletions bin/deflate_bench.zig
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand All @@ -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 .{};
}
};
22 changes: 22 additions & 0 deletions bin/gzip.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const std = @import("std");
const flate = @import("flate");

// Comparable to standard gzip with -kf flags:
// $ gzip -kf <file_name>
//
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());
}
}
26 changes: 18 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/deflate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 08e0539

Please sign in to comment.