From b8560a5b0547bf0a615cc4887211c5f76ffd335b Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 00:50:49 -0700 Subject: [PATCH] Preprocessor performance, round 2: inline the lexer into lexRaw - Lexer.next is force-inlined into the preprocessor's raw token loop and the _Pragma check no longer runs a string compare on every identifier (pp: -3%). - pp_bench gains a `load` mode (source manager only). Overall vs 93d1e03 (before any tuning), 4764-file DXC test corpus, ReleaseFast, interleaved A/B: lex 13.28 -> 10.34 ms (-22%) load 6.89 -> 3.96 ms (-43%) pp 50.11 -> 38.74 ms (-23%) print 70.78 -> 48.99 ms (-31%) Tried and dropped in this round (no gain): a single-space fast path in Lexer.next, and vector blank skipping limited to runs of 4+ spaces. Co-Authored-By: Claude Opus 5.5 --- bench/pp_bench.zig | 6 +++++- src/front/preproc.zig | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bench/pp_bench.zig b/bench/pp_bench.zig index bce586e..e631461 100644 --- a/bench/pp_bench.zig +++ b/bench/pp_bench.zig @@ -10,7 +10,7 @@ const Io = std.Io; const zdxc = @import("zdxc"); const front = zdxc.front; -const Mode = enum { lex, pp, print }; +const Mode = enum { lex, load, pp, print }; /// Serves each path from memory after the first read. const CachedFs = struct { @@ -98,6 +98,10 @@ fn runOne(gpa: std.mem.Allocator, cfs: *CachedFs, path: []const u8, mode: Mode) } var sm = front.source.SourceManager.init(arena, cfs.fileSystem()); + if (mode == .load) { + const id = try sm.load(path) orelse return 0; + return sm.file(id).line_starts.len; + } var diags = zdxc.Diagnostics.init(arena); var pp = front.preproc.Preprocessor.init(arena, &sm, &diags, .{}); if (!try pp.enterMainFile(path)) return 0; diff --git a/src/front/preproc.zig b/src/front/preproc.zig index c515ed1..7863f89 100644 --- a/src/front/preproc.zig +++ b/src/front/preproc.zig @@ -282,7 +282,7 @@ pub const Preprocessor = struct { } while (pp.includes.items.len > 0) { const frame = &pp.includes.items[pp.includes.items.len - 1]; - const t = frame.lex.next(); + const t = @call(.always_inline, lexer.Lexer.next, .{&frame.lex}); if (t.kind != .eof) return .{ .tok = t, .from_file = true }; // End of file: diagnose unterminated conditionals, pop. while (pp.conds.items.len > frame.cond_base) { @@ -316,7 +316,7 @@ pub const Preprocessor = struct { const r = try pp.lexRawSkipDirectives(); const t = r.tok; if (t.kind != .identifier or t.flags.no_expand) return t; - if (std.mem.eql(u8, t.text, "_Pragma")) { + if (t.text.len == 7 and t.text[0] == '_' and std.mem.eql(u8, t.text, "_Pragma")) { if (try pp.pragmaOperator(t)) |pragma| return pragma; continue; }