unicode: generate our own lookup tables

This commit is contained in:
Mitchell Hashimoto
2024-02-08 21:01:11 -08:00
parent 4ae41579da
commit 9755d0696e
7 changed files with 361 additions and 0 deletions

View File

@@ -31,6 +31,8 @@ hyperfine \
"./zig-out/bin/bench-codepoint-width --mode=utf8proc${ARGS} </tmp/ghostty_bench_data" \
-n ziglyph \
"./zig-out/bin/bench-codepoint-width --mode=ziglyph${ARGS} </tmp/ghostty_bench_data" \
-n table \
"./zig-out/bin/bench-codepoint-width --mode=table${ARGS} </tmp/ghostty_bench_data" \
-n simd \
"./zig-out/bin/bench-codepoint-width --mode=simd${ARGS} </tmp/ghostty_bench_data"

View File

@@ -17,6 +17,7 @@ const ArenaAllocator = std.heap.ArenaAllocator;
const ziglyph = @import("ziglyph");
const cli = @import("../cli.zig");
const simd = @import("../simd/main.zig");
const table = @import("../unicode/main.zig").table;
const UTF8Decoder = @import("../terminal/UTF8Decoder.zig");
const Args = struct {
@@ -53,6 +54,8 @@ const Mode = enum {
/// Our SIMD implementation.
simd,
table,
};
pub const std_options = struct {
@@ -82,6 +85,7 @@ pub fn main() !void {
.utf8proc => try benchUtf8proc(reader, buf),
.ziglyph => try benchZiglyph(reader, buf),
.simd => try benchSimd(reader, buf),
.table => try benchTable(reader, buf),
}
}
@@ -153,6 +157,30 @@ noinline fn benchUtf8proc(
}
}
noinline fn benchTable(
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 = table.get(@intCast(cp)).width;
// Write the width to the buffer to avoid it being compiled away
buf[0] = @intCast(width);
}
}
}
}
noinline fn benchZiglyph(
reader: anytype,
buf: []u8,