Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache instantiation expression types early enough to prevent reentrancy during printback #59931

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

weswigham
Copy link
Member

Fixes #59821

let hasSomeApplicableSignature = false;
let nonApplicableType: Type | undefined;
const result = getInstantiatedType(exprType);
links.instantiationExpressionTypes.set(exprType.id, result);
const errorType = hasSomeApplicableSignature ? nonApplicableType : exprType;
if (errorType) {
diagnostics.add(createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Type_0_has_no_signatures_for_which_the_type_argument_list_is_applicable, typeToString(errorType)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of the stack overflow was basically that the typeToString on this line eventually recursively calls into getInstantiationExpressionType to make the version of the type that's a member of the mapped type being printed, which is identical, but re-triggers the error emit and then spins.

@weswigham
Copy link
Member Author

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 10, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

Comment on lines +37404 to +37408
if (!links.instantiationExpressionTypes) {
links.instantiationExpressionTypes = new Map();
}
if (links.instantiationExpressionTypes.has(exprType.id)) {
return links.instantiationExpressionTypes.get(exprType.id)!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!links.instantiationExpressionTypes) {
links.instantiationExpressionTypes = new Map();
}
if (links.instantiationExpressionTypes.has(exprType.id)) {
return links.instantiationExpressionTypes.get(exprType.id)!;
links.instantiationExpressionTypes ??= new Map();
const cachedType = links.instantiationExpressionTypes.get(exprType.id);
if (cachedType) {
return cachedType;

@@ -6238,6 +6238,7 @@ export interface NodeLinks {
potentialReflectCollisions?: Node[];
potentialUnusedRenamedBindingElementsInTypes?: BindingElement[];
externalHelpersModule?: Symbol; // Resolved symbol for the external helpers module
instantiationExpressionTypes?: Map<number, Type>; // Cache of instantiation expression types for the node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason you cache by IDs instead of by Type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Historic convention and habit, mostly. I can swap it if you'd like it.

@typescript-bot
Copy link
Collaborator

@weswigham
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,153 62,153 ~ ~ ~ p=1.000 n=6
Types 50,242 50,242 ~ ~ ~ p=1.000 n=6
Memory used 193,241k (± 0.74%) 194,279k (± 0.96%) ~ 192,459k 195,995k p=0.810 n=6
Parse Time 1.30s (± 0.75%) 1.29s (± 1.77%) ~ 1.25s 1.31s p=0.402 n=6
Bind Time 0.71s 0.71s (± 0.57%) ~ 0.71s 0.72s p=0.405 n=6
Check Time 9.60s (± 0.39%) 9.58s (± 0.71%) ~ 9.52s 9.69s p=0.374 n=6
Emit Time 2.74s (± 1.01%) 2.74s (± 0.45%) ~ 2.72s 2.75s p=0.870 n=6
Total Time 14.34s (± 0.25%) 14.32s (± 0.61%) ~ 14.24s 14.44s p=0.470 n=6
angular-1 - node (v18.15.0, x64)
Errors 7 7 ~ ~ ~ p=1.000 n=6
Symbols 945,753 945,729 -24 (- 0.00%) ~ ~ p=0.001 n=6
Types 410,067 410,043 -24 (- 0.01%) ~ ~ p=0.001 n=6
Memory used 1,222,743k (± 0.00%) 1,222,738k (± 0.00%) ~ 1,222,684k 1,222,784k p=0.936 n=6
Parse Time 6.64s (± 0.74%) 6.65s (± 0.88%) ~ 6.57s 6.75s p=1.000 n=6
Bind Time 1.86s (± 0.28%) 1.86s (± 0.34%) ~ 1.85s 1.87s p=0.386 n=6
Check Time 31.18s (± 0.39%) 31.11s (± 0.40%) ~ 30.87s 31.21s p=0.688 n=6
Emit Time 15.09s (± 0.47%) 15.06s (± 0.56%) ~ 14.96s 15.19s p=0.377 n=6
Total Time 54.76s (± 0.22%) 54.68s (± 0.22%) ~ 54.51s 54.80s p=0.423 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,515,550 2,515,550 ~ ~ ~ p=1.000 n=6
Types 933,023 933,023 ~ ~ ~ p=1.000 n=6
Memory used 2,350,318k (± 0.00%) 2,350,386k (± 0.00%) ~ 2,350,267k 2,350,538k p=0.298 n=6
Parse Time 11.13s (± 0.26%) 11.10s (± 0.17%) ~ 11.08s 11.13s p=0.124 n=6
Bind Time 2.59s (± 0.40%) 2.59s (± 0.68%) ~ 2.56s 2.61s p=0.402 n=6
Check Time 85.12s (± 0.36%) 85.39s (± 0.24%) ~ 85.03s 85.60s p=0.170 n=6
Emit Time 0.33s (± 2.29%) 0.33s (± 2.29%) ~ 0.32s 0.34s p=1.000 n=6
Total Time 99.17s (± 0.33%) 99.41s (± 0.21%) ~ 99.05s 99.62s p=0.173 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,250,213 1,250,227 +14 (+ 0.00%) ~ ~ p=0.001 n=6
Types 265,146 265,146 ~ ~ ~ p=1.000 n=6
Memory used 2,402,315k (± 0.01%) 2,402,883k (± 0.02%) ~ 2,402,197k 2,403,373k p=0.066 n=6
Parse Time 5.09s (± 0.57%) 5.11s (± 0.85%) ~ 5.05s 5.17s p=0.575 n=6
Bind Time 1.91s (± 0.81%) 1.90s (± 0.44%) ~ 1.89s 1.91s p=0.051 n=6
Check Time 34.83s (± 0.19%) 34.82s (± 0.30%) ~ 34.71s 35.00s p=0.688 n=6
Emit Time 3.32s (± 1.22%) 3.36s (± 4.75%) ~ 3.25s 3.68s p=0.872 n=6
Total Time 45.14s (± 0.23%) 45.19s (± 0.62%) ~ 45.00s 45.75s p=0.689 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,250,213 1,250,227 +14 (+ 0.00%) ~ ~ p=0.001 n=6
Types 265,146 265,146 ~ ~ ~ p=1.000 n=6
Memory used 2,477,545k (± 0.05%) 2,477,016k (± 0.04%) ~ 2,475,900k 2,478,345k p=0.471 n=6
Parse Time 6.35s (± 0.68%) 6.35s (± 0.43%) ~ 6.31s 6.39s p=0.936 n=6
Bind Time 2.03s (± 0.70%) 2.04s (± 0.50%) ~ 2.03s 2.06s p=0.141 n=6
Check Time 41.29s (± 0.39%) 41.37s (± 0.48%) ~ 41.11s 41.65s p=0.378 n=6
Emit Time 4.04s (± 4.09%) 4.12s (± 4.55%) ~ 3.98s 4.43s p=0.575 n=6
Total Time 53.74s (± 0.38%) 53.91s (± 0.35%) ~ 53.66s 54.17s p=0.230 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 259,449 259,452 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 105,986 105,986 ~ ~ ~ p=1.000 n=6
Memory used 433,792k (± 0.01%) 433,771k (± 0.02%) ~ 433,692k 433,894k p=0.575 n=6
Parse Time 2.85s (± 0.99%) 2.84s (± 0.79%) ~ 2.82s 2.88s p=1.000 n=6
Bind Time 1.07s (± 0.38%) 1.06s (± 0.78%) ~ 1.05s 1.07s p=0.527 n=6
Check Time 15.40s (± 0.38%) 15.40s (± 0.52%) ~ 15.31s 15.53s p=0.810 n=6
Emit Time 1.40s (± 1.23%) 1.39s (± 1.15%) ~ 1.38s 1.42s p=0.559 n=6
Total Time 20.71s (± 0.25%) 20.69s (± 0.39%) ~ 20.58s 20.80s p=1.000 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 68 68 ~ ~ ~ p=1.000 n=6
Symbols 225,018 225,018 ~ ~ ~ p=1.000 n=6
Types 94,249 94,249 ~ ~ ~ p=1.000 n=6
Memory used 370,272k (± 0.03%) 370,263k (± 0.02%) ~ 370,189k 370,346k p=0.936 n=6
Parse Time 2.76s (± 0.77%) 2.76s (± 0.51%) ~ 2.74s 2.78s p=0.935 n=6
Bind Time 1.57s (± 0.74%) 1.58s (± 0.77%) ~ 1.56s 1.59s p=0.242 n=6
Check Time 15.72s (± 0.20%) 15.76s (± 0.50%) ~ 15.70s 15.91s p=0.573 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.05s (± 0.20%) 20.09s (± 0.37%) ~ 20.04s 20.24s p=0.468 n=6
vscode - node (v18.15.0, x64)
Errors 1 1 ~ ~ ~ p=1.000 n=6
Symbols 3,044,226 3,044,161 -65 (- 0.00%) ~ ~ p=0.001 n=6
Types 1,052,704 1,052,649 -55 (- 0.01%) ~ ~ p=0.001 n=6
Memory used 3,152,425k (± 0.00%) 3,152,421k (± 0.00%) ~ 3,152,350k 3,152,500k p=0.810 n=6
Parse Time 13.82s (± 0.59%) 13.83s (± 0.32%) ~ 13.76s 13.90s p=0.936 n=6
Bind Time 4.35s (± 0.45%) 4.36s (± 0.12%) ~ 4.35s 4.36s p=0.549 n=6
Check Time 81.49s (± 0.61%) 81.25s (± 0.32%) ~ 80.99s 81.73s p=0.378 n=6
Emit Time 22.15s (± 0.62%) 22.09s (± 0.46%) ~ 21.94s 22.24s p=0.747 n=6
Total Time 121.81s (± 0.43%) 121.53s (± 0.27%) ~ 121.28s 122.16s p=0.471 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 277,022 277,022 ~ ~ ~ p=1.000 n=6
Types 112,817 112,817 ~ ~ ~ p=1.000 n=6
Memory used 426,812k (± 0.02%) 426,749k (± 0.01%) ~ 426,681k 426,828k p=0.298 n=6
Parse Time 4.89s (± 0.21%) 4.89s (± 0.47%) ~ 4.87s 4.93s p=0.743 n=6
Bind Time 2.13s (± 0.55%) 2.13s (± 0.97%) ~ 2.12s 2.17s p=1.000 n=6
Check Time 21.78s (± 0.32%) 21.73s (± 0.11%) ~ 21.71s 21.76s p=0.228 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 28.80s (± 0.21%) 28.75s (± 0.17%) ~ 28.70s 28.83s p=0.228 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 539,317 539,317 ~ ~ ~ p=1.000 n=6
Types 178,997 178,997 ~ ~ ~ p=1.000 n=6
Memory used 483,996k (± 0.02%) 484,068k (± 0.04%) ~ 483,860k 484,288k p=0.574 n=6
Parse Time 4.27s (± 0.51%) 4.25s (± 0.43%) ~ 4.23s 4.28s p=0.060 n=6
Bind Time 1.56s (± 0.48%) 1.55s (± 1.13%) ~ 1.52s 1.57s p=0.437 n=6
Check Time 22.52s (± 0.47%) 22.58s (± 0.23%) ~ 22.48s 22.63s p=0.377 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 28.35s (± 0.44%) 28.38s (± 0.19%) ~ 28.28s 28.44s p=0.873 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

Hey @weswigham, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@weswigham Here are the results of running the user tests with tsc comparing main and refs/pull/59931/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@weswigham Here are the results of running the top 400 repos with tsc comparing main and refs/pull/59931/merge:

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Needs merge
Development

Successfully merging this pull request may close these issues.

RangeError: Maximum call stack size exceeded
4 participants