Skip to content

Commit 70618f3

Browse files
chrisbbreuerclaude
andcommitted
perf: restore integer comparison in comptime_match for power-of-2 sizes
Use fast integer comparison (u8/u16/u32/u64) for 1/2/4/8-byte words. Fall back to std.mem.eql for non-power-of-2 sizes (e.g. 6-byte words like "export") which caused failures with u48 on Linux. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b631d2a commit 70618f3

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

packages/zig-dtsx/src/scanner.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ const Declaration = types.Declaration;
88
const DeclarationKind = types.DeclarationKind;
99
const Allocator = std.mem.Allocator;
1010

11-
/// Comptime-optimized word comparison using std.mem.eql (compiler optimizes small sizes).
11+
/// Comptime-optimized word comparison: uses integer casts for power-of-2 sizes (1/2/4/8 bytes).
1212
inline fn comptime_match(comptime N: usize, src: *const [N]u8, comptime word: []const u8) bool {
13+
// Use integer comparison for power-of-2 sizes (u8, u16, u32, u64)
14+
if (N == 1 or N == 2 or N == 4 or N == 8) {
15+
const T = std.meta.Int(.unsigned, N * 8);
16+
const expected: T = comptime blk: {
17+
var val: T = 0;
18+
for (word, 0..) |b, i| {
19+
val |= @as(T, b) << @intCast(i * 8);
20+
}
21+
break :blk val;
22+
};
23+
return @as(*align(1) const T, @ptrCast(src)).* == expected;
24+
}
1325
return std.mem.eql(u8, src, word);
1426
}
1527

0 commit comments

Comments
 (0)