Parser performance: keyword map and cheaper isIdent

pp_bench gains a `parse` mode (preprocess + parse). On the 4764-file,
13.1 MB DXC test corpus (ReleaseFast, interleaved A/B):
  parse  130.9 -> 78.2 ms (-40%); the parser's own share 92 -> 39 ms.

- A third of parse time was mem.eql in linear scans over keyword and
  builtin-type lists. All of them now go through one comptime
  std.StaticStringMap of per-word flags (reserved, qualifier, contextual,
  scalar, object, template): -38%.
- Token.isIdent compares lengths inline before calling mem.eql: -3%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 01:29:58 -07:00
parent f6b2e9cf36
commit 39b8e38765
3 changed files with 93 additions and 40 deletions

View File

@ -1,7 +1,7 @@
//! Preprocessor benchmark: lexes / preprocesses a set of files in-process //! Preprocessor benchmark: lexes / preprocesses a set of files in-process
//! and reports the best-of-N wall time and throughput. //! and reports the best-of-N wall time and throughput.
//! //!
//! Usage: pp_bench [-mode lex|pp|print] [-iters N] <file-list.txt> //! Usage: pp_bench [-mode lex|load|pp|print|parse] [-iters N] <file-list.txt>
//! The file list has one path per line. File contents are read once and //! The file list has one path per line. File contents are read once and
//! served from memory, so disk I/O is excluded from the timings. //! served from memory, so disk I/O is excluded from the timings.
@ -10,7 +10,7 @@ const Io = std.Io;
const zdxc = @import("zdxc"); const zdxc = @import("zdxc");
const front = zdxc.front; const front = zdxc.front;
const Mode = enum { lex, load, pp, print }; const Mode = enum { lex, load, pp, print, parse };
/// Serves each path from memory after the first read. /// Serves each path from memory after the first read.
const CachedFs = struct { const CachedFs = struct {
@ -105,6 +105,11 @@ fn runOne(gpa: std.mem.Allocator, cfs: *CachedFs, path: []const u8, mode: Mode)
var diags = zdxc.Diagnostics.init(arena); var diags = zdxc.Diagnostics.init(arena);
var pp = front.preproc.Preprocessor.init(arena, &sm, &diags, .{}); var pp = front.preproc.Preprocessor.init(arena, &sm, &diags, .{});
if (!try pp.enterMainFile(path)) return 0; if (!try pp.enterMainFile(path)) return 0;
if (mode == .parse) {
var parser = try front.parser.Parser.fromPreprocessor(arena, &diags, &pp);
const tu = try parser.parseTranslationUnit();
return tu.decls.len + diags.error_count;
}
if (mode == .print) { if (mode == .print) {
var out: Io.Writer.Allocating = .init(arena); var out: Io.Writer.Allocating = .init(arena);
try front.pp_print.print(&pp, &out.writer); try front.pp_print.print(&pp, &out.writer);

View File

@ -95,8 +95,9 @@ pub const Token = struct {
return t.kind == kind; return t.kind == kind;
} }
pub fn isIdent(t: Token, name: []const u8) bool { pub inline fn isIdent(t: Token, name: []const u8) bool {
return t.kind == .identifier and std.mem.eql(u8, t.text, name); // Length first: most keyword checks fail on it without a call.
return t.kind == .identifier and t.text.len == name.len and std.mem.eql(u8, t.text, name);
} }
}; };

View File

@ -1555,22 +1555,17 @@ fn isFloatLiteral(text: []const u8) bool {
} }
/// Words that can never be identifiers. /// Words that can never be identifiers.
fn isReserved(w: []const u8) bool { const reserved_words = [_][]const u8{
const words = [_][]const u8{
"if", "else", "for", "while", "do", "switch", "case", "if", "else", "for", "while", "do", "switch", "case",
"default", "break", "continue", "return", "discard", "struct", "class", "default", "break", "continue", "return", "discard", "struct", "class",
"interface", "cbuffer", "tbuffer", "typedef", "namespace", "enum", "template", "interface", "cbuffer", "tbuffer", "typedef", "namespace", "enum", "template",
"typename", "true", "false", "this", "sizeof", "static", "const", "typename", "true", "false", "this", "sizeof", "static", "const",
"uniform", "extern", "volatile", "groupshared", "inline", "using", "operator", "uniform", "extern", "volatile", "groupshared", "inline", "using", "operator",
"static_assert", "inout", "in", "out", "static_assert", "inout", "in", "out",
}; };
for (words) |k| if (std.mem.eql(u8, w, k)) return true;
return false;
}
/// Maps a qualifier keyword to its ast.Qualifiers field name. /// Qualifier keywords and their ast.Qualifiers field names.
fn qualifierField(w: []const u8) ?[]const u8 { const qualifier_words = [_][2][]const u8{
const map = [_][2][]const u8{
.{ "const", "const" }, .{ "static", "static" }, .{ "uniform", "uniform" }, .{ "const", "const" }, .{ "static", "static" }, .{ "uniform", "uniform" },
.{ "extern", "extern" }, .{ "volatile", "volatile" }, .{ "precise", "precise" }, .{ "extern", "extern" }, .{ "volatile", "volatile" }, .{ "precise", "precise" },
.{ "groupshared", "groupshared" }, .{ "shared", "shared" }, .{ "in", "in" }, .{ "groupshared", "groupshared" }, .{ "shared", "shared" }, .{ "in", "in" },
@ -1582,20 +1577,29 @@ fn qualifierField(w: []const u8) ?[]const u8 {
.{ "triangle", "triangle" }, .{ "lineadj", "lineadj" }, .{ "triangleadj", "triangleadj" }, .{ "triangle", "triangle" }, .{ "lineadj", "lineadj" }, .{ "triangleadj", "triangleadj" },
.{ "vertices", "vertices" }, .{ "indices", "indices" }, .{ "primitives", "primitives" }, .{ "vertices", "vertices" }, .{ "indices", "indices" }, .{ "primitives", "primitives" },
.{ "payload", "payload" }, .{ "payload", "payload" },
}; };
for (map) |e| if (std.mem.eql(u8, w, e[0])) return e[1];
return null;
}
/// Qualifiers that are ordinary identifiers unless a type follows. /// Qualifiers that are ordinary identifiers unless a type follows.
fn isContextualQualifier(w: []const u8) bool { const contextual_words = [_][]const u8{
const words = [_][]const u8{
"precise", "shared", "row_major", "column_major", "nointerpolation", "linear", "centroid", "precise", "shared", "row_major", "column_major", "nointerpolation", "linear", "centroid",
"sample", "noperspective", "globallycoherent", "reordercoherent", "export", "point", "line", "sample", "noperspective", "globallycoherent", "reordercoherent", "export", "point", "line",
"triangle", "lineadj", "triangleadj", "vertices", "indices", "primitives", "payload", "triangle", "lineadj", "triangleadj", "vertices", "indices", "primitives", "payload",
}; };
for (words) |k| if (std.mem.eql(u8, w, k)) return true;
return false; fn isReserved(w: []const u8) bool {
const info = word_map.get(w) orelse return false;
return info.reserved;
}
/// Maps a qualifier keyword to its ast.Qualifiers field name.
fn qualifierField(w: []const u8) ?[]const u8 {
const info = word_map.get(w) orelse return null;
return info.qualifier;
}
fn isContextualQualifier(w: []const u8) bool {
const info = word_map.get(w) orelse return false;
return info.contextual;
} }
const scalar_types = [_][]const u8{ const scalar_types = [_][]const u8{
@ -1675,19 +1679,61 @@ const template_types = [_][]const u8{
/// Builtin function templates called with explicit arguments. /// Builtin function templates called with explicit arguments.
const template_funcs = [_][]const u8{ "RawBufferLoad", "RawBufferStore", "static_pointer_cast", "reinterpret_pointer_cast" }; const template_funcs = [_][]const u8{ "RawBufferLoad", "RawBufferStore", "static_pointer_cast", "reinterpret_pointer_cast" };
/// Everything known about a keyword or builtin name, from one lookup.
const WordInfo = struct {
reserved: bool = false,
qualifier: ?[]const u8 = null,
contextual: bool = false,
scalar: bool = false,
numeric_scalar: bool = false,
object: bool = false,
template: bool = false,
};
fn inList(list: []const []const u8, w: []const u8) bool { fn inList(list: []const []const u8, w: []const u8) bool {
for (list) |k| if (std.mem.eql(u8, w, k)) return true; for (list) |k| if (std.mem.eql(u8, w, k)) return true;
return false; return false;
} }
/// Compile-time map over every keyword and builtin type / template name;
/// replaces linear scans of the lists above (a third of parse time).
const word_map = blk: {
@setEvalBranchQuota(1_000_000);
var words: []const []const u8 = &(reserved_words ++ contextual_words ++ scalar_types ++ object_types ++ template_types ++ template_funcs);
for (qualifier_words) |q| words = words ++ &[_][]const u8{q[0]};
var kvs: [words.len]struct { []const u8, WordInfo } = undefined;
var n: usize = 0;
outer: for (words) |w| {
for (kvs[0..n]) |kv| if (std.mem.eql(u8, kv[0], w)) continue :outer;
var info: WordInfo = .{
.reserved = inList(&reserved_words, w),
.contextual = inList(&contextual_words, w),
.scalar = inList(&scalar_types, w),
.numeric_scalar = inList(&scalar_types, w) and !std.mem.endsWith(u8, w, "_packed"),
.object = inList(&object_types, w),
.template = inList(&template_types, w) or inList(&template_funcs, w),
};
for (qualifier_words) |q| {
if (std.mem.eql(u8, q[0], w)) info.qualifier = q[1];
}
kvs[n] = .{ w, info };
n += 1;
}
const final = kvs[0..n].*;
break :blk std.StaticStringMap(WordInfo).initComptime(final);
};
fn isNumericScalar(w: []const u8) bool { fn isNumericScalar(w: []const u8) bool {
return inList(&scalar_types, w) and !std.mem.endsWith(u8, w, "_packed"); const info = word_map.get(w) orelse return false;
return info.numeric_scalar;
} }
/// Scalars, `<scalar><1-4>` vectors, `<scalar><1-4>x<1-4>` matrices and /// Scalars, `<scalar><1-4>` vectors, `<scalar><1-4>x<1-4>` matrices and
/// builtin object types. /// builtin object types.
pub fn isBuiltinType(w: []const u8) bool { pub fn isBuiltinType(w: []const u8) bool {
if (inList(&scalar_types, w) or inList(&object_types, w)) return true; if (word_map.get(w)) |info| {
if (info.scalar or info.object) return true;
}
if (w.len < 2) return false; if (w.len < 2) return false;
const last = w[w.len - 1]; const last = w[w.len - 1];
if (last < '1' or last > '4') return false; if (last < '1' or last > '4') return false;
@ -1698,7 +1744,8 @@ pub fn isBuiltinType(w: []const u8) bool {
} }
fn isBuiltinTemplate(w: []const u8) bool { fn isBuiltinTemplate(w: []const u8) bool {
return inList(&template_types, w) or inList(&template_funcs, w); const info = word_map.get(w) orelse return false;
return info.template;
} }
fn unsignedName(a: Allocator, base: []const u8) Allocator.Error![]const u8 { fn unsignedName(a: Allocator, base: []const u8) Allocator.Error![]const u8 {