From faf7bdf87e729080db2fa5b5a77bed92c6abfd46 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 6 Nov 2026 18:01:48 -0800 Subject: [PATCH] Optimize `compiler.mjs` for `__const` dependencies and `isDecorator` Since #28144, every C define in `struct_info.json` is added to `LibraryManager.library` as three entries (`$FOO`, `$FOO__const`, and `$FOO__internal`). Adding hundreds more constants to `struct_info.json` (as in #28199) would otherwise incur two compile-time performance costs in `compiler.mjs`: 1. In `--symbols-only` mode we only need to discover transitive dependencies on native symbols. As JS library functions replace magic numbers with `struct_info.json` constants, `addImplicitDeps` (`src/jsifier.mjs`) would otherwise record every referenced constant in `__deps` and repeatedly traverse them in `getTransitiveDeps`. Skipping `__const` symbols in `--symbols-only` mode avoids this work. 2. Each `struct_info.json` constant adds three keys to `LibraryManager.library`, which is iterated over in multiple passes that call `isDecorator` (`src/utility.mjs`) on every key. Using `lastIndexOf('__')` and a `Set` lookup instead of `decoratorSuffixes.some` with `endsWith` avoids scanning all 15 decorator suffixes for every key in the library. See: #28199 --- src/jsifier.mjs | 1 + src/utility.mjs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/jsifier.mjs b/src/jsifier.mjs index 92ad005490559..c5856a40bc5d8 100644 --- a/src/jsifier.mjs +++ b/src/jsifier.mjs @@ -784,6 +784,7 @@ function(${args}) { function addImplicitDeps(snippet) { for (const dep of calcImplicitDeps(snippet)) { if (dep === symbol) continue; + if (symbolsOnly && LibraryManager.library[dep + '__const']) continue; if (explicitDeps.has(dep)) { if (!symbolsOnly && (STRICT || !isUserSymbol) && LibraryManager.library[symbol] !== dep) { warn(`${symbol}: redundant __deps entry: ${dep}`); diff --git a/src/utility.mjs b/src/utility.mjs index c8ee6f5d4397d..f2ffa07459611 100644 --- a/src/utility.mjs +++ b/src/utility.mjs @@ -227,8 +227,11 @@ export const decoratorSuffixes = [ '__const', ]; +const decoratorSuffixSet = new Set(decoratorSuffixes); + export function isDecorator(ident) { - return decoratorSuffixes.some((suffix) => ident.endsWith(suffix)); + const index = ident.lastIndexOf('__'); + return index > 0 && decoratorSuffixSet.has(ident.slice(index)); } export function defineConsts(consts) {