Skip to content

Ensure we don't overwrite computed CouldContainTypeVariables in new optimization - #54507

Merged
Jake Bailey (jakebailey) merged 1 commit into
microsoft:mainfrom
jakebailey:fix-54348-2
Jun 5, 2023
Merged

Ensure we don't overwrite computed CouldContainTypeVariables in new optimization#54507
Jake Bailey (jakebailey) merged 1 commit into
microsoft:mainfrom
jakebailey:fix-54348-2

Conversation

@jakebailey

@jakebaileyJake Bailey (jakebailey) commented Jun 2, 2023

Copy link
Copy Markdown
Member

Fixes#54348

In this branch (added in #53246), we checked that result didn't already have CouldContainTypeVariables calculated. If not, then we instead calculate it directly by only checking typeArguments.

However, if result happens to be mentioned in typeArguments, the call to couldContainTypeVariables may change result.objectFlags. When this happens, we stomp over the previous computation with our own.

Normally, this is harmless because CouldContainTypeVariables is realistically just an optimization to prevent unnecessary type traversals.

However, result can in fact be neverType or wildcardType (and maybe others)! If we accidentally overwrite the previously computed result with "true" (a more conservative calculation), this can actually cause a problem later, as seen in #54348. This is because inferTypes may end up with two identical source and targets.

In the bug's case, source == target == wildcardType. wildcardType is a special case for inferTypes, which for "reasons" can only stop infinitely recursing by seeing that wildcardType cannot contain type variables. So, if we stomp over a previously calculated "false" answer here with a more conservative "true", what was just a missing optimization turns into a stack overflow. (Theoretically, I think we should also make inferTypesnot depend on this optimization flag and just directly check for the infinite recursion. But, it did catch this bug...)

This PR "fixes" the problem by double checking that someone else hasn't already calculated the value while we were. If it's already calculated, then we just skip over. I also verified that the speedup in material-ui is not broken by this fix (phew).

I haven't been able to produce a decent test case. There are actually 15 test cases in our test suite which hit this new branch, but none with observable behavior differences. All provided tests in the issue are massive and I can't minimize them.

I am also not 100% sure of this fix; most of the time, the optimization from #53246 is operating on a freshly instantiated type (which is why the optimization is valid). But sometimes, it's not actually operating on a new type. Perhaps the "right" fix is to skip this code when the type isn't a newly created one just for this section? Because those types shouldn't be involved in this optimization anyway?

(result as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed |
(resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariables : 0);
}
}

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This could also have been:

if((resultasObjectFlagsType).objectFlags&ObjectFlags.CouldContainTypeVariablesComputed){resultCouldContainTypeVariables&&=!!((resultasObjectFlagsType).objectFlags&ObjectFlags.CouldContainTypeVariables);}(resultasObjectFlagsType).objectFlags|=ObjectFlags.CouldContainTypeVariablesComputed|(resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariables : 0);

e.g., merge the result. But that felt wrong. But, my current fix doesn't feel all correct either. I'm going to investigate and see if there's a specific class of types that we're hitting.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I also have a version which does something... clever. Grab typeCount before obtaining the result, and if its id is greater than the saved typeCount, then you know it's a new type and we can do the calculation. This stems from my thought that this optimization can only possibly be valid on newly created types.

But, that feels really unusual.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Honestly, with that latter solution (given guidance on how to determine what a "new type" is), we could simplify things a bit and just force calculate the value unconditionally, dropping the change in instantiateAnonymousType.

@jakebailey

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based user code test suite on this PR at cffa515. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the parallelized Definitely Typed test suite on this PR at cffa515. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite on this PR at cffa515. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the tarball bundle task on this PR at cffa515. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the abridged perf test suite on this PR at cffa515. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the extended test suite on this PR at cffa515. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
"devDependencies": {
"typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/155328/artifacts?artifactName=tgz&fileId=365D63D132F5C69B3D7866A71B9B2BEE6A72052895416632C1B283C1F1DF8ED302&fileName=/typescript-5.2.0-insiders.20230602.tgz"
}
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.2.0-pr-54507-7".;

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..54507

Metricmain54507DeltaBestWorstp-value
Angular - node (v16.17.1, x64)
Memory used365,511k (± 0.02%)365,536k (± 0.02%)~365,475k365,610kp=0.336 n=6
Parse Time3.54s (± 0.34%)3.57s (± 0.38%)+0.02s (+ 0.66%)3.55s3.59sp=0.018 n=6
Bind Time1.18s (± 0.54%)1.18s (± 0.35%)~1.17s1.18sp=0.673 n=6
Check Time9.58s (± 0.35%)9.58s (± 0.64%)~9.52s9.69sp=0.747 n=6
Emit Time7.89s (± 0.58%)7.91s (± 0.71%)~7.85s7.99sp=0.625 n=6
Total Time22.19s (± 0.25%)22.23s (± 0.43%)~22.12s22.40sp=0.518 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used192,922k (± 0.02%)193,461k (± 0.72%)~192,871k196,306kp=0.423 n=6
Parse Time1.60s (± 0.68%)1.60s (± 1.00%)~1.57s1.61sp=1.000 n=6
Bind Time0.83s (± 0.49%)0.83s (± 0.62%)~0.82s0.83sp=0.595 n=6
Check Time10.20s (± 0.78%)10.17s (± 0.52%)~10.10s10.23sp=0.630 n=6
Emit Time3.00s (± 0.84%)2.99s (± 0.72%)~2.96s3.01sp=0.256 n=6
Total Time15.63s (± 0.57%)15.58s (± 0.38%)~15.50s15.66sp=0.423 n=6
Monaco - node (v16.17.1, x64)
Memory used345,899k (± 0.01%)345,893k (± 0.00%)~345,873k345,911kp=0.689 n=6
Parse Time2.73s (± 0.56%)2.74s (± 0.75%)~2.71s2.76sp=0.328 n=6
Bind Time1.08s (± 0.50%)1.08s (± 0.70%)~1.07s1.09sp=0.476 n=6
Check Time7.85s (± 0.42%)7.84s (± 0.20%)~7.82s7.86sp=0.194 n=6
Emit Time4.46s (± 0.73%)4.45s (± 0.83%)~4.40s4.50sp=0.421 n=6
Total Time16.12s (± 0.30%)16.11s (± 0.37%)~16.01s16.18sp=0.747 n=6
TFS - node (v16.17.1, x64)
Memory used299,953k (± 0.00%)299,965k (± 0.01%)~299,947k299,987kp=0.149 n=6
Parse Time2.16s (± 0.86%)2.16s (± 0.54%)~2.15s2.18sp=0.806 n=6
Bind Time1.24s (± 0.61%)1.23s (± 0.85%)~1.22s1.25sp=0.611 n=6
Check Time7.27s (± 0.36%)7.27s (± 0.28%)~7.24s7.29sp=0.681 n=6
Emit Time4.37s (± 0.76%)4.34s (± 0.72%)~4.28s4.37sp=0.145 n=6
Total Time15.03s (± 0.36%)15.00s (± 0.32%)~14.94s15.05sp=0.422 n=6
material-ui - node (v16.17.1, x64)
Memory used481,027k (± 0.00%)480,982k (± 0.01%)-45k (- 0.01%)480,936k481,015kp=0.020 n=6
Parse Time3.25s (± 0.45%)3.26s (± 0.41%)~3.24s3.28sp=0.279 n=6
Bind Time0.93s (± 0.55%)0.94s (± 0.43%)+0.01s (+ 0.89%)0.94s0.95sp=0.022 n=6
Check Time17.83s (± 0.41%)17.78s (± 0.50%)~17.71s17.94sp=0.227 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time22.01s (± 0.32%)21.98s (± 0.43%)~21.90s22.16sp=0.336 n=6
xstate - node (v16.17.1, x64)
Memory used560,661k (± 0.01%)560,596k (± 0.02%)~560,481k560,702kp=0.378 n=6
Parse Time3.98s (± 0.40%)4.00s (± 0.37%)~3.98s4.02sp=0.053 n=6
Bind Time1.76s (± 0.43%)1.76s (± 0.56%)~1.75s1.78sp=0.652 n=6
Check Time3.05s (± 0.60%)3.04s (± 0.78%)~3.01s3.07sp=0.414 n=6
Emit Time0.09s (± 0.00%)0.09s (± 0.00%)~0.09s0.09sp=1.000 n=6
Total Time8.88s (± 0.17%)8.89s (± 0.43%)~8.85s8.94sp=1.000 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-148-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
BenchmarkNameIterations
Current545076
Baselinemain6

Developer Information:

Download Benchmark

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user test suite comparing main and refs/pull/54507/merge:

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Unknown failure"
  • 1 instance of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/54507/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@capaj

Copy link
Copy Markdown

I can confirm this does fix our infinite recursion problem on our 85k LOC codebase where both 5.0.4 and 5.1.3 fail with infinite recursion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have some gripes after some review about the comment above not matching the actual implementation below (which I totally didn't notice when I reviewed the original PR), and that mismatch is probably causing us to do extra work on unions and intersections that we may not need to do, but that's not necessarily related to this fix. As we discussed, since you can clearly have an input type argument that's the same type as result (eg, an argument to a noop mapped type), you need to conditionalize the check like this. :)

@jakebailey

Copy link
Copy Markdown
MemberAuthor

As we discussed, since you can clearly have an input type argument that's the same type as result (eg, an argument to a noop mapped type), you need to conditionalize the check like this. :)

Just to be clear, you're suggesting keeping my "if the computed flag gets set, bail out" condition?

I am also totally happy to integrate your patch from our DMs; it seems more complete and still safe in general I think.

@jakebailey

Copy link
Copy Markdown
MemberAuthor

Very likely this is superseded by #54538, which covers what I have but better.

@weswigham

Copy link
Copy Markdown
Member

Very likely this is superseded by #54538, which covers what I have but better.

It just already includes this change (verbatim) - this change is, itself, clearly needed and an appropriate fix for the linked issue (moreso than the type id thing, I feel, after looking into our behaviors here); feel free to merge it.

@jakebailey

Copy link
Copy Markdown
MemberAuthor

Very likely this is superseded by #54538, which covers what I have but better.

It just already includes this change (verbatim) - this change is, itself, clearly needed and an appropriate fix for the linked issue (moreso than the type id thing, I feel, after looking into our behaviors here); feel free to merge it.

Gotcha, thanks!

@jakebailey

Copy link
Copy Markdown
MemberAuthor

I'm going to spend some time trying to minimize this into a test case, but, note that theoretically, this is only hurting optimization. It's just that inferTypes relied on that optimization to stop an infinite recursion (which, it could just have easily not done and we would have never noticed this). So, any test case I have may stop testing the crash itself at some point.

@jakebailey
Jake Bailey (jakebailey) merged commit e3c5209 into microsoft:mainJun 5, 2023
@jakebailey
Jake Bailey (jakebailey) deleted the fix-54348-2 branch June 5, 2023 22:31
@jakebailey

Copy link
Copy Markdown
MemberAuthor

TypeScript Bot (@typescript-bot) cherry-pick this to release-5.1

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 5, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the task to cherry-pick this into release-5.1 on this PR at cffa515. You can monitor the build here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), I've opened #54545 for you.

TypeScript Bot (typescript-bot) pushed a commit to typescript-bot/TypeScript that referenced this pull request Jun 5, 2023
Component commits:
cffa515 Ensure we don't overwrite computed CouldContainTypeVariables in new optimization
Daniel Rosenwasser (DanielRosenwasser) pushed a commit that referenced this pull request Jun 7, 2023
…e-5.1 (#54545)
Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Milestone BugPRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS 5.1.1-rc: RangeError: Maximum call stack size exceeded

4 participants

@jakebailey@typescript-bot@capaj@weswigham