Skip to content

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Sep 29, 2022

This PR supersedes #50960 and fixes #50858.

Thanks @mxsdev for calling attention to this issue and proposing a fix. This PR effectively implements the same idea as #50960 in a simpler manner. Also, this PR borrows the tests from #50960.

Fixes #46310.
Fixes #50858.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 29, 2022
@ahejlsberg
Copy link
Member Author

ahejlsberg commented Sep 29, 2022

@mxsdev The simplification in this PR is simply to perform contextual type instantiation for all type parameters that declare default types, even if no inferences have been made for those type parameters.

@DanielRosenwasser
Copy link
Member

(slight typo - @mxsdev)

@ahejlsberg
Copy link
Member Author

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 29, 2022

Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at 58d90d4. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 29, 2022

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at 58d90d4. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 29, 2022

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 58d90d4. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 29, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 29, 2022

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 58d90d4. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/50994/merge:

Everything looks good!

@@ -27534,7 +27534,7 @@ namespace ts {
const inferenceContext = getInferenceContext(node);
// If no inferences have been made, nothing is gained from instantiating as type parameters
// would just be replaced with their defaults similar to the apparent type.
Copy link
Contributor

Choose a reason for hiding this comment

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

is this part of the comment here still relevant? with the code change in this PR this now reads weirdly to me, or there is some additional context missing here - like what kind of default type parameters should be ignored and what kind of default parameters might be useful. Or perhaps this comment was never about defaults and it should mention base constraints?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, that should say constraints, not defaults.

@Andarist
Copy link
Contributor

@ahejlsberg I've created a PR targeting this branch, with an additional test case from this other issue, the PR can be found here: #51002

This PR here should also add #46310 to the issues that it's going to fix

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..50994

Metric main 50994 Delta Best Worst
Angular - node (v14.15.1, x64)
Memory used 338,104k (± 0.01%) 338,101k (± 0.01%) -3k (- 0.00%) 338,071k 338,166k
Parse Time 2.08s (± 0.42%) 2.08s (± 0.81%) +0.00s (+ 0.24%) 2.04s 2.12s
Bind Time 0.80s (± 0.65%) 0.80s (± 0.95%) -0.00s (- 0.38%) 0.79s 0.82s
Check Time 5.92s (± 0.40%) 5.90s (± 0.42%) -0.02s (- 0.37%) 5.84s 5.95s
Emit Time 6.31s (± 0.80%) 6.32s (± 0.55%) +0.01s (+ 0.19%) 6.25s 6.40s
Total Time 15.10s (± 0.43%) 15.09s (± 0.41%) -0.01s (- 0.05%) 14.96s 15.27s
Compiler-Unions - node (v14.15.1, x64)
Memory used 190,218k (± 0.01%) 190,235k (± 0.01%) +17k (+ 0.01%) 190,195k 190,297k
Parse Time 0.86s (± 1.14%) 0.86s (± 0.77%) +0.00s (+ 0.12%) 0.86s 0.89s
Bind Time 0.49s (± 1.17%) 0.49s (± 1.19%) -0.00s (- 0.61%) 0.48s 0.50s
Check Time 6.79s (± 0.51%) 6.78s (± 0.77%) -0.01s (- 0.15%) 6.67s 6.94s
Emit Time 2.41s (± 1.25%) 2.41s (± 0.51%) +0.00s (+ 0.04%) 2.39s 2.44s
Total Time 10.55s (± 0.42%) 10.54s (± 0.58%) -0.01s (- 0.08%) 10.44s 10.73s
Monaco - node (v14.15.1, x64)
Memory used 326,576k (± 0.01%) 326,602k (± 0.01%) +26k (+ 0.01%) 326,498k 326,656k
Parse Time 1.61s (± 1.48%) 1.60s (± 0.53%) -0.01s (- 0.74%) 1.58s 1.62s
Bind Time 0.73s (± 1.21%) 0.73s (± 1.00%) -0.01s (- 0.82%) 0.72s 0.75s
Check Time 5.74s (± 0.88%) 5.76s (± 0.45%) +0.02s (+ 0.35%) 5.71s 5.80s
Emit Time 3.43s (± 1.30%) 3.38s (± 0.85%) -0.05s (- 1.40%) 3.31s 3.43s
Total Time 11.51s (± 0.83%) 11.47s (± 0.42%) -0.05s (- 0.40%) 11.34s 11.55s
TFS - node (v14.15.1, x64)
Memory used 289,694k (± 0.01%) 289,691k (± 0.01%) -3k (- 0.00%) 289,655k 289,763k
Parse Time 1.30s (± 0.51%) 1.31s (± 0.77%) +0.00s (+ 0.15%) 1.29s 1.33s
Bind Time 0.80s (± 0.88%) 0.79s (± 1.99%) -0.01s (- 1.00%) 0.73s 0.81s
Check Time 5.43s (± 0.32%) 5.42s (± 0.27%) -0.01s (- 0.17%) 5.39s 5.45s
Emit Time 3.63s (± 0.63%) 3.62s (± 0.69%) -0.01s (- 0.39%) 3.58s 3.71s
Total Time 11.17s (± 0.29%) 11.14s (± 0.35%) -0.03s (- 0.28%) 11.06s 11.26s
material-ui - node (v14.15.1, x64)
Memory used 435,627k (± 0.00%) 435,653k (± 0.00%) +26k (+ 0.01%) 435,620k 435,698k
Parse Time 1.89s (± 0.35%) 1.89s (± 0.39%) +0.00s (+ 0.16%) 1.88s 1.91s
Bind Time 0.59s (± 0.76%) 0.58s (± 0.62%) -0.00s (- 0.68%) 0.58s 0.59s
Check Time 12.89s (± 0.53%) 13.01s (± 0.56%) +0.11s (+ 0.87%) 12.85s 13.16s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.37s (± 0.42%) 15.48s (± 0.52%) +0.11s (+ 0.74%) 15.31s 15.65s
xstate - node (v14.15.1, x64)
Memory used 544,097k (± 0.00%) 544,092k (± 0.00%) -5k (- 0.00%) 544,057k 544,174k
Parse Time 2.63s (± 0.41%) 2.62s (± 0.32%) -0.01s (- 0.42%) 2.60s 2.64s
Bind Time 0.99s (± 1.01%) 0.98s (± 1.23%) -0.01s (- 0.61%) 0.96s 1.01s
Check Time 1.52s (± 0.39%) 1.52s (± 0.66%) 0.00s ( 0.00%) 1.51s 1.55s
Emit Time 0.07s (± 3.14%) 0.07s (± 3.14%) 0.00s ( 0.00%) 0.07s 0.08s
Total Time 5.23s (± 0.21%) 5.20s (± 0.47%) -0.03s (- 0.52%) 5.16s 5.26s
Angular - node (v10.16.3, x64)
Memory used 362,522k (± 0.02%) 362,438k (± 0.02%) -84k (- 0.02%) 362,290k 362,573k
Parse Time 2.10s (± 0.59%) 2.10s (± 0.65%) +0.00s (+ 0.00%) 2.08s 2.14s
Bind Time 0.78s (± 1.13%) 0.79s (± 0.84%) +0.01s (+ 1.28%) 0.78s 0.81s
Check Time 6.26s (± 0.41%) 6.25s (± 0.51%) -0.01s (- 0.11%) 6.17s 6.32s
Emit Time 6.10s (± 0.63%) 6.13s (± 0.59%) +0.03s (+ 0.48%) 6.06s 6.21s
Total Time 15.24s (± 0.38%) 15.27s (± 0.41%) +0.03s (+ 0.21%) 15.10s 15.42s
Compiler-Unions - node (v10.16.3, x64)
Memory used 206,017k (± 0.29%) 206,381k (± 0.04%) +364k (+ 0.18%) 206,146k 206,535k
Parse Time 0.84s (± 0.81%) 0.84s (± 0.59%) +0.00s (+ 0.12%) 0.84s 0.86s
Bind Time 0.47s (± 0.85%) 0.47s (± 1.19%) -0.00s (- 0.21%) 0.46s 0.48s
Check Time 8.51s (± 0.47%) 8.52s (± 0.64%) +0.01s (+ 0.11%) 8.43s 8.63s
Emit Time 2.50s (± 0.97%) 2.44s (± 0.88%) -0.05s (- 2.20%) 2.40s 2.48s
Total Time 12.32s (± 0.40%) 12.27s (± 0.48%) -0.05s (- 0.37%) 12.17s 12.39s
Monaco - node (v10.16.3, x64)
Memory used 344,839k (± 0.02%) 344,808k (± 0.02%) -31k (- 0.01%) 344,621k 344,963k
Parse Time 1.61s (± 0.50%) 1.61s (± 0.78%) +0.01s (+ 0.44%) 1.59s 1.64s
Bind Time 0.71s (± 0.95%) 0.71s (± 0.71%) -0.00s (- 0.28%) 0.69s 0.71s
Check Time 6.24s (± 0.51%) 6.28s (± 0.36%) +0.04s (+ 0.58%) 6.22s 6.33s
Emit Time 3.25s (± 0.91%) 3.25s (± 0.64%) +0.01s (+ 0.25%) 3.21s 3.31s
Total Time 11.80s (± 0.42%) 11.86s (± 0.31%) +0.05s (+ 0.46%) 11.79s 11.95s
TFS - node (v10.16.3, x64)
Memory used 306,054k (± 0.03%) 306,068k (± 0.02%) +13k (+ 0.00%) 305,936k 306,204k
Parse Time 1.30s (± 0.53%) 1.30s (± 0.67%) +0.01s (+ 0.54%) 1.28s 1.32s
Bind Time 0.67s (± 0.83%) 0.68s (± 0.85%) +0.01s (+ 1.19%) 0.67s 0.70s
Check Time 5.54s (± 0.36%) 5.53s (± 0.28%) -0.01s (- 0.14%) 5.49s 5.56s
Emit Time 3.44s (± 1.17%) 3.44s (± 1.13%) 0.00s ( 0.00%) 3.32s 3.50s
Total Time 10.94s (± 0.45%) 10.95s (± 0.47%) +0.01s (+ 0.07%) 10.83s 11.03s
material-ui - node (v10.16.3, x64)
Memory used 458,159k (± 0.01%) 458,115k (± 0.01%) -43k (- 0.01%) 458,034k 458,250k
Parse Time 1.85s (± 0.55%) 1.85s (± 0.68%) 0.00s ( 0.00%) 1.81s 1.87s
Bind Time 0.58s (± 2.36%) 0.57s (± 2.33%) -0.01s (- 1.71%) 0.56s 0.61s
Check Time 15.84s (± 0.48%) 15.77s (± 0.81%) -0.07s (- 0.47%) 15.52s 16.05s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 18.28s (± 0.43%) 18.19s (± 0.73%) -0.09s (- 0.47%) 17.93s 18.48s
xstate - node (v10.16.3, x64)
Memory used 581,076k (± 0.01%) 580,967k (± 0.01%) -109k (- 0.02%) 580,829k 581,108k
Parse Time 2.62s (± 0.46%) 2.61s (± 0.36%) -0.00s (- 0.15%) 2.59s 2.63s
Bind Time 0.91s (± 0.85%) 0.92s (± 0.54%) +0.00s (+ 0.55%) 0.90s 0.92s
Check Time 1.61s (± 0.77%) 1.62s (± 0.71%) +0.01s (+ 0.56%) 1.59s 1.64s
Emit Time 0.08s (± 4.79%) 0.07s (± 4.13%) 🟩-0.00s (- 5.26%) 0.07s 0.08s
Total Time 5.21s (± 0.38%) 5.22s (± 0.33%) +0.01s (+ 0.12%) 5.18s 5.25s
Angular - node (v12.1.0, x64)
Memory used 339,874k (± 0.10%) 339,910k (± 0.07%) +37k (+ 0.01%) 338,976k 340,180k
Parse Time 2.10s (± 1.03%) 2.12s (± 0.47%) +0.02s (+ 0.76%) 2.10s 2.14s
Bind Time 0.77s (± 0.75%) 0.78s (± 1.13%) +0.01s (+ 0.91%) 0.76s 0.80s
Check Time 5.92s (± 0.75%) 5.92s (± 0.36%) +0.01s (+ 0.10%) 5.87s 5.98s
Emit Time 6.40s (± 1.02%) 6.36s (± 0.62%) -0.04s (- 0.62%) 6.25s 6.44s
Total Time 15.20s (± 0.64%) 15.18s (± 0.29%) -0.01s (- 0.09%) 15.07s 15.26s
Compiler-Unions - node (v12.1.0, x64)
Memory used 193,121k (± 0.52%) 194,072k (± 0.08%) +952k (+ 0.49%) 193,543k 194,342k
Parse Time 0.83s (± 0.78%) 0.83s (± 0.70%) +0.00s (+ 0.12%) 0.82s 0.84s
Bind Time 0.47s (± 1.26%) 0.48s (± 1.00%) +0.00s (+ 0.42%) 0.47s 0.49s
Check Time 6.76s (± 0.61%) 6.76s (± 0.63%) -0.00s (- 0.03%) 6.65s 6.87s
Emit Time 2.48s (± 1.36%) 2.46s (± 1.14%) -0.02s (- 0.85%) 2.40s 2.52s
Total Time 10.54s (± 0.47%) 10.52s (± 0.52%) -0.02s (- 0.18%) 10.40s 10.68s
Monaco - node (v12.1.0, x64)
Memory used 327,745k (± 0.02%) 327,810k (± 0.03%) +65k (+ 0.02%) 327,593k 327,960k
Parse Time 1.60s (± 0.63%) 1.60s (± 0.44%) +0.00s (+ 0.19%) 1.59s 1.62s
Bind Time 0.70s (± 0.74%) 0.70s (± 0.00%) -0.00s (- 0.14%) 0.70s 0.70s
Check Time 5.85s (± 0.44%) 5.88s (± 0.49%) +0.02s (+ 0.41%) 5.81s 5.94s
Emit Time 3.33s (± 0.80%) 3.34s (± 0.69%) +0.01s (+ 0.27%) 3.27s 3.39s
Total Time 11.48s (± 0.43%) 11.51s (± 0.44%) +0.03s (+ 0.28%) 11.38s 11.62s
TFS - node (v12.1.0, x64)
Memory used 290,622k (± 0.06%) 290,704k (± 0.02%) +82k (+ 0.03%) 290,567k 290,821k
Parse Time 1.31s (± 0.99%) 1.30s (± 0.67%) -0.00s (- 0.31%) 1.28s 1.32s
Bind Time 0.68s (± 1.07%) 0.68s (± 0.98%) +0.01s (+ 0.74%) 0.67s 0.70s
Check Time 5.41s (± 0.51%) 5.44s (± 0.59%) +0.02s (+ 0.41%) 5.38s 5.54s
Emit Time 3.51s (± 0.79%) 3.53s (± 1.17%) +0.02s (+ 0.54%) 3.43s 3.62s
Total Time 10.91s (± 0.33%) 10.95s (± 0.66%) +0.04s (+ 0.34%) 10.81s 11.15s
material-ui - node (v12.1.0, x64)
Memory used 437,103k (± 0.09%) 437,439k (± 0.01%) +337k (+ 0.08%) 437,335k 437,540k
Parse Time 1.84s (± 0.48%) 1.83s (± 0.65%) -0.01s (- 0.76%) 1.80s 1.86s
Bind Time 0.55s (± 0.66%) 0.55s (± 0.66%) 0.00s ( 0.00%) 0.55s 0.56s
Check Time 12.95s (± 0.73%) 12.85s (± 0.62%) -0.10s (- 0.78%) 12.73s 13.03s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.34s (± 0.63%) 15.23s (± 0.55%) -0.11s (- 0.74%) 15.11s 15.43s
xstate - node (v12.1.0, x64)
Memory used 546,276k (± 0.02%) 549,560k (± 1.28%) +3,284k (+ 0.60%) 546,267k 578,070k
Parse Time 2.57s (± 0.74%) 2.57s (± 0.53%) -0.00s (- 0.16%) 2.53s 2.60s
Bind Time 0.89s (± 0.77%) 0.89s (± 0.41%) -0.00s (- 0.11%) 0.89s 0.90s
Check Time 1.50s (± 0.50%) 1.49s (± 0.55%) -0.00s (- 0.20%) 1.47s 1.51s
Emit Time 0.07s (± 3.14%) 0.07s (± 3.14%) 0.00s ( 0.00%) 0.07s 0.08s
Total Time 5.04s (± 0.40%) 5.02s (± 0.37%) -0.01s (- 0.26%) 4.97s 5.05s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v14.15.1, x64)
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
Scenarios
  • Angular - node (v14.15.1, x64)
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • xstate - node (v14.15.1, x64)
  • xstate - node (v10.16.3, x64)
  • xstate - node (v12.1.0, x64)
Benchmark Name Iterations
Current 50994 10
Baseline main 10

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top-repos suite comparing main and refs/pull/50994/merge:

Everything looks good!

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'd think this'd make us pick the default in more ambiguous contextually typed inference situations (I'm thinking something that's circularly constrained with a default could change to use the default from an any maybe), but it looks like nothing in the extended suites is obviously broken, so that's good~

@mxsdev
Copy link

mxsdev commented Sep 30, 2022

@mxsdev The simplification in this PR is simply to perform contextual type instantiation for all type parameters that declare default types, even if no inferences have been made for those type parameters.

Ah I see, much simpler/more direct indeed. I was concerned originally about doing that since I assumed like @weswigham that it might infer in too many situations.

Good stuff!

// If no inferences have been made, and none of the type parameters for which we are inferring
// specify default types, nothing is gained from instantiating as type parameters would just be
// replaced with their constraints similar to the apparent type.
if (inferenceContext && contextFlags! & ContextFlags.Signature && some(inferenceContext.inferences, hasInferenceCandidatesOrDefault)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I also have an additional fix touching this very line. Since this is being touched now, perhaps you could take a look if it looks any good? :p

https://github.com/microsoft/TypeScript/pull/48838/files#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8R27537

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'm not quite sure what that fix is trying to accomplish. Either way I don't think it is related to the issue being solved in this PR, so I think it's better to keep it separate.

@ahejlsberg
Copy link
Member Author

Tests and performance all look good. I'll merge this.

@ahejlsberg ahejlsberg merged commit 96894db into main Sep 30, 2022
@ahejlsberg ahejlsberg deleted the fix50858 branch September 30, 2022 14:02
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
6 participants