Skip to content

Commit

Permalink
Updated to work on 0.14 dev master
Browse files Browse the repository at this point in the history
  • Loading branch information
permutationlock committed Aug 8, 2024
1 parent f7df0a5 commit 4a67521
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 246 deletions.
88 changes: 44 additions & 44 deletions benchmarks/buffered_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const vio = io.vio;
const LOOPS = 1000;

pub fn main() !void {
var in: [10000]u8 = undefined;
var in: [100000]u8 = undefined;
try std.posix.getrandom(&in);

{
Expand Down Expand Up @@ -210,47 +210,47 @@ pub fn main() !void {
);
}

{
// time std.io stream
var found: usize = 0;
var bytes: usize = 0;
var fbr = std.io.fixedBufferStream(&in);
var out: [10000]u8 = undefined;
var out_stream = std.io.fixedBufferStream(&out);

var bfbr = std.io.bufferedReader(fbr.reader());
var boutstream = std.io.bufferedWriter(out_stream.writer());

const reader = bfbr.reader();
const anyreader = reader.any();
const writer = boutstream.writer();

var timer = try std.time.Timer.start();

for (0..LOOPS) |_| {
fbr.pos = 0;

while (true) {
anyreader.streamUntilDelimiter(
writer,
'\n',
out.len,
) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};

try boutstream.flush();
found += 1;
bytes += out_stream.getWritten().len;
out_stream.pos = 0;
}
}
const elapsed = timer.lap();
std.debug.print("std.io fixedBufferStream\n", .{});
std.debug.print(
"Took: {d}us ({d}ns / iteration) {d} entries, {d} bytes\n",
.{ elapsed / 1000, elapsed / LOOPS, found, bytes },
);
}
//{
// // time std.io stream
// var found: usize = 0;
// var bytes: usize = 0;
// var fbr = std.io.fixedBufferStream(&in);
// var out: [10000]u8 = undefined;
// var out_stream = std.io.fixedBufferStream(&out);

// var bfbr = std.io.bufferedReader(fbr.reader());
// var boutstream = std.io.bufferedWriter(out_stream.writer());

// const reader = bfbr.reader();
// const anyreader = reader.any();
// const writer = boutstream.writer();

// var timer = try std.time.Timer.start();

// for (0..LOOPS) |_| {
// fbr.pos = 0;

// while (true) {
// anyreader.streamUntilDelimiter(
// writer,
// '\n',
// out.len,
// ) catch |err| switch (err) {
// error.EndOfStream => break,
// else => return err,
// };

// try boutstream.flush();
// found += 1;
// bytes += out_stream.getWritten().len;
// out_stream.pos = 0;
// }
// }
// const elapsed = timer.lap();
// std.debug.print("buffered std.io fixedBufferStream\n", .{});
// std.debug.print(
// "Took: {d}us ({d}ns / iteration) {d} entries, {d} bytes\n",
// .{ elapsed / 1000, elapsed / LOOPS, found, bytes },
// );
//}
}
30 changes: 26 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});

const zimpl = b.addModule("zimpl", .{
.root_source_file = .{ .path = "src/zimpl.zig" },
.root_source_file = .{
.src_path = .{ .owner = b, .sub_path = "src/zimpl.zig" },
},
});

const examples = [_]BuildFile{
Expand All @@ -30,7 +32,9 @@ pub fn build(b: *Build) !void {
inline for (examples) |example| {
const ex_test = b.addTest(.{
.name = example.name,
.root_source_file = .{ .path = example.path },
.root_source_file = .{
.src_path = .{ .owner = b, .sub_path = example.path },
},
.target = target,
.optimize = optimize,
});
Expand All @@ -42,9 +46,25 @@ pub fn build(b: *Build) !void {
test_step.dependOn(&run.step);
}

const tlib = b.addStaticLibrary(.{
.name = "zimpl",
.root_source_file = .{
.src_path = .{ .owner = b, .sub_path = "src/zimpl.zig" },
},
.target = target,
.optimize = optimize,
});
const docs_step = b.step("docs", "Emit docs");
const docs_install = b.addInstallDirectory(.{
.install_dir = .prefix,
.install_subdir = "docs",
.source_dir = tlib.getEmittedDocs(),
});
docs_step.dependOn(&docs_install.step);

const io = b.addModule("io", .{
.root_source_file = .{
.path = "examples/io.zig",
.src_path = .{ .owner = b, .sub_path = "examples/io.zig" },
},
.imports = &.{.{ .name = "zimpl", .module = zimpl }},
});
Expand All @@ -60,7 +80,9 @@ pub fn build(b: *Build) !void {
inline for (benchmarks) |benchmark| {
const bench = b.addExecutable(.{
.name = benchmark.name,
.root_source_file = .{ .path = benchmark.path },
.root_source_file = .{
.src_path = .{ .owner = b, .sub_path = benchmark.path },
},
.target = target,
.optimize = .ReleaseFast,
});
Expand Down
17 changes: 11 additions & 6 deletions examples/read_file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const std = @import("std");
const io = @import("io.zig");
const vio = @import("vio.zig");
const FixedBufferStream = io.FixedBufferStream;
const Impl = @import("zimpl").Impl;

test "read file with std.fs.File" {
const file = try std.fs.cwd().openFile("examples/read_file/test.txt", .{});
Expand All @@ -17,13 +16,19 @@ test "read file with std.fs.File" {

test "read file with std.posix.fd_t" {
const fd = try std.posix.open("examples/read_file/test.txt", .{}, 0);
const fd_reader: Impl(io.Reader, std.posix.fd_t) = .{
.read = std.posix.read,
.ReadError = std.posix.ReadError,
};
var buffer: [32]u8 = undefined;
var fbs: FixedBufferStream = .{ .buffer = &buffer };
try io.streamUntilDelimiter(fd, fd_reader, &fbs, .{}, '\n', 32);
try io.streamUntilDelimiter(
fd,
.{
.read = std.posix.read,
.ReadError = std.posix.ReadError,
},
&fbs,
.{},
'\n',
32,
);
try std.testing.expectEqualStrings(
"Hello, I am a file!",
fbs.getWritten(),
Expand Down
4 changes: 2 additions & 2 deletions examples/vcount.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const testing = std.testing;

const zimpl = @import("zimpl");
const VIfc = zimpl.VIfc;
const ztable = @import("zimpl").ztable;
const VIfc = ztable.VIfc;

const Counter = VIfc(@import("count.zig").Counter);

Expand Down
6 changes: 3 additions & 3 deletions examples/vcount2.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const std = @import("std");
const testing = std.testing;

const zimpl = @import("zimpl");
const VTable = zimpl.VTable;
const vtable = zimpl.vtable;
const ztable = @import("zimpl").ztable;
const VTable = ztable.VTable;
const vtable = ztable.vtable;

const Counter = @import("count.zig").Counter;

Expand Down
9 changes: 3 additions & 6 deletions examples/vio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const native_endian = @import("builtin").target.cpu.arch.endian();
const mem = std.mem;
const assert = std.debug.assert;

const zimpl = @import("zimpl");
const VIfc = zimpl.VIfc;
const ztable = @import("zimpl").ztable;
const VIfc = ztable.VIfc;

const io = @import("io.zig");

Expand All @@ -22,10 +22,7 @@ pub inline fn readBuffer(reader: Reader) anyerror![]const u8 {
return reader.vtable.readBuffer.?(reader.ctx);
}

pub inline fn readAll(
reader: Reader,
buffer: []u8,
) anyerror!usize {
pub inline fn readAll(reader: Reader, buffer: []u8) anyerror!usize {
return readAtLeast(reader, buffer, buffer.len);
}

Expand Down
Loading

0 comments on commit 4a67521

Please sign in to comment.