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

Consistent narrowing by discriminant #38311

Merged
merged 2 commits into from
May 6, 2020
Merged

Consistent narrowing by discriminant #38311

merged 2 commits into from
May 6, 2020

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented May 3, 2020

With this PR we fix inconsistencies in our narrowing logic for discriminated unions. Previously we required the declared type of a reference to be a union type before we'd narrow by discriminants. This meant narrowing by discriminant wouldn't work in conjunction with user defined type predicate narrowing, with the notable (and inconsistent) exception of switch statements. We now consistently allow narrowing by discriminant of a reference with a declared type that isn't a union but a control flow type that is (which, for example, might result from first narrowing an unknown to a union type using a user defined type predicate and then narrowing the union type by a discriminant).

Fixes #30557.
Fixes #36777.

@ahejlsberg
Copy link
Member Author

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 3, 2020

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 3, 2020

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 3, 2020

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 3, 2020

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

@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..38311

Metric master 38311 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 328,207k (± 0.02%) 327,547k (± 0.03%) -661k (- 0.20%) 327,354k 327,707k
Parse Time 1.65s (± 0.57%) 1.64s (± 0.62%) -0.01s (- 0.67%) 1.61s 1.66s
Bind Time 0.91s (± 0.44%) 0.90s (± 1.16%) -0.01s (- 1.43%) 0.88s 0.92s
Check Time 4.83s (± 0.44%) 4.81s (± 0.56%) -0.01s (- 0.23%) 4.75s 4.86s
Emit Time 5.44s (± 0.84%) 5.42s (± 0.53%) -0.01s (- 0.26%) 5.37s 5.52s
Total Time 12.83s (± 0.42%) 12.77s (± 0.32%) -0.05s (- 0.42%) 12.70s 12.87s
Monaco - node (v10.16.3, x64)
Memory used 327,218k (± 0.02%) 327,242k (± 0.02%) +24k (+ 0.01%) 327,120k 327,366k
Parse Time 1.28s (± 0.55%) 1.28s (± 0.71%) -0.00s (- 0.00%) 1.26s 1.30s
Bind Time 0.79s (± 0.63%) 0.79s (± 0.47%) 0.00s ( 0.00%) 0.78s 0.79s
Check Time 4.83s (± 0.51%) 4.80s (± 0.49%) -0.03s (- 0.70%) 4.75s 4.84s
Emit Time 2.95s (± 0.85%) 2.93s (± 0.79%) -0.02s (- 0.74%) 2.86s 2.97s
Total Time 9.85s (± 0.38%) 9.79s (± 0.44%) -0.06s (- 0.57%) 9.68s 9.88s
TFS - node (v10.16.3, x64)
Memory used 292,293k (± 0.02%) 292,293k (± 0.02%) +0k (+ 0.00%) 292,172k 292,454k
Parse Time 0.97s (± 0.67%) 0.97s (± 0.78%) -0.00s (- 0.21%) 0.94s 0.98s
Bind Time 0.75s (± 0.94%) 0.76s (± 0.79%) +0.01s (+ 0.93%) 0.75s 0.77s
Check Time 4.34s (± 0.61%) 4.35s (± 0.58%) +0.01s (+ 0.21%) 4.29s 4.41s
Emit Time 3.11s (± 0.61%) 3.08s (± 1.02%) -0.03s (- 0.90%) 3.00s 3.13s
Total Time 9.16s (± 0.39%) 9.15s (± 0.51%) -0.01s (- 0.10%) 9.06s 9.23s
material-ui - node (v10.16.3, x64)
Memory used 449,256k (± 0.01%) 449,062k (± 0.01%) -195k (- 0.04%) 448,923k 449,162k
Parse Time 1.79s (± 0.67%) 1.78s (± 0.60%) -0.01s (- 0.28%) 1.77s 1.82s
Bind Time 0.70s (± 0.88%) 0.70s (± 1.00%) -0.00s (- 0.00%) 0.69s 0.72s
Check Time 12.71s (± 1.05%) 12.65s (± 0.76%) -0.06s (- 0.47%) 12.45s 12.82s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.20s (± 0.94%) 15.13s (± 0.65%) -0.07s (- 0.44%) 14.93s 15.29s
Angular - node (v12.1.0, x64)
Memory used 303,676k (± 0.01%) 303,182k (± 0.02%) -494k (- 0.16%) 302,966k 303,294k
Parse Time 1.58s (± 0.60%) 1.58s (± 0.52%) 0.00s ( 0.00%) 1.57s 1.61s
Bind Time 0.89s (± 0.50%) 0.89s (± 0.86%) -0.00s (- 0.34%) 0.87s 0.90s
Check Time 4.70s (± 0.60%) 4.69s (± 0.46%) -0.01s (- 0.21%) 4.64s 4.74s
Emit Time 5.55s (± 0.68%) 5.54s (± 0.58%) -0.00s (- 0.04%) 5.49s 5.62s
Total Time 12.72s (± 0.40%) 12.71s (± 0.41%) -0.01s (- 0.11%) 12.59s 12.84s
Monaco - node (v12.1.0, x64)
Memory used 307,171k (± 0.02%) 307,179k (± 0.02%) +8k (+ 0.00%) 307,104k 307,360k
Parse Time 1.23s (± 0.83%) 1.22s (± 0.48%) -0.01s (- 0.98%) 1.20s 1.23s
Bind Time 0.76s (± 1.09%) 0.76s (± 0.99%) -0.01s (- 1.05%) 0.75s 0.78s
Check Time 4.65s (± 0.53%) 4.64s (± 0.59%) -0.00s (- 0.02%) 4.57s 4.69s
Emit Time 3.01s (± 1.49%) 3.00s (± 0.79%) -0.01s (- 0.43%) 2.95s 3.06s
Total Time 9.65s (± 0.56%) 9.62s (± 0.48%) -0.04s (- 0.37%) 9.49s 9.71s
TFS - node (v12.1.0, x64)
Memory used 274,546k (± 0.02%) 274,522k (± 0.02%) -24k (- 0.01%) 274,393k 274,634k
Parse Time 0.94s (± 1.50%) 0.93s (± 0.70%) -0.01s (- 1.59%) 0.92s 0.94s
Bind Time 0.73s (± 0.96%) 0.72s (± 0.47%) -0.01s (- 1.09%) 0.72s 0.73s
Check Time 4.27s (± 0.40%) 4.24s (± 0.51%) -0.03s (- 0.63%) 4.20s 4.28s
Emit Time 3.15s (± 0.70%) 3.11s (± 1.04%) -0.04s (- 1.27%) 3.06s 3.21s
Total Time 9.10s (± 0.31%) 9.01s (± 0.51%) -0.09s (- 0.95%) 8.91s 9.14s
material-ui - node (v12.1.0, x64)
Memory used 426,512k (± 0.07%) 426,448k (± 0.01%) -64k (- 0.02%) 426,355k 426,552k
Parse Time 1.77s (± 0.37%) 1.76s (± 0.42%) -0.01s (- 0.68%) 1.74s 1.77s
Bind Time 0.65s (± 0.92%) 0.65s (± 0.92%) -0.00s (- 0.00%) 0.63s 0.66s
Check Time 11.28s (± 1.07%) 11.20s (± 0.67%) -0.08s (- 0.69%) 11.09s 11.43s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 13.69s (± 0.88%) 13.61s (± 0.58%) -0.09s (- 0.65%) 13.50s 13.85s
Angular - node (v8.9.0, x64)
Memory used 323,133k (± 0.02%) 322,654k (± 0.02%) -478k (- 0.15%) 322,561k 322,774k
Parse Time 2.13s (± 0.41%) 2.11s (± 0.61%) -0.02s (- 0.85%) 2.08s 2.14s
Bind Time 0.94s (± 0.85%) 0.94s (± 1.14%) +0.00s (+ 0.32%) 0.92s 0.97s
Check Time 5.50s (± 0.97%) 5.44s (± 1.75%) -0.06s (- 1.13%) 5.25s 5.61s
Emit Time 6.32s (± 0.91%) 6.33s (± 1.32%) +0.01s (+ 0.19%) 6.21s 6.59s
Total Time 14.88s (± 0.44%) 14.83s (± 0.56%) -0.06s (- 0.39%) 14.66s 14.99s
Monaco - node (v8.9.0, x64)
Memory used 325,844k (± 0.02%) 325,803k (± 0.02%) -41k (- 0.01%) 325,693k 325,954k
Parse Time 1.55s (± 0.37%) 1.55s (± 0.40%) -0.00s (- 0.13%) 1.54s 1.57s
Bind Time 0.94s (± 2.14%) 0.91s (± 1.15%) -0.02s (- 2.35%) 0.89s 0.94s
Check Time 5.43s (± 0.74%) 5.42s (± 0.67%) -0.01s (- 0.15%) 5.33s 5.51s
Emit Time 3.48s (± 0.86%) 3.52s (± 0.62%) +0.03s (+ 0.98%) 3.45s 3.55s
Total Time 11.40s (± 0.48%) 11.40s (± 0.42%) +0.00s (+ 0.00%) 11.25s 11.48s
TFS - node (v8.9.0, x64)
Memory used 291,814k (± 0.02%) 291,841k (± 0.01%) +27k (+ 0.01%) 291,725k 291,961k
Parse Time 1.26s (± 0.74%) 1.26s (± 0.38%) +0.00s (+ 0.08%) 1.25s 1.27s
Bind Time 0.76s (± 0.59%) 0.76s (± 0.96%) -0.00s (- 0.00%) 0.74s 0.77s
Check Time 5.14s (± 1.38%) 4.97s (± 1.19%) 🟩-0.17s (- 3.31%) 4.88s 5.16s
Emit Time 3.20s (± 2.47%) 3.32s (± 2.37%) +0.12s (+ 3.72%) 3.11s 3.46s
Total Time 10.36s (± 0.34%) 10.31s (± 0.53%) -0.05s (- 0.48%) 10.20s 10.45s
material-ui - node (v8.9.0, x64)
Memory used 452,144k (± 0.01%) 451,882k (± 0.01%) -261k (- 0.06%) 451,803k 452,035k
Parse Time 2.13s (± 0.58%) 2.13s (± 0.54%) +0.00s (+ 0.19%) 2.10s 2.15s
Bind Time 0.83s (± 1.08%) 0.82s (± 0.70%) -0.00s (- 0.36%) 0.81s 0.84s
Check Time 16.75s (± 0.88%) 16.79s (± 1.25%) +0.04s (+ 0.23%) 16.38s 17.27s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 19.71s (± 0.71%) 19.74s (± 1.07%) +0.03s (+ 0.17%) 19.32s 20.23s
Angular - node (v8.9.0, x86)
Memory used 186,102k (± 0.03%) 185,870k (± 0.02%) -231k (- 0.12%) 185,793k 185,964k
Parse Time 2.07s (± 1.13%) 2.05s (± 0.36%) -0.02s (- 1.06%) 2.04s 2.07s
Bind Time 1.09s (± 0.51%) 1.08s (± 0.51%) -0.01s (- 0.55%) 1.07s 1.09s
Check Time 5.02s (± 0.61%) 5.04s (± 0.43%) +0.02s (+ 0.36%) 4.99s 5.08s
Emit Time 6.14s (± 1.25%) 6.09s (± 0.66%) -0.06s (- 0.93%) 5.98s 6.18s
Total Time 14.32s (± 0.66%) 14.26s (± 0.31%) -0.06s (- 0.43%) 14.17s 14.35s
Monaco - node (v8.9.0, x86)
Memory used 185,618k (± 0.02%) 185,643k (± 0.02%) +25k (+ 0.01%) 185,593k 185,721k
Parse Time 1.61s (± 0.77%) 1.60s (± 0.62%) -0.01s (- 0.62%) 1.58s 1.62s
Bind Time 0.78s (± 0.99%) 0.78s (± 0.77%) -0.01s (- 0.77%) 0.76s 0.79s
Check Time 5.46s (± 0.44%) 5.47s (± 0.76%) +0.01s (+ 0.11%) 5.40s 5.58s
Emit Time 2.89s (± 0.68%) 2.89s (± 0.97%) +0.01s (+ 0.21%) 2.85s 2.96s
Total Time 10.73s (± 0.26%) 10.73s (± 0.48%) -0.00s (- 0.03%) 10.63s 10.82s
TFS - node (v8.9.0, x86)
Memory used 167,174k (± 0.01%) 167,146k (± 0.01%) -28k (- 0.02%) 167,086k 167,207k
Parse Time 1.30s (± 0.53%) 1.29s (± 0.51%) -0.01s (- 0.39%) 1.28s 1.31s
Bind Time 0.72s (± 1.23%) 0.72s (± 1.01%) -0.01s (- 0.83%) 0.70s 0.74s
Check Time 4.71s (± 0.53%) 4.70s (± 0.72%) -0.01s (- 0.13%) 4.64s 4.79s
Emit Time 3.07s (± 3.39%) 3.03s (± 1.85%) -0.04s (- 1.21%) 2.93s 3.21s
Total Time 9.79s (± 0.99%) 9.74s (± 0.79%) -0.06s (- 0.57%) 9.64s 9.96s
material-ui - node (v8.9.0, x86)
Memory used 256,117k (± 0.02%) 255,969k (± 0.02%) -148k (- 0.06%) 255,874k 256,134k
Parse Time 2.20s (± 0.59%) 2.18s (± 0.65%) -0.01s (- 0.64%) 2.16s 2.21s
Bind Time 0.70s (± 1.57%) 0.69s (± 0.86%) -0.01s (- 1.14%) 0.68s 0.71s
Check Time 15.32s (± 1.08%) 15.26s (± 0.64%) -0.06s (- 0.38%) 15.03s 15.46s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 18.22s (± 0.96%) 18.14s (± 0.55%) -0.07s (- 0.40%) 17.91s 18.36s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-166-generic
Architecturex64
Available Memory16 GB
Available Memory1 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v8.9.0, x64)
  • node (v8.9.0, x86)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v8.9.0, x64)
  • Angular - node (v8.9.0, x86)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v8.9.0, x64)
  • Monaco - node (v8.9.0, x86)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v8.9.0, x64)
  • TFS - node (v8.9.0, x86)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v8.9.0, x64)
  • material-ui - node (v8.9.0, x86)
Benchmark Name Iterations
Current 38311 10
Baseline master 10

@ahejlsberg
Copy link
Member Author

Tests all look good (nothing but preexisting conditions).

@weswigham
Copy link
Member

weswigham commented May 4, 2020

Does this obsolete #33434 ? #30593?

@jack-williams
Copy link
Collaborator

@weswigham I think it pretty much replaces the second: this is so much simpler. I don’t believe this is quite as general as the other PR aims to be, but I went through all the duplicates on the original issue and this fixes them.

I think my only comment here is whether it’s worth filter the declared type for non-object types. I’m not sure this approach works if the declared type is something like object | undefined.

The first PR you link was some tidying I haven’t updated yet (sorry). I don’t think it’s changed by this.

@ahejlsberg ahejlsberg merged commit 2524fb1 into master May 6, 2020
@ahejlsberg ahejlsberg deleted the fix36777 branch May 6, 2020 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants