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

Improve detection of type variables in instantiated object types #53246

Merged
merged 1 commit into from
Mar 19, 2023

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Mar 14, 2023

When an object type that references type parameters is instantiated, it is often the case that the resulting instantiated type doesn't reference type parameters (because it is often the case that all type arguments are non-generic types). With this PR we improve our detection of such object types in order to short-circuit further pointless type instantiation and type inference involving those types.

The optimizations in this PR reduces the check time for the material-ui project by over 50%. (Note that the reduction isn't nearly as dramatic with the very old version of material-ui we use in our performance test suite.)

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 14, 2023
@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 Mar 14, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 14, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 14, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 14, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 14, 2023

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

Update: The results are in!

@ahejlsberg ahejlsberg added this to the TypeScript 5.1.0 milestone Mar 14, 2023
@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..53246

Metric main 53246 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 361,443k (± 0.01%) 361,468k (± 0.02%) ~ 361,406k 361,538k p=0.575 n=6
Parse Time 3.51s (± 0.42%) 3.52s (± 0.33%) ~ 3.50s 3.53s p=0.309 n=6
Bind Time 1.18s (± 0.35%) 1.18s (± 0.35%) ~ 1.18s 1.19s p=1.000 n=6
Check Time 9.48s (± 0.56%) 9.51s (± 0.38%) ~ 9.45s 9.54s p=0.255 n=6
Emit Time 7.91s (± 0.41%) 7.94s (± 0.63%) ~ 7.86s 8.01s p=0.258 n=6
Total Time 22.08s (± 0.28%) 22.16s (± 0.37%) ~ 22.01s 22.25s p=0.076 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 192,743k (± 0.03%) 192,729k (± 0.03%) ~ 192,671k 192,820k p=0.689 n=6
Parse Time 1.59s (± 0.86%) 1.58s (± 0.96%) ~ 1.56s 1.60s p=0.934 n=6
Bind Time 0.82s (± 0.50%) 0.82s (± 0.77%) ~ 0.81s 0.83s p=0.673 n=6
Check Time 10.11s (± 0.63%) 10.07s (± 0.41%) ~ 10.01s 10.13s p=0.295 n=6
Emit Time 3.01s (± 0.66%) 3.00s (± 0.47%) ~ 2.98s 3.02s p=0.507 n=6
Total Time 15.52s (± 0.36%) 15.47s (± 0.22%) ~ 15.43s 15.52s p=0.228 n=6
Monaco - node (v16.17.1, x64)
Memory used 346,715k (± 0.01%) 346,751k (± 0.00%) +36k (+ 0.01%) 346,737k 346,776k p=0.045 n=6
Parse Time 2.73s (± 0.28%) 2.72s (± 0.38%) -0.02s (- 0.55%) 2.70s 2.73s p=0.025 n=6
Bind Time 1.09s (± 0.58%) 1.09s (± 0.37%) ~ 1.09s 1.10s p=0.673 n=6
Check Time 7.77s (± 0.27%) 7.76s (± 0.36%) ~ 7.73s 7.79s p=1.000 n=6
Emit Time 4.49s (± 0.96%) 4.46s (± 0.68%) ~ 4.41s 4.49s p=0.318 n=6
Total Time 16.07s (± 0.34%) 16.03s (± 0.21%) ~ 15.99s 16.09s p=0.121 n=6
TFS - node (v16.17.1, x64)
Memory used 300,111k (± 0.01%) 300,122k (± 0.01%) ~ 300,090k 300,155k p=0.423 n=6
Parse Time 2.17s (± 0.74%) 2.18s (± 0.68%) ~ 2.16s 2.20s p=0.511 n=6
Bind Time 1.23s (± 0.95%) 1.24s (± 0.94%) ~ 1.23s 1.26s p=0.187 n=6
Check Time 7.18s (± 0.24%) 7.20s (± 0.39%) ~ 7.16s 7.23s p=0.292 n=6
Emit Time 4.35s (± 0.91%) 4.35s (± 0.49%) ~ 4.33s 4.38s p=0.870 n=6
Total Time 14.93s (± 0.24%) 14.97s (± 0.21%) ~ 14.92s 15.00s p=0.106 n=6
material-ui - node (v16.17.1, x64)
Memory used 477,725k (± 0.01%) 476,649k (± 0.01%) -1,076k (- 0.23%) 476,592k 476,752k p=0.005 n=6
Parse Time 3.22s (± 0.23%) 3.21s (± 0.75%) ~ 3.18s 3.24s p=1.000 n=6
Bind Time 0.95s (± 0.54%) 0.95s (± 0.94%) ~ 0.94s 0.96s p=0.541 n=6
Check Time 18.14s (± 0.82%) 17.95s (± 0.19%) -0.19s (- 1.05%) 17.91s 18.00s p=0.005 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.31s (± 0.67%) 22.11s (± 0.18%) -0.20s (- 0.88%) 22.05s 22.17s p=0.005 n=6
xstate - node (v16.17.1, x64)
Memory used 550,669k (± 0.01%) 550,576k (± 0.01%) -93k (- 0.02%) 550,502k 550,652k p=0.045 n=6
Parse Time 3.94s (± 0.21%) 3.95s (± 0.42%) ~ 3.93s 3.97s p=0.226 n=6
Bind Time 1.79s (± 0.55%) 1.80s (± 0.84%) ~ 1.78s 1.82s p=0.498 n=6
Check Time 3.00s (± 0.57%) 3.01s (± 0.64%) ~ 2.98s 3.03s p=0.747 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.82s (± 0.18%) 8.85s (± 0.41%) ~ 8.81s 8.89s p=0.198 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-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 53246 6
Baseline main 6

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/53246/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

@oliviertassinari
Copy link

Oh wow, @mnajdova you might want to run your TypeScript benchmark against this change, see if we can measure the impact.

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.

We can tighten these checks even further, when we want to, now that we're willing to rely on there not being any sneaky type variables outside the object's mapper (which, iirc, is what we've worried about when I've mentioned doing this in the past). Since mappers have inspectable sources and targets (where possible) nowadays, we can rig them to actually keep a set of which type parameters remain unmapped for a given stack of mappers after instantiation when we compose them, and then use that to check if any of the inference targets are actually still present in the target type (thus, allowing us to skip inference on arguments or parts of types which only contain type parameters for outer calls).

@jakebailey
Copy link
Member

jakebailey commented Mar 15, 2023

On material-ui's docs project:

Benchmark 1: 4.9
  Time (mean ± σ):     85.634 s ±  0.332 s    [User: 82.993 s, System: 2.638 s]
  Range (min … max):   85.384 s … 86.006 s    5 runs
 
Benchmark 2: 5.0
  Time (mean ± σ):     77.467 s ±  1.111 s    [User: 74.444 s, System: 3.016 s]
  Range (min … max):   76.656 s … 79.313 s    5 runs
 
Benchmark 3: PR 53246
  Time (mean ± σ):     49.761 s ±  0.355 s    [User: 47.202 s, System: 2.552 s]
  Range (min … max):   49.439 s … 50.297 s    5 runs
 
Summary
  'PR 53246' ran
    1.56 ± 0.02 times faster than '5.0'
    1.72 ± 0.01 times faster than '4.9'

☺️

@jakebailey
Copy link
Member

Just in case anyone was confused as to why I'm not getting quite the great number, the above is the total time end-to-end measured via hyperfine + tsc, so includes parse/bind/emit as well as check, whereas the PR description mentions specifically check time.

@ahejlsberg ahejlsberg merged commit b70784e into main Mar 19, 2023
@ahejlsberg ahejlsberg deleted the improveTypeVariableDetection branch March 19, 2023 15:13
@tomaszs
Copy link

tomaszs commented Apr 22, 2023

Hello, I was browsing that MR and the source code but have trouble understanding what is it about. It would be great to read a description for mortal human being because it's interesting. Great work BTW. Congrats on the results!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants