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

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

Merged
merged 1 commit into from
Jun 5, 2023

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Jun 2, 2023

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 inferTypes not 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?

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jun 2, 2023
if (!((result as ObjectFlagsType).objectFlags & ObjectFlags.CouldContainTypeVariablesComputed)) {
(result as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed |
(resultCouldContainTypeVariables ? ObjectFlags.CouldContainTypeVariables : 0);
}
}
Copy link
Member Author

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 ((result as ObjectFlagsType).objectFlags & ObjectFlags.CouldContainTypeVariablesComputed) {
    resultCouldContainTypeVariables &&= !!((result as ObjectFlagsType).objectFlags & ObjectFlags.CouldContainTypeVariables);
}
(result as ObjectFlagsType).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
Member Author

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
Member Author

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
Member Author

@typescript-bot test this
@typescript-bot test top100
@typescript-bot user test this
@typescript-bot run dt
@typescript-bot perf test this faster
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 2, 2023

Hey @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
Collaborator

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

Here they are:

Comparison Report - main..54507

Metric main 54507 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 365,511k (± 0.02%) 365,536k (± 0.02%) ~ 365,475k 365,610k p=0.336 n=6
Parse Time 3.54s (± 0.34%) 3.57s (± 0.38%) +0.02s (+ 0.66%) 3.55s 3.59s p=0.018 n=6
Bind Time 1.18s (± 0.54%) 1.18s (± 0.35%) ~ 1.17s 1.18s p=0.673 n=6
Check Time 9.58s (± 0.35%) 9.58s (± 0.64%) ~ 9.52s 9.69s p=0.747 n=6
Emit Time 7.89s (± 0.58%) 7.91s (± 0.71%) ~ 7.85s 7.99s p=0.625 n=6
Total Time 22.19s (± 0.25%) 22.23s (± 0.43%) ~ 22.12s 22.40s p=0.518 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 192,922k (± 0.02%) 193,461k (± 0.72%) ~ 192,871k 196,306k p=0.423 n=6
Parse Time 1.60s (± 0.68%) 1.60s (± 1.00%) ~ 1.57s 1.61s p=1.000 n=6
Bind Time 0.83s (± 0.49%) 0.83s (± 0.62%) ~ 0.82s 0.83s p=0.595 n=6
Check Time 10.20s (± 0.78%) 10.17s (± 0.52%) ~ 10.10s 10.23s p=0.630 n=6
Emit Time 3.00s (± 0.84%) 2.99s (± 0.72%) ~ 2.96s 3.01s p=0.256 n=6
Total Time 15.63s (± 0.57%) 15.58s (± 0.38%) ~ 15.50s 15.66s p=0.423 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,899k (± 0.01%) 345,893k (± 0.00%) ~ 345,873k 345,911k p=0.689 n=6
Parse Time 2.73s (± 0.56%) 2.74s (± 0.75%) ~ 2.71s 2.76s p=0.328 n=6
Bind Time 1.08s (± 0.50%) 1.08s (± 0.70%) ~ 1.07s 1.09s p=0.476 n=6
Check Time 7.85s (± 0.42%) 7.84s (± 0.20%) ~ 7.82s 7.86s p=0.194 n=6
Emit Time 4.46s (± 0.73%) 4.45s (± 0.83%) ~ 4.40s 4.50s p=0.421 n=6
Total Time 16.12s (± 0.30%) 16.11s (± 0.37%) ~ 16.01s 16.18s p=0.747 n=6
TFS - node (v16.17.1, x64)
Memory used 299,953k (± 0.00%) 299,965k (± 0.01%) ~ 299,947k 299,987k p=0.149 n=6
Parse Time 2.16s (± 0.86%) 2.16s (± 0.54%) ~ 2.15s 2.18s p=0.806 n=6
Bind Time 1.24s (± 0.61%) 1.23s (± 0.85%) ~ 1.22s 1.25s p=0.611 n=6
Check Time 7.27s (± 0.36%) 7.27s (± 0.28%) ~ 7.24s 7.29s p=0.681 n=6
Emit Time 4.37s (± 0.76%) 4.34s (± 0.72%) ~ 4.28s 4.37s p=0.145 n=6
Total Time 15.03s (± 0.36%) 15.00s (± 0.32%) ~ 14.94s 15.05s p=0.422 n=6
material-ui - node (v16.17.1, x64)
Memory used 481,027k (± 0.00%) 480,982k (± 0.01%) -45k (- 0.01%) 480,936k 481,015k p=0.020 n=6
Parse Time 3.25s (± 0.45%) 3.26s (± 0.41%) ~ 3.24s 3.28s p=0.279 n=6
Bind Time 0.93s (± 0.55%) 0.94s (± 0.43%) +0.01s (+ 0.89%) 0.94s 0.95s p=0.022 n=6
Check Time 17.83s (± 0.41%) 17.78s (± 0.50%) ~ 17.71s 17.94s p=0.227 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.01s (± 0.32%) 21.98s (± 0.43%) ~ 21.90s 22.16s p=0.336 n=6
xstate - node (v16.17.1, x64)
Memory used 560,661k (± 0.01%) 560,596k (± 0.02%) ~ 560,481k 560,702k p=0.378 n=6
Parse Time 3.98s (± 0.40%) 4.00s (± 0.37%) ~ 3.98s 4.02s p=0.053 n=6
Bind Time 1.76s (± 0.43%) 1.76s (± 0.56%) ~ 1.75s 1.78s p=0.652 n=6
Check Time 3.05s (± 0.60%) 3.04s (± 0.78%) ~ 3.01s 3.07s p=0.414 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.88s (± 0.17%) 8.89s (± 0.43%) ~ 8.85s 8.94s p=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)
Benchmark Name Iterations
Current 54507 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

@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
Collaborator

@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
Collaborator

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

@capaj
Copy link

capaj commented Jun 5, 2023

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
Member

@weswigham weswigham left a comment

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
Member Author

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
Member Author

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

@weswigham
Copy link
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
Member Author

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
Member Author

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 jakebailey merged commit e3c5209 into microsoft:main Jun 5, 2023
@jakebailey jakebailey deleted the fix-54348-2 branch June 5, 2023 22:31
@jakebailey
Copy link
Member Author

@typescript-bot cherry-pick this to release-5.1

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 5, 2023

Heya @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
Collaborator

Hey @jakebailey, I've opened #54545 for you.

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
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>
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
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