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

Bail early in getNarrowedTypeWorker if type is candidate #55926

Merged
merged 2 commits into from
Nov 6, 2023

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Sep 30, 2023

This technically handles #52345 (comment) and maybe #55948, bringing it back to <=4.7 levels, but I think it's a little silly to narrow something to itself. But, probably cheap to check anyhow.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 30, 2023
@jakebailey
Copy link
Member Author

@typescript-bot perf test this
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 30, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 30, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 30, 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/158040/artifacts?artifactName=tgz&fileId=5B641F9BC561DDCDFE93B4E7D8D9581096D3B6BE20D3E0ED9F4417909E1BA7EC02&fileName=/typescript-5.3.0-insiders.20230930.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.3.0-pr-55926-3".;

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,051k (± 0.02%) 295,098k (± 0.01%) +48k (+ 0.02%) 295,050k 295,144k p=0.045 n=6
Parse Time 2.63s (± 0.72%) 2.64s (± 0.52%) ~ 2.62s 2.66s p=0.510 n=6
Bind Time 0.84s (± 0.97%) 0.84s (± 1.30%) ~ 0.83s 0.85s p=0.662 n=6
Check Time 8.07s (± 0.13%) 8.06s (± 0.20%) ~ 8.03s 8.08s p=0.249 n=6
Emit Time 7.03s (± 0.48%) 7.04s (± 0.30%) ~ 7.02s 7.07s p=0.418 n=6
Total Time 18.57s (± 0.13%) 18.57s (± 0.19%) ~ 18.53s 18.62s p=0.872 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 191,640k (± 1.23%) 192,490k (± 1.20%) ~ 190,658k 196,458k p=0.936 n=6
Parse Time 1.34s (± 0.78%) 1.34s (± 0.99%) ~ 1.32s 1.35s p=0.451 n=6
Bind Time 0.73s (± 0.00%) 0.73s (± 0.00%) ~ 0.73s 0.73s p=1.000 n=6
Check Time 9.21s (± 0.88%) 9.27s (± 1.21%) ~ 9.13s 9.38s p=0.378 n=6
Emit Time 2.63s (± 0.47%) 2.63s (± 0.39%) ~ 2.62s 2.65s p=0.101 n=6
Total Time 13.91s (± 0.56%) 13.97s (± 0.87%) ~ 13.81s 14.11s p=0.378 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,296k (± 0.01%) 347,319k (± 0.01%) ~ 347,295k 347,351k p=0.128 n=6
Parse Time 2.46s (± 0.33%) 2.45s (± 0.49%) ~ 2.43s 2.46s p=0.115 n=6
Bind Time 0.94s (± 0.00%) 0.94s (± 0.00%) ~ 0.94s 0.94s p=1.000 n=6
Check Time 6.90s (± 0.28%) 6.91s (± 0.57%) ~ 6.84s 6.95s p=0.418 n=6
Emit Time 4.02s (± 0.26%) 4.01s (± 0.55%) ~ 3.99s 4.05s p=0.100 n=6
Total Time 14.33s (± 0.19%) 14.31s (± 0.25%) ~ 14.28s 14.37s p=0.418 n=6
TFS - node (v18.15.0, x64)
Memory used 302,549k (± 0.00%) 302,538k (± 0.01%) ~ 302,506k 302,566k p=0.378 n=6
Parse Time 2.00s (± 0.81%) 2.02s (± 0.58%) ~ 2.01s 2.04s p=0.085 n=6
Bind Time 1.01s (± 1.04%) 1.00s (± 0.75%) ~ 0.99s 1.01s p=0.611 n=6
Check Time 6.26s (± 0.47%) 6.27s (± 0.50%) ~ 6.23s 6.31s p=0.418 n=6
Emit Time 3.56s (± 0.70%) 3.56s (± 0.14%) ~ 3.56s 3.57s p=0.360 n=6
Total Time 12.83s (± 0.11%) 12.86s (± 0.23%) ~ 12.81s 12.88s p=0.145 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,488k (± 0.00%) 470,502k (± 0.00%) ~ 470,480k 470,518k p=0.230 n=6
Parse Time 2.57s (± 0.38%) 2.59s (± 0.80%) ~ 2.57s 2.62s p=0.181 n=6
Bind Time 1.00s (± 0.52%) 1.00s (± 1.37%) ~ 0.98s 1.02s p=0.794 n=6
Check Time 16.60s (± 0.37%) 16.58s (± 0.32%) ~ 16.53s 16.66s p=0.572 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.17s (± 0.35%) 20.16s (± 0.20%) ~ 20.11s 20.21s p=0.687 n=6
xstate - node (v18.15.0, x64)
Memory used 512,600k (± 0.02%) 512,582k (± 0.01%) ~ 512,498k 512,618k p=0.689 n=6
Parse Time 3.27s (± 0.36%) 3.28s (± 0.32%) ~ 3.26s 3.29s p=0.406 n=6
Bind Time 1.55s (± 0.26%) 1.55s (± 0.26%) ~ 1.54s 1.55s p=1.000 n=6
Check Time 2.87s (± 0.56%) 2.89s (± 1.07%) ~ 2.85s 2.93s p=0.225 n=6
Emit Time 0.08s (± 4.99%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=1.000 n=6
Total Time 7.76s (± 0.23%) 7.78s (± 0.38%) ~ 7.73s 7.82s p=0.145 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

tsserver

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-UnionsTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,346ms (± 0.83%) 2,343ms (± 1.10%) ~ 2,313ms 2,387ms p=0.873 n=6
Req 2 - geterr 5,366ms (± 1.40%) 5,385ms (± 1.52%) ~ 5,272ms 5,455ms p=0.873 n=6
Req 3 - references 332ms (± 1.19%) 327ms (± 0.16%) -5ms (- 1.61%) 326ms 327ms p=0.039 n=6
Req 4 - navto 275ms (± 0.82%) 276ms (± 0.94%) ~ 273ms 280ms p=0.935 n=6
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) ~ 1,356 1,356 p=1.000 n=6
Req 5 - completionInfo 82ms (±10.20%) 84ms (± 8.27%) ~ 75ms 90ms p=0.802 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,465ms (± 1.21%) 2,482ms (± 0.76%) ~ 2,456ms 2,504ms p=0.423 n=6
Req 2 - geterr 4,155ms (± 1.59%) 4,073ms (± 1.44%) -82ms (- 1.98%) 4,028ms 4,190ms p=0.045 n=6
Req 3 - references 335ms (± 1.03%) 338ms (± 1.16%) ~ 334ms 343ms p=0.089 n=6
Req 4 - navto 283ms (± 0.27%) 283ms (± 0.48%) ~ 282ms 285ms p=1.000 n=6
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) ~ 1,518 1,518 p=1.000 n=6
Req 5 - completionInfo 78ms (± 5.82%) 78ms (± 9.38%) ~ 71ms 87ms p=0.801 n=6
xstateTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,595ms (± 0.38%) 2,597ms (± 0.53%) ~ 2,577ms 2,618ms p=1.000 n=6
Req 2 - geterr 1,704ms (± 2.81%) 1,676ms (± 1.97%) ~ 1,631ms 1,731ms p=0.378 n=6
Req 3 - references 109ms (± 4.57%) 115ms (± 9.24%) ~ 105ms 126ms p=0.468 n=6
Req 4 - navto 360ms (± 0.41%) 359ms (± 0.14%) -2ms (- 0.42%) 358ms 359ms p=0.039 n=6
Req 5 - completionInfo count 2,071 (± 0.00%) 2,071 (± 0.00%) ~ 2,071 2,071 p=1.000 n=6
Req 5 - completionInfo 308ms (± 1.47%) 307ms (± 1.61%) ~ 303ms 316ms p=0.572 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • CompilerTSServer - node (v18.15.0, x64)
  • Compiler-UnionsTSServer - node (v18.15.0, x64)
  • xstateTSServer - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Startup

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
tsc-startup - node (v18.15.0, x64)
Execution time 152.43ms (± 0.17%) 152.45ms (± 0.19%) ~ 151.33ms 156.11ms p=1.000 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time 227.01ms (± 0.15%) 226.99ms (± 0.17%) ~ 225.65ms 232.33ms p=0.294 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time 228.69ms (± 0.16%) 228.75ms (± 0.17%) ~ 227.22ms 235.65ms p=0.191 n=600
typescript-startup - node (v18.15.0, x64)
Execution time 228.51ms (± 0.16%) 228.54ms (± 0.13%) ~ 227.16ms 231.65ms p=0.124 n=600
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • tsc-startup - node (v18.15.0, x64)
  • tsserver-startup - node (v18.15.0, x64)
  • tsserverlibrary-startup - node (v18.15.0, x64)
  • typescript-startup - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@jakebailey
Copy link
Member Author

@typescript-bot perf test this faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 2, 2023

Heya @jakebailey, I've started to run the tsc-only perf test suite on this PR at 1c14f5c. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,067k (± 0.01%) 295,065k (± 0.01%) ~ 295,049k 295,089k p=0.936 n=6
Parse Time 2.63s (± 0.52%) 2.63s (± 0.44%) ~ 2.61s 2.64s p=0.865 n=6
Bind Time 0.83s (± 0.62%) 0.84s (± 0.97%) +0.01s (+ 1.20%) 0.83s 0.85s p=0.050 n=6
Check Time 8.07s (± 0.38%) 8.07s (± 0.39%) ~ 8.03s 8.12s p=0.627 n=6
Emit Time 7.04s (± 0.33%) 7.06s (± 0.12%) +0.02s (+ 0.31%) 7.05s 7.07s p=0.026 n=6
Total Time 18.58s (± 0.10%) 18.60s (± 0.18%) ~ 18.56s 18.66s p=0.413 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 193,048k (± 1.48%) 191,115k (± 0.54%) ~ 190,674k 193,234k p=0.810 n=6
Parse Time 1.35s (± 1.24%) 1.35s (± 0.30%) ~ 1.34s 1.35s p=0.446 n=6
Bind Time 0.73s (± 0.00%) 0.73s (± 0.00%) ~ 0.73s 0.73s p=1.000 n=6
Check Time 9.19s (± 0.70%) 9.19s (± 0.54%) ~ 9.14s 9.26s p=1.000 n=6
Emit Time 2.64s (± 0.65%) 2.63s (± 0.34%) ~ 2.62s 2.64s p=0.458 n=6
Total Time 13.90s (± 0.54%) 13.90s (± 0.38%) ~ 13.84s 13.97s p=1.000 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,300k (± 0.00%) 347,296k (± 0.00%) ~ 347,284k 347,309k p=0.575 n=6
Parse Time 2.46s (± 0.40%) 2.46s (± 0.21%) ~ 2.45s 2.46s p=0.931 n=6
Bind Time 0.94s (± 0.43%) 0.94s (± 0.43%) ~ 0.94s 0.95s p=0.218 n=6
Check Time 6.91s (± 0.53%) 6.91s (± 0.35%) ~ 6.88s 6.95s p=0.629 n=6
Emit Time 4.02s (± 0.38%) 4.01s (± 0.44%) ~ 3.98s 4.03s p=0.103 n=6
Total Time 14.33s (± 0.33%) 14.31s (± 0.25%) ~ 14.26s 14.37s p=0.419 n=6
TFS - node (v18.15.0, x64)
Memory used 302,560k (± 0.01%) 302,546k (± 0.00%) ~ 302,527k 302,566k p=0.748 n=6
Parse Time 1.99s (± 0.61%) 2.00s (± 0.88%) ~ 1.98s 2.03s p=0.277 n=6
Bind Time 1.01s (± 1.02%) 1.00s (± 1.22%) ~ 0.99s 1.02s p=0.784 n=6
Check Time 6.27s (± 0.17%) 6.27s (± 0.16%) ~ 6.26s 6.28s p=0.931 n=6
Emit Time 3.57s (± 0.52%) 3.55s (± 0.34%) ~ 3.54s 3.57s p=0.061 n=6
Total Time 12.84s (± 0.18%) 12.83s (± 0.15%) ~ 12.80s 12.86s p=0.285 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,512k (± 0.01%) 470,506k (± 0.00%) ~ 470,486k 470,524k p=0.936 n=6
Parse Time 2.58s (± 0.32%) 2.58s (± 0.53%) ~ 2.56s 2.60s p=0.605 n=6
Bind Time 0.99s (± 1.18%) 0.99s (± 0.76%) ~ 0.98s 1.00s p=0.796 n=6
Check Time 16.63s (± 0.26%) 16.62s (± 0.40%) ~ 16.54s 16.69s p=0.688 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.20s (± 0.17%) 20.19s (± 0.28%) ~ 20.13s 20.25s p=0.630 n=6
xstate - node (v18.15.0, x64)
Memory used 512,583k (± 0.00%) 512,609k (± 0.02%) ~ 512,506k 512,745k p=0.378 n=6
Parse Time 3.27s (± 0.41%) 3.27s (± 0.37%) ~ 3.24s 3.27s p=0.675 n=6
Bind Time 1.55s (± 0.33%) 1.55s (± 0.33%) ~ 1.55s 1.56s p=1.000 n=6
Check Time 2.88s (± 0.64%) 2.88s (± 0.99%) ~ 2.84s 2.92s p=0.871 n=6
Emit Time 0.08s (± 0.00%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=0.405 n=6
Total Time 7.78s (± 0.20%) 7.77s (± 0.43%) ~ 7.74s 7.82s p=0.293 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@jakebailey jakebailey marked this pull request as ready for review October 12, 2023 21:54
@jakebailey
Copy link
Member Author

I still feel like this might be slightly silly, but it seems really cheap and quick to just check for cases where we're trying to narrow something to itself and bail early without doing all of the other work.

@gabritto
Copy link
Member

The change makes sense, but where in type checking the motivating case https://github.com/epmatsw/tsdemo/blob/main/slow.ts do we call getNarrowedType with type and candidate being the same?

@jakebailey
Copy link
Member Author

I think the example has been updated; it used to be narrowing something to itself.

@gabritto
Copy link
Member

I think the example has been updated; it used to be narrowing something to itself.

Do you have an example then?

@jakebailey
Copy link
Member Author

The repro repo has it if you go back a few commits: https://github.com/epmatsw/tsdemo/tree/b2a2af937e492819045ea4e880a56236494c2147

Something like:

function test(base: SpecificString) {
  if (isSpecificString(base)) {
    return base;
  }
}

Where SpecificString is a large union of literals.

@gabritto
Copy link
Member

Ah, so it is an explicit narrowing to itself. I don't know how common it is, it doesn't seem like something you want to be doing, but then again you might be programming defensively and you want to validate at run time that something has a type, so I can see a case for writing this kind of code.
I think I agree that, as long as it's cheap to check and doesn't make anything slower, there's not a lot of harm in doing that.

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.

Don't know if you're still waiting on my review for this, but this can be a reminder to merge this at some point~

@jakebailey
Copy link
Member Author

I was, kinda; mainly I was trying to decide if this was even worthwhile if it's so rare and silly.

@jakebailey jakebailey merged commit 4b29ab5 into microsoft:main Nov 6, 2023
19 checks passed
@jakebailey jakebailey deleted the fix-52345-other branch November 6, 2023 23:50
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
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants