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

Smarter subtype reduction in union types #42353

Merged
merged 6 commits into from
Feb 4, 2021
Merged

Smarter subtype reduction in union types #42353

merged 6 commits into from
Feb 4, 2021

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Jan 15, 2021

With this PR we improve our subtype reduction algorithm for union types to more efficiently handle primitive type constituents. Specifically, prior to performing subtype reduction we first perform the (much cheaper) literal type reduction pass. This allows us to almost always exclude primitive types from the subtype reduction pass and include them only when an empty object type is present.

With the PR the literal type reduction pass reduces away undefined when the type set includes void. Previously we would only perform this reduction as part of subtype reduction. This minor change accounts for all but one of the baseline changes.

Fixes #41615.

@ahejlsberg
Copy link
Member Author

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at 439c9ad. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

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

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - master..42353

Metric master 42353 Delta Best Worst
Angular - node (v14.15.1, x64)
Memory used 322,695k (± 0.01%) 322,782k (± 0.01%) +87k (+ 0.03%) 322,701k 322,832k
Parse Time 2.00s (± 0.30%) 2.01s (± 1.31%) +0.01s (+ 0.60%) 1.98s 2.11s
Bind Time 0.85s (± 0.56%) 0.85s (± 0.40%) 0.00s ( 0.00%) 0.85s 0.86s
Check Time 4.89s (± 0.48%) 4.85s (± 0.53%) -0.04s (- 0.76%) 4.77s 4.89s
Emit Time 5.54s (± 0.82%) 5.53s (± 0.44%) -0.01s (- 0.13%) 5.46s 5.58s
Total Time 13.28s (± 0.43%) 13.24s (± 0.38%) -0.03s (- 0.25%) 13.14s 13.41s
Compiler-Unions - node (v14.15.1, x64)
Memory used 200,973k (± 0.59%) 200,439k (± 0.48%) -534k (- 0.27%) 199,448k 203,282k
Parse Time 0.81s (± 0.42%) 0.82s (± 0.61%) +0.00s (+ 0.25%) 0.81s 0.83s
Bind Time 0.52s (± 0.69%) 0.53s (± 0.56%) +0.00s (+ 0.76%) 0.52s 0.53s
Check Time 9.76s (± 0.48%) 9.73s (± 0.46%) -0.03s (- 0.29%) 9.64s 9.83s
Emit Time 2.35s (± 1.36%) 2.35s (± 1.45%) 0.00s ( 0.00%) 2.30s 2.44s
Total Time 13.45s (± 0.38%) 13.43s (± 0.47%) -0.02s (- 0.17%) 13.31s 13.61s
Monaco - node (v14.15.1, x64)
Memory used 336,841k (± 0.01%) 336,883k (± 0.01%) +41k (+ 0.01%) 336,832k 336,951k
Parse Time 1.64s (± 0.47%) 1.65s (± 1.64%) +0.01s (+ 0.55%) 1.62s 1.75s
Bind Time 0.73s (± 0.47%) 0.73s (± 0.61%) +0.01s (+ 0.69%) 0.73s 0.75s
Check Time 4.85s (± 0.39%) 4.85s (± 0.45%) -0.00s (- 0.04%) 4.80s 4.90s
Emit Time 2.92s (± 0.57%) 2.94s (± 0.49%) +0.02s (+ 0.55%) 2.91s 2.98s
Total Time 10.14s (± 0.33%) 10.17s (± 0.34%) +0.02s (+ 0.25%) 10.08s 10.25s
TFS - node (v14.15.1, x64)
Memory used 291,604k (± 0.01%) 291,578k (± 0.01%) -26k (- 0.01%) 291,535k 291,669k
Parse Time 1.29s (± 0.67%) 1.29s (± 0.94%) +0.00s (+ 0.23%) 1.28s 1.33s
Bind Time 0.69s (± 0.72%) 0.69s (± 0.90%) +0.00s (+ 0.44%) 0.68s 0.71s
Check Time 4.52s (± 0.55%) 4.50s (± 0.52%) -0.02s (- 0.44%) 4.45s 4.56s
Emit Time 3.05s (± 0.89%) 3.06s (± 0.54%) +0.01s (+ 0.33%) 3.01s 3.09s
Total Time 9.54s (± 0.57%) 9.54s (± 0.33%) +0.00s (+ 0.01%) 9.46s 9.63s
material-ui - node (v14.15.1, x64)
Memory used 472,350k (± 0.05%) 472,244k (± 0.05%) -106k (- 0.02%) 471,907k 472,593k
Parse Time 2.12s (± 0.50%) 2.16s (± 1.92%) +0.04s (+ 1.74%) 2.10s 2.32s
Bind Time 0.69s (± 1.31%) 0.69s (± 1.06%) -0.00s (- 0.43%) 0.68s 0.71s
Check Time 12.62s (± 0.91%) 12.56s (± 0.65%) -0.06s (- 0.44%) 12.40s 12.76s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.43s (± 0.79%) 15.41s (± 0.57%) -0.02s (- 0.15%) 15.24s 15.61s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-198-generic
Architecturex64
Available Memory16 GB
Available Memory11 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 42353 10
Baseline master 10

@ahejlsberg
Copy link
Member Author

@typescript-bot perf test this faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - master..42353

Metric master 42353 Delta Best Worst
Angular - node (v14.15.1, x64)
Memory used 322,695k (± 0.01%) 322,755k (± 0.01%) +60k (+ 0.02%) 322,693k 322,816k
Parse Time 2.00s (± 0.30%) 2.00s (± 0.47%) +0.01s (+ 0.40%) 1.99s 2.03s
Bind Time 0.85s (± 0.56%) 0.85s (± 1.18%) +0.00s (+ 0.12%) 0.84s 0.89s
Check Time 4.89s (± 0.48%) 4.88s (± 0.28%) -0.01s (- 0.29%) 4.84s 4.91s
Emit Time 5.54s (± 0.82%) 5.53s (± 0.49%) -0.00s (- 0.05%) 5.49s 5.61s
Total Time 13.28s (± 0.43%) 13.27s (± 0.31%) -0.01s (- 0.06%) 13.16s 13.37s
Compiler-Unions - node (v14.15.1, x64)
Memory used 200,973k (± 0.59%) 200,452k (± 0.55%) -521k (- 0.26%) 198,843k 203,353k
Parse Time 0.81s (± 0.42%) 0.81s (± 0.55%) -0.00s (- 0.12%) 0.80s 0.82s
Bind Time 0.52s (± 0.69%) 0.53s (± 0.69%) +0.00s (+ 0.38%) 0.52s 0.53s
Check Time 9.76s (± 0.48%) 9.78s (± 0.63%) +0.02s (+ 0.19%) 9.65s 9.90s
Emit Time 2.35s (± 1.36%) 2.38s (± 2.21%) +0.03s (+ 1.32%) 2.31s 2.54s
Total Time 13.45s (± 0.38%) 13.50s (± 0.80%) +0.05s (+ 0.37%) 13.32s 13.79s
Monaco - node (v14.15.1, x64)
Memory used 336,841k (± 0.01%) 336,905k (± 0.01%) +63k (+ 0.02%) 336,836k 336,972k
Parse Time 1.64s (± 0.47%) 1.64s (± 0.54%) -0.00s (- 0.24%) 1.61s 1.65s
Bind Time 0.73s (± 0.47%) 0.73s (± 0.55%) +0.00s (+ 0.28%) 0.72s 0.74s
Check Time 4.85s (± 0.39%) 4.85s (± 0.25%) -0.00s (- 0.08%) 4.83s 4.87s
Emit Time 2.92s (± 0.57%) 2.94s (± 0.48%) +0.02s (+ 0.75%) 2.91s 2.97s
Total Time 10.14s (± 0.33%) 10.16s (± 0.18%) +0.02s (+ 0.18%) 10.12s 10.20s
TFS - node (v14.15.1, x64)
Memory used 291,604k (± 0.01%) 291,576k (± 0.01%) -29k (- 0.01%) 291,526k 291,609k
Parse Time 1.29s (± 0.67%) 1.29s (± 0.38%) -0.00s (- 0.16%) 1.28s 1.30s
Bind Time 0.69s (± 0.72%) 0.69s (± 0.90%) +0.00s (+ 0.44%) 0.68s 0.70s
Check Time 4.52s (± 0.55%) 4.49s (± 0.52%) -0.03s (- 0.64%) 4.45s 4.53s
Emit Time 3.05s (± 0.89%) 3.05s (± 0.95%) +0.00s (+ 0.00%) 2.98s 3.11s
Total Time 9.54s (± 0.57%) 9.51s (± 0.47%) -0.02s (- 0.24%) 9.41s 9.62s
material-ui - node (v14.15.1, x64)
Memory used 472,350k (± 0.05%) 472,180k (± 0.05%) -171k (- 0.04%) 471,890k 472,591k
Parse Time 2.12s (± 0.50%) 2.13s (± 0.61%) +0.01s (+ 0.33%) 2.10s 2.16s
Bind Time 0.69s (± 1.31%) 0.69s (± 1.12%) +0.00s (+ 0.00%) 0.67s 0.70s
Check Time 12.62s (± 0.91%) 12.58s (± 0.62%) -0.03s (- 0.27%) 12.41s 12.76s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.43s (± 0.79%) 15.40s (± 0.54%) -0.03s (- 0.18%) 15.21s 15.57s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-198-generic
Architecturex64
Available Memory16 GB
Available Memory11 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v14.15.1, x64)
Benchmark Name Iterations
Current 42353 10
Baseline master 10

@ahejlsberg
Copy link
Member Author

Tests all look good, performance is unaffected. I think this one is good to go.

@DanielRosenwasser
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 15, 2021

Hey @DanielRosenwasser, 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/93487/artifacts?artifactName=tgz&fileId=AEDC91E971639137796E784965E57B73A7D27658E1521910BE6AFD92733B44DE02&fileName=/typescript-4.2.0-insiders.20210115.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@4.2.0-pr-42353-13".;

@DanielRosenwasser
Copy link
Member

Tagging @brieb in case Airbnb wants to try this out.

@DanielRosenwasser DanielRosenwasser added the Breaking Change Would introduce errors in existing code label Jan 20, 2021
@@ -28873,7 +28859,7 @@ namespace ts {
if (returnType.flags & TypeFlags.ESSymbolLike && isSymbolOrSymbolForCall(node)) {
return getESSymbolLikeTypeForNode(walkUpParenthesizedExpressions(node.parent));
}
if (node.kind === SyntaxKind.CallExpression && node.parent.kind === SyntaxKind.ExpressionStatement &&
if (node.kind === SyntaxKind.CallExpression && !node.questionDotToken && node.parent.kind === SyntaxKind.ExpressionStatement &&
Copy link
Member

Choose a reason for hiding this comment

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

Was this supposed to be part of this change set? Or part of another PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an intended change. We have some code in checkCallExpression that attempts to issue errors when calls to assertion functions aren't actually processed as assertions (because, for example, they don't have targets that were declared with explicit type annotations). This code kicks in only with a function's return type is exactly void. It would not kick in in cases where the return type was void | undefined, but now that we reduce that to void, it does kick in.

The core issue really is that we don't want the check when the call expression is a question-dot call because the assertion isn't know to have actually happened. So that's what this fixes.

@ahejlsberg
Copy link
Member Author

@RyanCavanaugh I'd like to get this one merged, want to take a look?

# Conflicts:
#	src/compiler/checker.ts
@ahejlsberg ahejlsberg merged commit ab2729a into master Feb 4, 2021
@ahejlsberg ahejlsberg deleted the fix41615 branch February 4, 2021 21:18
@brieb
Copy link

brieb commented Feb 5, 2021

Wow, thank you so much for the fix!!

Can you pack another version? Or I can wait for the nightly and report back. Happy to try it out on the original code that motivated the issue report.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 5, 2021

I can't pack from here, but the next nightly will have it. If you'd be able to pick that up, we'd appreciate it a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Breaking Change Would introduce errors in existing code For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
5 participants