terminal/new: add bench

This commit is contained in:
Mitchell Hashimoto
2024-02-19 21:56:07 -08:00
parent a1c14d1859
commit dc6de51472
5 changed files with 56 additions and 19 deletions

View File

@ -8,7 +8,7 @@
# - "ascii", uniform random ASCII bytes # - "ascii", uniform random ASCII bytes
# - "utf8", uniform random unicode characters, encoded as utf8 # - "utf8", uniform random unicode characters, encoded as utf8
# - "rand", pure random data, will contain many invalid code sequences. # - "rand", pure random data, will contain many invalid code sequences.
DATA="utf8" DATA="ascii"
SIZE="25000000" SIZE="25000000"
# Uncomment to test with an active terminal state. # Uncomment to test with an active terminal state.

View File

@ -15,6 +15,7 @@ const ArenaAllocator = std.heap.ArenaAllocator;
const ziglyph = @import("ziglyph"); const ziglyph = @import("ziglyph");
const cli = @import("../cli.zig"); const cli = @import("../cli.zig");
const terminal = @import("../terminal/main.zig"); const terminal = @import("../terminal/main.zig");
const terminalnew = @import("../terminal/new/main.zig");
const Args = struct { const Args = struct {
mode: Mode = .noop, mode: Mode = .noop,
@ -26,7 +27,7 @@ const Args = struct {
/// Process input with a real terminal. This will be MUCH slower than /// Process input with a real terminal. This will be MUCH slower than
/// the other modes because it has to maintain terminal state but will /// the other modes because it has to maintain terminal state but will
/// help get more realistic numbers. /// help get more realistic numbers.
terminal: bool = false, terminal: Terminal = .none,
@"terminal-rows": usize = 80, @"terminal-rows": usize = 80,
@"terminal-cols": usize = 120, @"terminal-cols": usize = 120,
@ -42,6 +43,8 @@ const Args = struct {
if (self._arena) |arena| arena.deinit(); if (self._arena) |arena| arena.deinit();
self.* = undefined; self.* = undefined;
} }
const Terminal = enum { none, old, new };
}; };
const Mode = enum { const Mode = enum {
@ -91,8 +94,7 @@ pub fn main() !void {
const writer = std.io.getStdOut().writer(); const writer = std.io.getStdOut().writer();
const buf = try alloc.alloc(u8, args.@"buffer-size"); const buf = try alloc.alloc(u8, args.@"buffer-size");
const seed: u64 = if (args.seed >= 0) @bitCast(args.seed) const seed: u64 = if (args.seed >= 0) @bitCast(args.seed) else @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())));
else @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())));
// Handle the modes that do not depend on terminal state first. // Handle the modes that do not depend on terminal state first.
switch (args.mode) { switch (args.mode) {
@ -104,8 +106,8 @@ pub fn main() !void {
// Handle the ones that depend on terminal state next // Handle the ones that depend on terminal state next
inline .scalar, inline .scalar,
.simd, .simd,
=> |tag| { => |tag| switch (args.terminal) {
if (args.terminal) { .old => {
const TerminalStream = terminal.Stream(*TerminalHandler); const TerminalStream = terminal.Stream(*TerminalHandler);
var t = try terminal.Terminal.init( var t = try terminal.Terminal.init(
alloc, alloc,
@ -119,14 +121,32 @@ pub fn main() !void {
.simd => try benchSimd(reader, &stream, buf), .simd => try benchSimd(reader, &stream, buf),
else => @compileError("missing case"), 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 = .{} }; var stream: terminal.Stream(NoopHandler) = .{ .handler = .{} };
switch (tag) { switch (tag) {
.scalar => try benchScalar(reader, &stream, buf), .scalar => try benchScalar(reader, &stream, buf),
.simd => try benchSimd(reader, &stream, buf), .simd => try benchSimd(reader, &stream, buf),
else => @compileError("missing case"), else => @compileError("missing case"),
} }
} },
}, },
} }
} }
@ -163,11 +183,11 @@ fn genUtf8(writer: anytype, seed: u64) !void {
while (true) { while (true) {
var i: usize = 0; var i: usize = 0;
while (i <= buf.len - 4) { while (i <= buf.len - 4) {
const cp: u18 = while(true) { const cp: u18 = while (true) {
const cp = rnd.int(u18); const cp = rnd.int(u18);
if (ziglyph.isPrint(cp)) break cp; if (ziglyph.isPrint(cp)) break cp;
}; };
i += try std.unicode.utf8Encode(cp, buf[i..]); i += try std.unicode.utf8Encode(cp, buf[i..]);
} }
@ -244,3 +264,11 @@ const TerminalHandler = struct {
try self.t.print(cp); try self.t.print(cp);
} }
}; };
const NewTerminalHandler = struct {
t: *terminalnew.Terminal,
pub fn print(self: *NewTerminalHandler, cp: u21) !void {
try self.t.print(cp);
}
};

View File

@ -51,12 +51,5 @@ pub usingnamespace if (builtin.target.isWasm()) struct {
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());
_ = @import("new/hash_map.zig"); _ = @import("new/main.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");
} }

View File

@ -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. // If we're at the column limit, then we need to wrap the next time.
// In this case, we don't move the cursor. // In this case, we don't move the cursor.
if (self.screen.cursor.x == right_limit) { if (self.screen.cursor.x == right_limit) {
self.screen.cursor.pending_wrap = true; //self.screen.cursor.pending_wrap = true;
return; return;
} }

16
src/terminal/new/main.zig Normal file
View File

@ -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");
}