mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-18 09:46:07 +03:00
benchmark: add codepoint width benchmark
This commit is contained in:
204
src/benchmark/CodepointWidth.zig
Normal file
204
src/benchmark/CodepointWidth.zig
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
//! This benchmark tests the throughput of codepoint width calculation.
|
||||||
|
//! This is a common operation in terminal character printing and the
|
||||||
|
//! motivating factor to write this benchmark was discovering that our
|
||||||
|
//! codepoint width function was 30% of the runtime of every character
|
||||||
|
//! print.
|
||||||
|
const CodepointWidth = @This();
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const Benchmark = @import("Benchmark.zig");
|
||||||
|
const options = @import("options.zig");
|
||||||
|
const UTF8Decoder = @import("../terminal/UTF8Decoder.zig");
|
||||||
|
const simd = @import("../simd/main.zig");
|
||||||
|
const table = @import("../unicode/main.zig").table;
|
||||||
|
|
||||||
|
const log = std.log.scoped(.@"terminal-stream-bench");
|
||||||
|
|
||||||
|
opts: Options,
|
||||||
|
|
||||||
|
/// The file, opened in the setup function.
|
||||||
|
data_f: ?std.fs.File = null,
|
||||||
|
|
||||||
|
pub const Options = struct {
|
||||||
|
/// The type of codepoint width calculation to use.
|
||||||
|
mode: Mode = .noop,
|
||||||
|
|
||||||
|
/// The data to read as a filepath. If this is "-" then
|
||||||
|
/// we will read stdin. If this is unset, then we will
|
||||||
|
/// do nothing (benchmark is a noop). It'd be more unixy to
|
||||||
|
/// use stdin by default but I find that a hanging CLI command
|
||||||
|
/// with no interaction is a bit annoying.
|
||||||
|
data: ?[]const u8 = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Mode = enum {
|
||||||
|
/// The baseline mode copies the data from the fd into a buffer. This
|
||||||
|
/// is used to show the minimal overhead of reading the fd into memory
|
||||||
|
/// and establishes a baseline for the other modes.
|
||||||
|
noop,
|
||||||
|
|
||||||
|
/// libc wcwidth
|
||||||
|
wcwidth,
|
||||||
|
|
||||||
|
/// Our SIMD implementation.
|
||||||
|
simd,
|
||||||
|
|
||||||
|
/// Test our lookup table implementation.
|
||||||
|
table,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Create a new terminal stream handler for the given arguments.
|
||||||
|
pub fn create(
|
||||||
|
alloc: Allocator,
|
||||||
|
opts: Options,
|
||||||
|
) !*CodepointWidth {
|
||||||
|
const ptr = try alloc.create(CodepointWidth);
|
||||||
|
errdefer alloc.destroy(ptr);
|
||||||
|
ptr.* = .{ .opts = opts };
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn destroy(self: *CodepointWidth, alloc: Allocator) void {
|
||||||
|
alloc.destroy(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn benchmark(self: *CodepointWidth) Benchmark {
|
||||||
|
return .init(self, .{
|
||||||
|
.stepFn = switch (self.opts.mode) {
|
||||||
|
.noop => stepNoop,
|
||||||
|
.wcwidth => stepWcwidth,
|
||||||
|
.table => stepTable,
|
||||||
|
.simd => stepSimd,
|
||||||
|
},
|
||||||
|
.setupFn = setup,
|
||||||
|
.teardownFn = teardown,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup(ptr: *anyopaque) Benchmark.Error!void {
|
||||||
|
const self: *CodepointWidth = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
// Open our data file to prepare for reading. We can do more
|
||||||
|
// validation here eventually.
|
||||||
|
assert(self.data_f == null);
|
||||||
|
self.data_f = options.dataFile(self.opts.data) catch |err| {
|
||||||
|
log.warn("error opening data file err={}", .{err});
|
||||||
|
return error.BenchmarkFailed;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn teardown(ptr: *anyopaque) void {
|
||||||
|
const self: *CodepointWidth = @ptrCast(@alignCast(ptr));
|
||||||
|
if (self.data_f) |f| {
|
||||||
|
f.close();
|
||||||
|
self.data_f = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stepNoop(ptr: *anyopaque) Benchmark.Error!void {
|
||||||
|
_ = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "c" fn wcwidth(c: u32) c_int;
|
||||||
|
|
||||||
|
fn stepWcwidth(ptr: *anyopaque) Benchmark.Error!void {
|
||||||
|
const self: *CodepointWidth = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
const f = self.data_f orelse return;
|
||||||
|
var r = std.io.bufferedReader(f.reader());
|
||||||
|
var d: UTF8Decoder = .{};
|
||||||
|
var buf: [4096]u8 = undefined;
|
||||||
|
while (true) {
|
||||||
|
const n = r.read(&buf) catch |err| {
|
||||||
|
log.warn("error reading data file err={}", .{err});
|
||||||
|
return error.BenchmarkFailed;
|
||||||
|
};
|
||||||
|
if (n == 0) break; // EOF reached
|
||||||
|
|
||||||
|
for (buf[0..n]) |c| {
|
||||||
|
const cp_, const consumed = d.next(c);
|
||||||
|
assert(consumed);
|
||||||
|
if (cp_) |cp| {
|
||||||
|
const width = wcwidth(cp);
|
||||||
|
|
||||||
|
// Write the width to the buffer to avoid it being compiled
|
||||||
|
// away
|
||||||
|
buf[0] = @intCast(width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stepTable(ptr: *anyopaque) Benchmark.Error!void {
|
||||||
|
const self: *CodepointWidth = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
const f = self.data_f orelse return;
|
||||||
|
var r = std.io.bufferedReader(f.reader());
|
||||||
|
var d: UTF8Decoder = .{};
|
||||||
|
var buf: [4096]u8 = undefined;
|
||||||
|
while (true) {
|
||||||
|
const n = r.read(&buf) catch |err| {
|
||||||
|
log.warn("error reading data file err={}", .{err});
|
||||||
|
return error.BenchmarkFailed;
|
||||||
|
};
|
||||||
|
if (n == 0) break; // EOF reached
|
||||||
|
|
||||||
|
for (buf[0..n]) |c| {
|
||||||
|
const cp_, const consumed = d.next(c);
|
||||||
|
assert(consumed);
|
||||||
|
if (cp_) |cp| {
|
||||||
|
// This is the same trick we do in terminal.zig so we
|
||||||
|
// keep it here.
|
||||||
|
const width = if (cp <= 0xFF)
|
||||||
|
1
|
||||||
|
else
|
||||||
|
table.get(@intCast(cp)).width;
|
||||||
|
|
||||||
|
// Write the width to the buffer to avoid it being compiled
|
||||||
|
// away
|
||||||
|
buf[0] = @intCast(width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stepSimd(ptr: *anyopaque) Benchmark.Error!void {
|
||||||
|
const self: *CodepointWidth = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
const f = self.data_f orelse return;
|
||||||
|
var r = std.io.bufferedReader(f.reader());
|
||||||
|
var d: UTF8Decoder = .{};
|
||||||
|
var buf: [4096]u8 = undefined;
|
||||||
|
while (true) {
|
||||||
|
const n = r.read(&buf) catch |err| {
|
||||||
|
log.warn("error reading data file err={}", .{err});
|
||||||
|
return error.BenchmarkFailed;
|
||||||
|
};
|
||||||
|
if (n == 0) break; // EOF reached
|
||||||
|
|
||||||
|
for (buf[0..n]) |c| {
|
||||||
|
const cp_, const consumed = d.next(c);
|
||||||
|
assert(consumed);
|
||||||
|
if (cp_) |cp| {
|
||||||
|
const width = simd.codepointWidth(cp);
|
||||||
|
|
||||||
|
// Write the width to the buffer to avoid it being compiled
|
||||||
|
// away
|
||||||
|
buf[0] = @intCast(width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test CodepointWidth {
|
||||||
|
const testing = std.testing;
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
|
||||||
|
const impl: *CodepointWidth = try .create(alloc, .{});
|
||||||
|
defer impl.destroy(alloc);
|
||||||
|
|
||||||
|
const bench = impl.benchmark();
|
||||||
|
_ = try bench.run(.once);
|
||||||
|
}
|
@ -18,6 +18,7 @@ const assert = std.debug.assert;
|
|||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const terminalpkg = @import("../terminal/main.zig");
|
const terminalpkg = @import("../terminal/main.zig");
|
||||||
const Benchmark = @import("Benchmark.zig");
|
const Benchmark = @import("Benchmark.zig");
|
||||||
|
const options = @import("options.zig");
|
||||||
const Terminal = terminalpkg.Terminal;
|
const Terminal = terminalpkg.Terminal;
|
||||||
const Stream = terminalpkg.Stream(*Handler);
|
const Stream = terminalpkg.Stream(*Handler);
|
||||||
|
|
||||||
@ -89,12 +90,10 @@ fn setup(ptr: *anyopaque) Benchmark.Error!void {
|
|||||||
// Open our data file to prepare for reading. We can do more
|
// Open our data file to prepare for reading. We can do more
|
||||||
// validation here eventually.
|
// validation here eventually.
|
||||||
assert(self.data_f == null);
|
assert(self.data_f == null);
|
||||||
if (self.opts.data) |path| {
|
self.data_f = options.dataFile(self.opts.data) catch |err| {
|
||||||
self.data_f = std.fs.cwd().openFile(path, .{}) catch |err| {
|
log.warn("error opening data file err={}", .{err});
|
||||||
log.warn("error opening data file err={}", .{err});
|
return error.BenchmarkFailed;
|
||||||
return error.BenchmarkFailed;
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn teardown(ptr: *anyopaque) void {
|
fn teardown(ptr: *anyopaque) void {
|
||||||
|
@ -6,6 +6,7 @@ const cli = @import("../cli.zig");
|
|||||||
/// benchmarks.
|
/// benchmarks.
|
||||||
pub const Action = enum {
|
pub const Action = enum {
|
||||||
@"terminal-stream",
|
@"terminal-stream",
|
||||||
|
@"codepoint-width",
|
||||||
|
|
||||||
/// Returns the struct associated with the action. The struct
|
/// Returns the struct associated with the action. The struct
|
||||||
/// should have a few decls:
|
/// should have a few decls:
|
||||||
@ -18,6 +19,7 @@ pub const Action = enum {
|
|||||||
pub fn Struct(comptime action: Action) type {
|
pub fn Struct(comptime action: Action) type {
|
||||||
return switch (action) {
|
return switch (action) {
|
||||||
.@"terminal-stream" => @import("TerminalStream.zig"),
|
.@"terminal-stream" => @import("TerminalStream.zig"),
|
||||||
|
.@"codepoint-width" => @import("CodepointWidth.zig"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -2,6 +2,7 @@ pub const cli = @import("cli.zig");
|
|||||||
pub const Benchmark = @import("Benchmark.zig");
|
pub const Benchmark = @import("Benchmark.zig");
|
||||||
pub const CApi = @import("CApi.zig");
|
pub const CApi = @import("CApi.zig");
|
||||||
pub const TerminalStream = @import("TerminalStream.zig");
|
pub const TerminalStream = @import("TerminalStream.zig");
|
||||||
|
pub const CodepointWidth = @import("CodepointWidth.zig");
|
||||||
|
|
||||||
test {
|
test {
|
||||||
_ = @import("std").testing.refAllDecls(@This());
|
_ = @import("std").testing.refAllDecls(@This());
|
||||||
|
20
src/benchmark/options.zig
Normal file
20
src/benchmark/options.zig
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
//! This file contains helpers for CLI options.
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
/// Returns the data file for the given path in a way that is consistent
|
||||||
|
/// across our CLI. If the path is not set then no file is returned.
|
||||||
|
/// If the path is "-", then we will return stdin. If the path is
|
||||||
|
/// a file then we will open and return the handle.
|
||||||
|
pub fn dataFile(path_: ?[]const u8) !?std.fs.File {
|
||||||
|
const path = path_ orelse return null;
|
||||||
|
|
||||||
|
// Stdin
|
||||||
|
if (std.mem.eql(u8, path, "-")) return std.io.getStdIn();
|
||||||
|
|
||||||
|
// Normal file
|
||||||
|
const file = try std.fs.cwd().openFile(path, .{});
|
||||||
|
errdefer file.close();
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
Reference in New Issue
Block a user