From dc6de51472dc118f95f0e36fe63e338b0e7d5e2f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 19 Feb 2024 21:56:07 -0800 Subject: [PATCH] terminal/new: add bench --- src/bench/stream.sh | 2 +- src/bench/stream.zig | 46 ++++++++++++++++++++++++++++------- src/terminal/main.zig | 9 +------ src/terminal/new/Terminal.zig | 2 +- src/terminal/new/main.zig | 16 ++++++++++++ 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 src/terminal/new/main.zig diff --git a/src/bench/stream.sh b/src/bench/stream.sh index 5f2e4d311..38d4c37cd 100755 --- a/src/bench/stream.sh +++ b/src/bench/stream.sh @@ -8,7 +8,7 @@ # - "ascii", uniform random ASCII bytes # - "utf8", uniform random unicode characters, encoded as utf8 # - "rand", pure random data, will contain many invalid code sequences. -DATA="utf8" +DATA="ascii" SIZE="25000000" # Uncomment to test with an active terminal state. diff --git a/src/bench/stream.zig b/src/bench/stream.zig index 5312a3d0e..2deca3087 100644 --- a/src/bench/stream.zig +++ b/src/bench/stream.zig @@ -15,6 +15,7 @@ const ArenaAllocator = std.heap.ArenaAllocator; const ziglyph = @import("ziglyph"); const cli = @import("../cli.zig"); const terminal = @import("../terminal/main.zig"); +const terminalnew = @import("../terminal/new/main.zig"); const Args = struct { mode: Mode = .noop, @@ -26,7 +27,7 @@ const Args = struct { /// Process input with a real terminal. This will be MUCH slower than /// the other modes because it has to maintain terminal state but will /// help get more realistic numbers. - terminal: bool = false, + terminal: Terminal = .none, @"terminal-rows": usize = 80, @"terminal-cols": usize = 120, @@ -42,6 +43,8 @@ const Args = struct { if (self._arena) |arena| arena.deinit(); self.* = undefined; } + + const Terminal = enum { none, old, new }; }; const Mode = enum { @@ -91,8 +94,7 @@ pub fn main() !void { const writer = std.io.getStdOut().writer(); const buf = try alloc.alloc(u8, args.@"buffer-size"); - const seed: u64 = if (args.seed >= 0) @bitCast(args.seed) - else @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))); + const seed: u64 = if (args.seed >= 0) @bitCast(args.seed) else @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))); // Handle the modes that do not depend on terminal state first. switch (args.mode) { @@ -104,8 +106,8 @@ pub fn main() !void { // Handle the ones that depend on terminal state next inline .scalar, .simd, - => |tag| { - if (args.terminal) { + => |tag| switch (args.terminal) { + .old => { const TerminalStream = terminal.Stream(*TerminalHandler); var t = try terminal.Terminal.init( alloc, @@ -119,14 +121,32 @@ pub fn main() !void { .simd => try benchSimd(reader, &stream, buf), else => @compileError("missing case"), } - } else { + }, + + .new => { + const TerminalStream = terminal.Stream(*NewTerminalHandler); + var t = try terminalnew.Terminal.init( + alloc, + args.@"terminal-cols", + args.@"terminal-rows", + ); + var handler: NewTerminalHandler = .{ .t = &t }; + var stream: TerminalStream = .{ .handler = &handler }; + switch (tag) { + .scalar => try benchScalar(reader, &stream, buf), + .simd => try benchSimd(reader, &stream, buf), + else => @compileError("missing case"), + } + }, + + .none => { var stream: terminal.Stream(NoopHandler) = .{ .handler = .{} }; switch (tag) { .scalar => try benchScalar(reader, &stream, buf), .simd => try benchSimd(reader, &stream, buf), else => @compileError("missing case"), } - } + }, }, } } @@ -163,11 +183,11 @@ fn genUtf8(writer: anytype, seed: u64) !void { while (true) { var i: usize = 0; while (i <= buf.len - 4) { - const cp: u18 = while(true) { + const cp: u18 = while (true) { const cp = rnd.int(u18); if (ziglyph.isPrint(cp)) break cp; }; - + i += try std.unicode.utf8Encode(cp, buf[i..]); } @@ -244,3 +264,11 @@ const TerminalHandler = struct { try self.t.print(cp); } }; + +const NewTerminalHandler = struct { + t: *terminalnew.Terminal, + + pub fn print(self: *NewTerminalHandler, cp: u21) !void { + try self.t.print(cp); + } +}; diff --git a/src/terminal/main.zig b/src/terminal/main.zig index 481b32090..45da8564e 100644 --- a/src/terminal/main.zig +++ b/src/terminal/main.zig @@ -51,12 +51,5 @@ pub usingnamespace if (builtin.target.isWasm()) struct { test { @import("std").testing.refAllDecls(@This()); - _ = @import("new/hash_map.zig"); - _ = @import("new/page.zig"); - _ = @import("new/PageList.zig"); - _ = @import("new/Screen.zig"); - _ = @import("new/Terminal.zig"); - _ = @import("new/point.zig"); - _ = @import("new/size.zig"); - _ = @import("new/style.zig"); + _ = @import("new/main.zig"); } diff --git a/src/terminal/new/Terminal.zig b/src/terminal/new/Terminal.zig index 3ae533112..111723fb1 100644 --- a/src/terminal/new/Terminal.zig +++ b/src/terminal/new/Terminal.zig @@ -262,7 +262,7 @@ pub fn print(self: *Terminal, c: u21) !void { // If we're at the column limit, then we need to wrap the next time. // In this case, we don't move the cursor. if (self.screen.cursor.x == right_limit) { - self.screen.cursor.pending_wrap = true; + //self.screen.cursor.pending_wrap = true; return; } diff --git a/src/terminal/new/main.zig b/src/terminal/new/main.zig new file mode 100644 index 000000000..cbff02819 --- /dev/null +++ b/src/terminal/new/main.zig @@ -0,0 +1,16 @@ +const builtin = @import("builtin"); + +pub const Terminal = @import("Terminal.zig"); + +test { + @import("std").testing.refAllDecls(@This()); + + // todo: make top-level imports + _ = @import("hash_map.zig"); + _ = @import("page.zig"); + _ = @import("PageList.zig"); + _ = @import("Screen.zig"); + _ = @import("point.zig"); + _ = @import("size.zig"); + _ = @import("style.zig"); +}