mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-05-22 21:00:15 +00:00
bench/codepoint-width: add wcwidth
This commit is contained in:
@@ -42,6 +42,9 @@ const Mode = enum {
|
||||
/// and establishes a baseline for the other modes.
|
||||
baseline,
|
||||
|
||||
/// libc wcwidth
|
||||
wcwidth,
|
||||
|
||||
/// Use ziglyph library to calculate the display width of each codepoint.
|
||||
ziglyph,
|
||||
|
||||
@@ -72,6 +75,7 @@ pub fn main() !void {
|
||||
// Handle the modes that do not depend on terminal state first.
|
||||
switch (args.mode) {
|
||||
.baseline => try benchBaseline(reader, buf),
|
||||
.wcwidth => try benchWcwidth(reader, buf),
|
||||
.ziglyph => try benchZiglyph(reader, buf),
|
||||
.simd => try benchSimd(reader, buf),
|
||||
}
|
||||
@@ -94,6 +98,32 @@ noinline fn benchBaseline(
|
||||
}
|
||||
}
|
||||
|
||||
extern "c" fn wcwidth(c: u32) c_int;
|
||||
|
||||
noinline fn benchWcwidth(
|
||||
reader: anytype,
|
||||
buf: []u8,
|
||||
) !void {
|
||||
var d: UTF8Decoder = .{};
|
||||
while (true) {
|
||||
const n = try reader.read(buf);
|
||||
if (n == 0) break;
|
||||
|
||||
// Using stream.next directly with a for loop applies a naive
|
||||
// scalar approach.
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
noinline fn benchZiglyph(
|
||||
reader: anytype,
|
||||
buf: []u8,
|
||||
|
||||
Reference in New Issue
Block a user