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 c9c3c1f (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 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 00:50:49 -07:00
parent 226ef16c96
commit 8bc1ae82b8
2 changed files with 7 additions and 3 deletions

View File

@ -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;

View File

@ -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;
}