From c2481748fbc90a5981136870d76cfac31d60a6d3 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 01:29:58 -0700 Subject: [PATCH] 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 --- bench/pp_bench.zig | 9 +++- src/front/lexer.zig | 5 +- src/front/parser.zig | 119 ++++++++++++++++++++++++++++++------------- 3 files changed, 93 insertions(+), 40 deletions(-) diff --git a/bench/pp_bench.zig b/bench/pp_bench.zig index e631461..c925128 100644 --- a/bench/pp_bench.zig +++ b/bench/pp_bench.zig @@ -1,7 +1,7 @@ //! Preprocessor benchmark: lexes / preprocesses a set of files in-process //! and reports the best-of-N wall time and throughput. //! -//! Usage: pp_bench [-mode lex|pp|print] [-iters N] +//! Usage: pp_bench [-mode lex|load|pp|print|parse] [-iters N] //! 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. @@ -10,7 +10,7 @@ const Io = std.Io; const zdxc = @import("zdxc"); 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. 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 pp = front.preproc.Preprocessor.init(arena, &sm, &diags, .{}); 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) { var out: Io.Writer.Allocating = .init(arena); try front.pp_print.print(&pp, &out.writer); diff --git a/src/front/lexer.zig b/src/front/lexer.zig index 9243a47..557267d 100644 --- a/src/front/lexer.zig +++ b/src/front/lexer.zig @@ -95,8 +95,9 @@ pub const Token = struct { return t.kind == kind; } - pub fn isIdent(t: Token, name: []const u8) bool { - return t.kind == .identifier and std.mem.eql(u8, t.text, name); + pub inline fn isIdent(t: Token, name: []const u8) bool { + // 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); } }; diff --git a/src/front/parser.zig b/src/front/parser.zig index 125307d..aa185b4 100644 --- a/src/front/parser.zig +++ b/src/front/parser.zig @@ -1555,47 +1555,51 @@ fn isFloatLiteral(text: []const u8) bool { } /// Words that can never be identifiers. +const reserved_words = [_][]const u8{ + "if", "else", "for", "while", "do", "switch", "case", + "default", "break", "continue", "return", "discard", "struct", "class", + "interface", "cbuffer", "tbuffer", "typedef", "namespace", "enum", "template", + "typename", "true", "false", "this", "sizeof", "static", "const", + "uniform", "extern", "volatile", "groupshared", "inline", "using", "operator", + "static_assert", "inout", "in", "out", +}; + +/// Qualifier keywords and their ast.Qualifiers field names. +const qualifier_words = [_][2][]const u8{ + .{ "const", "const" }, .{ "static", "static" }, .{ "uniform", "uniform" }, + .{ "extern", "extern" }, .{ "volatile", "volatile" }, .{ "precise", "precise" }, + .{ "groupshared", "groupshared" }, .{ "shared", "shared" }, .{ "in", "in" }, + .{ "out", "out" }, .{ "inout", "in" }, .{ "row_major", "row_major" }, + .{ "column_major", "column_major" }, .{ "nointerpolation", "nointerpolation" }, .{ "linear", "linear" }, + .{ "centroid", "centroid" }, .{ "sample", "sample" }, .{ "noperspective", "noperspective" }, + .{ "globallycoherent", "globallycoherent" }, .{ "reordercoherent", "reordercoherent" }, .{ "inline", "inline" }, + .{ "export", "export" }, .{ "point", "point" }, .{ "line", "line" }, + .{ "triangle", "triangle" }, .{ "lineadj", "lineadj" }, .{ "triangleadj", "triangleadj" }, + .{ "vertices", "vertices" }, .{ "indices", "indices" }, .{ "primitives", "primitives" }, + .{ "payload", "payload" }, +}; + +/// Qualifiers that are ordinary identifiers unless a type follows. +const contextual_words = [_][]const u8{ + "precise", "shared", "row_major", "column_major", "nointerpolation", "linear", "centroid", + "sample", "noperspective", "globallycoherent", "reordercoherent", "export", "point", "line", + "triangle", "lineadj", "triangleadj", "vertices", "indices", "primitives", "payload", +}; + fn isReserved(w: []const u8) bool { - const words = [_][]const u8{ - "if", "else", "for", "while", "do", "switch", "case", - "default", "break", "continue", "return", "discard", "struct", "class", - "interface", "cbuffer", "tbuffer", "typedef", "namespace", "enum", "template", - "typename", "true", "false", "this", "sizeof", "static", "const", - "uniform", "extern", "volatile", "groupshared", "inline", "using", "operator", - "static_assert", "inout", "in", "out", - }; - for (words) |k| if (std.mem.eql(u8, w, k)) return true; - return false; + 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 map = [_][2][]const u8{ - .{ "const", "const" }, .{ "static", "static" }, .{ "uniform", "uniform" }, - .{ "extern", "extern" }, .{ "volatile", "volatile" }, .{ "precise", "precise" }, - .{ "groupshared", "groupshared" }, .{ "shared", "shared" }, .{ "in", "in" }, - .{ "out", "out" }, .{ "inout", "in" }, .{ "row_major", "row_major" }, - .{ "column_major", "column_major" }, .{ "nointerpolation", "nointerpolation" }, .{ "linear", "linear" }, - .{ "centroid", "centroid" }, .{ "sample", "sample" }, .{ "noperspective", "noperspective" }, - .{ "globallycoherent", "globallycoherent" }, .{ "reordercoherent", "reordercoherent" }, .{ "inline", "inline" }, - .{ "export", "export" }, .{ "point", "point" }, .{ "line", "line" }, - .{ "triangle", "triangle" }, .{ "lineadj", "lineadj" }, .{ "triangleadj", "triangleadj" }, - .{ "vertices", "vertices" }, .{ "indices", "indices" }, .{ "primitives", "primitives" }, - .{ "payload", "payload" }, - }; - for (map) |e| if (std.mem.eql(u8, w, e[0])) return e[1]; - return null; + const info = word_map.get(w) orelse return null; + return info.qualifier; } -/// Qualifiers that are ordinary identifiers unless a type follows. fn isContextualQualifier(w: []const u8) bool { - const words = [_][]const u8{ - "precise", "shared", "row_major", "column_major", "nointerpolation", "linear", "centroid", - "sample", "noperspective", "globallycoherent", "reordercoherent", "export", "point", "line", - "triangle", "lineadj", "triangleadj", "vertices", "indices", "primitives", "payload", - }; - for (words) |k| if (std.mem.eql(u8, w, k)) return true; - return false; + const info = word_map.get(w) orelse return false; + return info.contextual; } const scalar_types = [_][]const u8{ @@ -1675,19 +1679,61 @@ const template_types = [_][]const u8{ /// Builtin function templates called with explicit arguments. 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 { for (list) |k| if (std.mem.eql(u8, w, k)) return true; 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 { - 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, `<1-4>` vectors, `<1-4>x<1-4>` matrices and /// builtin object types. 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; const last = w[w.len - 1]; 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 { - 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 {