Skip to content

Compile time: interfaces and one very large function (2,000 interfaces: 7.3 s -> 5.0 s; 16,000 ifs: 21.7 s -> 11.2 s) - #866

Merged
ASDAlexander77 merged 5 commits into
mainfrom
perf-compile-time-5
Nov 15, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
perf-compile-time-5

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Follow-up to #862-#865. This fixes the two cases those left growing faster than the source: interfaces, in MLIR generation, and one very large function.

Object file, no optimisation, --no-default-lib, Windows x64, Release compiler:

Input Before After
2,000 interfaces, each with a class and a use 7.3 s 5.0 s
one function of 2,000 if statements 1.7 s 0.9 s
one function of 8,000 if statements 6.9 s (after the first of the two commits for it) 4.8 s
one function of 16,000 if statements 21.7 s (as above) 11.2 s

All 14 shapes of tslang/benchmarks/compile-time/shapes.ps1 now take 1.7-2.3 times as long for twice the source at n = 1,000 to 2,000.

The fixes

MLIR generation tables each module's symbols. It asked theModule.lookupSymbol "is there a symbol of this name?" for every stub of a null interface's vtable, every thunk and every instance of a generic function, and that lookup walks the module. It now keeps the symbols of each module by name (symbolsOfModule, lookupModuleSymbol): a function is heard of at pushFunction, the one way a function enters a module, and a global at createGlobalVariable. A name that is not in the table is not in the module. A name with no operation recorded - a global of a dummy run, which is erased unseen, or two symbols of one name - is still looked up by the scan.

An exception's records are made once per type and module (mlirGenRttiForType). Every throw and typed catch handed its type to MLIRRTTIHelperVC, which looked each record up by name and scanned the module for the last global, and made nothing because the records were there. The helper makes its globals unseen, so the symbol table is made again by a scan when a call of it added to the module.

MLIR generation of 2,000 interfaces took 3.1 s, and 3.6 times that of 1,000; it takes 1.0 s and doubles with the source.

A block is split in one step. Lowering an if, a loop, a switch, a label or a return splits the block it is in. Under the conversion driver rewriter.splitBlock moves the rest of the block one operation at a time and records every move for undo, so a block of n ifs moved n^2/2 operations. These patterns now call Block::splitBlock. If the conversion fails elsewhere and is undone, the block stays split; the pass has failed and the function is not used. The try lowering is unchanged.

A function's blocks are split before its lowering. Even one splice sets the parent of every operation it moves, which was a quarter of the samples for a function of 8,000 ifs. The function's own blocks are now split before the conversion (splitFunctionBlocksAhead), after each operation whose lowering splits, from the last of a block to the first, each part ending in a branch to the next. The pattern of such an operation erases the branch and takes the block it went to as its continuation. Blocks of nested regions are left as generated.

docs/compile-time-work.md is updated; shapes.ps1 -Sizes accepts a comma list under pwsh -File.

Testing

On the last commit:

  • Release suite on Windows x64 with TSLANG_VERIFY_LLVM_GLOBALS_INDEX set, which now also checks every answer of the new symbol table against the scan and calls the exception-record helper again for a type it was called for (it must add nothing): no difference logged.
  • Release suite without it: 7,445 tests. Five test-borrow-facts-* failures, on my machine only (the build cache's CMAKE_AR names an MSVC toolset that is no longer installed; they pass with the installed lib.exe). One access violation in test-own-no-counting-own_field_chain_let, which is not from this change: see below.
  • LLVM IR from the compiler before Compile time: no module scan for every new global (2,000 classes: 100 s -> 3 s) #862 and from this branch, for the 150 largest tests, the ray tracer benchmark and a generated file, under default options, --opt_level=3 and -mm=rc: 450 of 453 identical. The other three are 00param_assigned_owned.ts, whose output differs from run to run with any compiler.
  • A compile error raised in the lowering pass (assignment to a property with no setter, after an if) gives the same message and exit code as before.

Not covered

  • Not built or tested on Linux, and not run with a Debug-built compiler. The Linux exception-record helper was read (each record sits behind an "already there" lookup), not run.
  • No test is added: the changes are in compile time only.
  • One function above 4,000 control-flow statements still grows about 2.5 times per doubling: TypeScriptToLLVMLoweringPass and the canonicalizer grow about 3 times there. Not looked at.
  • Found while testing, not fixed: tslang --emit=llvm --no-default-lib -mm=own tslang/test/tester/own/own_field_chain_let.ts ends with an access violation about once in 300 to 500 runs. The compiler from before Compile time: no module scan for every new global (2,000 classes: 100 s -> 3 s) #862 does the same (2 of 1,000 runs).

🤖 Generated with Claude Code

ASDAlexander77 and others added 5 commits November 15, 2026 18:03
… and the plan for a table of them

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…erfaces: 7.3 s -> 5.0 s)

MLIR generation asked theModule.lookupSymbol "is there a symbol of this name?"
for every stub of a null interface's vtable, every thunk and every instance of a
generic function, and the lookup walks the module. It now keeps the symbols of
each module by name (symbolsOfModule, lookupModuleSymbol): a function is heard
of at pushFunction, the one way a function enters a module, and a global at
createGlobalVariable. A name that is not in the table is not in the module. A
name with no operation in it - a global of a dummy run, which is erased unseen,
or two symbols of one name - is still looked up by the scan.

An exception's records are made once for a type and a module
(mlirGenRttiForType). Every `throw` and typed `catch` handed its type to
MLIRRTTIHelperVC, which looked each record up by name and scanned the module
for the last global, to make nothing: the records were there. The helper makes
its globals unseen, so the symbol table is made again when a call of it added
to the module, and the last global of the module stays remembered when it is
not called.

MLIR generation of 2,000 interfaces took 3.1 s and 3.6 times that for twice as
many; it takes 1.0 s and doubles with the source.

TSLANG_VERIFY_LLVM_GLOBALS_INDEX checks every answer of the table against the
scan, and calls the helper again for a type it was called for: it must add
nothing. Release suite on Windows x64 with and without it; LLVM IR unchanged on
the 150 largest tests. The Linux helper was read (each record sits behind an
"already there" lookup), not run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…: 1.7 s -> 0.9 s)

Lowering an `if`, a loop, a `switch`, a label or a return splits the block it is
in. Under the conversion driver rewriter.splitBlock moves the rest of the block
into the new one operation by operation and records every move, to undo it if
the conversion fails: a block of n `if`s moved n^2/2 operations. These patterns
do not fail once they have split, so they split with Block::splitBlock, one
splice the driver does not hear of. If the conversion fails elsewhere and is
undone, the block stays split; the pass has failed and the function is not
used - a compile error raised in this pass gives the same message and exit code.
The `try` lowering is unchanged.

TypeScriptToAffineLoweringTSFuncPass on one function of 2,000 `if`s took 0.89 s
and 3.9 times that of 1,000; the whole compile now takes 0.9 s and doubles with
the source up to 4,000 (8,000 is 3.0x that, not looked at).

shapes.ps1: -Sizes accepts a comma list when run with pwsh -File.

Release suite on Windows x64, with and without TSLANG_VERIFY_LLVM_GLOBALS_INDEX;
LLVM IR unchanged on the 150 largest tests.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…n twice 4,000

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…00 ifs: 21.7 s -> 11.2 s)

Block::splitBlock is one splice, but the splice sets the parent of every
operation it moves, so lowering n `if`s of one block was still n^2/2 steps: a
quarter of the samples for a function of 8,000.

The function's own blocks are now split before the conversion starts
(splitFunctionBlocksAhead): after each operation whose lowering splits its
block, from the last one of a block to the first, so that each split moves only
what lies between two of them; each part ends in a branch to the next, since the
conversion driver walks blocks in dominance order. The pattern of such an
operation erases that branch and takes the block it went to as the block it
would have split off. Blocks of nested regions are left as they were generated:
the patterns of the operations around them expect that.

One function of n `if`s: 4.8 s at 8,000 and 11.2 s at 16,000, from 6.9 s and
21.7 s; TypeScriptToAffineLoweringTSFuncPass grew 7.3x from 4,000 to 8,000 and
now grows with the source.

Release suite on Windows x64, with and without TSLANG_VERIFY_LLVM_GLOBALS_INDEX;
LLVM IR unchanged on the 150 largest tests. One run had an access violation in
test-own-no-counting-own_field_chain_let: it happens about once in 300 to 500
runs with the compiler from before #862 too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 350fd0f into main Nov 15, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the perf-compile-time-5 branch November 15, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant