Skip to content

Bail on 0- and 1-length lists in removeSubtypes to avoid spurious circularity problems #46981

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

Merged
merged 4 commits into from
Mar 8, 2022

Conversation

RyanCavanaugh
Copy link
Member

@RyanCavanaugh RyanCavanaugh commented Dec 1, 2021

Creates a reasonable workaround for #46939

Another example for illustration:

type Box = {
  content?: Foo
};
declare const b: Box;
class Foo {
  get foo() {
    return {
      content: this as Foo,
      ...b
    };
  }
}

When resolving the type of Foo#foo, we need to compute the type of the object literal in the return position. This has two parts: the content from the content: property (type Foo), and the content property from type Box (which is Foo | missing).

We want the type of content here to be subtype-reduced when creating the union of the two possible constituents, Foo and Foo. That list gets deduped in getUnionType to just [Foo], then passed to removeSubTypes, which computes this value ahead of the while loop below it which in reality runs zero iterations:

            const hasEmptyObject = hasObjectTypes && some(types, t => !!(t.flags & TypeFlags.Object) && !isGenericMappedType(t) && isEmptyResolvedType(resolveStructuredTypeMembers(t as ObjectType)));

That call to resolveStructuredTypeMembers attempts to resolve the type of Foo#foo, triggering a circularity even though none actually exists.

Instead we can just bail out earlier in the function, saving time (I hope?) and avoiding this computation until later when doing so won't introduce a circularity.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 1, 2021
@RyanCavanaugh RyanCavanaugh changed the title Bail on 1-length lists in removeSubtypes to avoid spurious circularity problems Bail on 0- and 1-length lists in removeSubtypes to avoid spurious circularity problems Dec 1, 2021
RyanCavanaugh added a commit to RyanCavanaugh/lens that referenced this pull request Dec 1, 2021
This code has a type circularity that previously went undetected. Adding an `as Wizard` assertion here removes the circularity.

See microsoft/TypeScript#46939 / microsoft/TypeScript#46981
@RyanCavanaugh
Copy link
Member Author

@typescript-bot perf test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 1, 2021

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..46981

Metric main 46981 Delta Best Worst
Angular - node (v10.16.3, x64)
Memory used 354,508k (± 0.02%) 354,636k (± 0.03%) +128k (+ 0.04%) 354,369k 354,800k
Parse Time 1.95s (± 0.32%) 1.95s (± 0.49%) -0.00s (- 0.21%) 1.92s 1.96s
Bind Time 0.84s (± 0.89%) 0.85s (± 0.58%) +0.00s (+ 0.36%) 0.84s 0.86s
Check Time 5.47s (± 0.45%) 5.50s (± 0.55%) +0.03s (+ 0.51%) 5.43s 5.56s
Emit Time 5.86s (± 0.33%) 5.90s (± 0.70%) +0.04s (+ 0.67%) 5.80s 6.00s
Total Time 14.12s (± 0.26%) 14.19s (± 0.41%) +0.07s (+ 0.47%) 14.07s 14.35s
Compiler-Unions - node (v10.16.3, x64)
Memory used 204,059k (± 0.03%) 203,902k (± 0.03%) -157k (- 0.08%) 203,766k 204,051k
Parse Time 0.78s (± 0.87%) 0.79s (± 1.01%) +0.01s (+ 1.02%) 0.77s 0.81s
Bind Time 0.52s (± 1.11%) 0.52s (± 1.39%) +0.00s (+ 0.38%) 0.50s 0.53s
Check Time 7.81s (± 0.50%) 7.86s (± 0.71%) +0.05s (+ 0.65%) 7.76s 8.05s
Emit Time 2.46s (± 0.78%) 2.47s (± 0.65%) +0.01s (+ 0.41%) 2.42s 2.50s
Total Time 11.56s (± 0.33%) 11.64s (± 0.47%) +0.07s (+ 0.65%) 11.53s 11.83s
Monaco - node (v10.16.3, x64)
Memory used 342,426k (± 0.02%) 342,357k (± 0.03%) -69k (- 0.02%) 342,202k 342,613k
Parse Time 1.48s (± 0.44%) 1.48s (± 0.50%) +0.00s (+ 0.34%) 1.47s 1.50s
Bind Time 0.74s (± 0.66%) 0.75s (± 0.49%) +0.00s (+ 0.27%) 0.74s 0.75s
Check Time 5.47s (± 0.70%) 5.46s (± 0.61%) -0.01s (- 0.15%) 5.38s 5.55s
Emit Time 3.19s (± 0.69%) 3.24s (± 0.90%) +0.06s (+ 1.82%) 3.19s 3.32s
Total Time 10.88s (± 0.22%) 10.94s (± 0.23%) +0.06s (+ 0.52%) 10.88s 11.01s
TFS - node (v10.16.3, x64)
Memory used 305,439k (± 0.03%) 305,469k (± 0.02%) +29k (+ 0.01%) 305,331k 305,647k
Parse Time 1.20s (± 0.46%) 1.20s (± 0.50%) +0.01s (+ 0.58%) 1.19s 1.22s
Bind Time 0.71s (± 0.66%) 0.71s (± 0.69%) +0.00s (+ 0.56%) 0.71s 0.73s
Check Time 5.03s (± 0.43%) 5.05s (± 0.67%) +0.02s (+ 0.50%) 4.98s 5.14s
Emit Time 3.34s (± 0.93%) 3.41s (± 1.50%) +0.07s (+ 2.06%) 3.28s 3.56s
Total Time 10.28s (± 0.35%) 10.38s (± 0.66%) +0.10s (+ 1.01%) 10.17s 10.52s
material-ui - node (v10.16.3, x64)
Memory used 471,310k (± 0.01%) 471,246k (± 0.02%) -64k (- 0.01%) 471,065k 471,434k
Parse Time 1.77s (± 0.40%) 1.78s (± 0.36%) +0.01s (+ 0.40%) 1.76s 1.79s
Bind Time 0.65s (± 0.52%) 0.65s (± 0.89%) 0.00s ( 0.00%) 0.64s 0.66s
Check Time 14.17s (± 0.38%) 14.30s (± 0.72%) +0.14s (+ 0.96%) 14.08s 14.47s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 16.59s (± 0.34%) 16.73s (± 0.65%) +0.14s (+ 0.85%) 16.50s 16.90s
xstate - node (v10.16.3, x64)
Memory used 568,998k (± 0.02%) 569,074k (± 0.02%) +76k (+ 0.01%) 568,800k 569,363k
Parse Time 2.55s (± 0.30%) 2.55s (± 0.49%) +0.01s (+ 0.31%) 2.53s 2.59s
Bind Time 1.00s (± 0.55%) 1.01s (± 0.61%) +0.01s (+ 0.70%) 1.00s 1.02s
Check Time 1.49s (± 0.50%) 1.50s (± 0.97%) +0.00s (+ 0.27%) 1.47s 1.54s
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) 0.00s ( 0.00%) 0.07s 0.07s
Total Time 5.11s (± 0.19%) 5.13s (± 0.39%) +0.02s (+ 0.43%) 5.09s 5.18s
Angular - node (v12.1.0, x64)
Memory used 332,458k (± 0.02%) 332,452k (± 0.02%) -6k (- 0.00%) 332,298k 332,582k
Parse Time 1.94s (± 0.56%) 1.96s (± 1.08%) +0.02s (+ 1.08%) 1.92s 2.02s
Bind Time 0.82s (± 0.91%) 0.82s (± 0.79%) +0.00s (+ 0.24%) 0.81s 0.83s
Check Time 5.30s (± 0.30%) 5.37s (± 1.28%) +0.06s (+ 1.19%) 5.29s 5.62s
Emit Time 6.15s (± 0.96%) 6.18s (± 0.55%) +0.04s (+ 0.62%) 6.11s 6.27s
Total Time 14.21s (± 0.47%) 14.33s (± 0.68%) +0.12s (+ 0.84%) 14.13s 14.61s
Compiler-Unions - node (v12.1.0, x64)
Memory used 191,602k (± 0.03%) 191,358k (± 0.05%) -245k (- 0.13%) 191,007k 191,497k
Parse Time 0.78s (± 0.61%) 0.79s (± 0.96%) +0.00s (+ 0.38%) 0.77s 0.80s
Bind Time 0.53s (± 0.89%) 0.53s (± 1.16%) -0.00s (- 0.38%) 0.52s 0.55s
Check Time 7.28s (± 0.39%) 7.32s (± 0.32%) +0.05s (+ 0.63%) 7.28s 7.39s
Emit Time 2.47s (± 0.81%) 2.47s (± 0.72%) -0.00s (- 0.20%) 2.43s 2.50s
Total Time 11.07s (± 0.38%) 11.11s (± 0.40%) +0.04s (+ 0.39%) 11.03s 11.22s
Monaco - node (v12.1.0, x64)
Memory used 325,493k (± 0.04%) 325,399k (± 0.02%) -95k (- 0.03%) 325,306k 325,542k
Parse Time 1.46s (± 0.72%) 1.47s (± 0.56%) +0.01s (+ 0.62%) 1.45s 1.49s
Bind Time 0.73s (± 0.61%) 0.73s (± 0.79%) +0.00s (+ 0.27%) 0.72s 0.74s
Check Time 5.34s (± 0.48%) 5.37s (± 0.50%) +0.03s (+ 0.49%) 5.31s 5.43s
Emit Time 3.21s (± 0.55%) 3.23s (± 0.47%) +0.02s (+ 0.50%) 3.20s 3.26s
Total Time 10.74s (± 0.36%) 10.79s (± 0.32%) +0.05s (+ 0.50%) 10.73s 10.87s
TFS - node (v12.1.0, x64)
Memory used 290,301k (± 0.03%) 290,263k (± 0.02%) -39k (- 0.01%) 290,177k 290,354k
Parse Time 1.21s (± 0.48%) 1.22s (± 0.53%) +0.01s (+ 0.74%) 1.21s 1.24s
Bind Time 0.68s (± 0.54%) 0.69s (± 0.75%) +0.00s (+ 0.58%) 0.68s 0.70s
Check Time 4.94s (± 0.47%) 4.95s (± 0.80%) +0.01s (+ 0.22%) 4.88s 5.07s
Emit Time 3.40s (± 1.27%) 3.40s (± 0.65%) +0.00s (+ 0.03%) 3.33s 3.43s
Total Time 10.23s (± 0.63%) 10.25s (± 0.47%) +0.03s (+ 0.25%) 10.15s 10.40s
material-ui - node (v12.1.0, x64)
Memory used 450,083k (± 0.02%) 449,965k (± 0.05%) -119k (- 0.03%) 449,028k 450,214k
Parse Time 1.78s (± 0.33%) 1.78s (± 0.66%) +0.01s (+ 0.28%) 1.76s 1.81s
Bind Time 0.64s (± 1.14%) 0.64s (± 1.27%) +0.01s (+ 0.78%) 0.62s 0.66s
Check Time 12.69s (± 0.40%) 12.75s (± 0.55%) +0.07s (+ 0.52%) 12.64s 12.92s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.10s (± 0.33%) 15.18s (± 0.45%) +0.07s (+ 0.49%) 15.05s 15.31s
xstate - node (v12.1.0, x64)
Memory used 535,055k (± 0.02%) 535,067k (± 0.01%) +12k (+ 0.00%) 534,872k 535,205k
Parse Time 2.48s (± 0.47%) 2.49s (± 0.65%) +0.02s (+ 0.65%) 2.46s 2.53s
Bind Time 1.04s (± 0.75%) 1.04s (± 0.71%) +0.00s (+ 0.10%) 1.03s 1.06s
Check Time 1.43s (± 0.56%) 1.44s (± 0.67%) +0.01s (+ 0.91%) 1.42s 1.46s
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) 0.00s ( 0.00%) 0.07s 0.07s
Total Time 5.02s (± 0.27%) 5.04s (± 0.32%) +0.03s (+ 0.60%) 5.01s 5.08s
Angular - node (v14.15.1, x64)
Memory used 330,820k (± 0.01%) 330,848k (± 0.01%) +27k (+ 0.01%) 330,785k 330,950k
Parse Time 1.94s (± 0.53%) 1.95s (± 0.74%) +0.01s (+ 0.72%) 1.92s 1.99s
Bind Time 0.86s (± 0.55%) 0.86s (± 0.42%) +0.00s (+ 0.47%) 0.86s 0.87s
Check Time 5.38s (± 0.48%) 5.40s (± 0.27%) +0.02s (+ 0.33%) 5.36s 5.43s
Emit Time 6.15s (± 0.33%) 6.23s (± 0.92%) +0.08s (+ 1.38%) 6.11s 6.41s
Total Time 14.32s (± 0.26%) 14.44s (± 0.42%) +0.12s (+ 0.87%) 14.31s 14.62s
Compiler-Unions - node (v14.15.1, x64)
Memory used 192,757k (± 0.49%) 191,700k (± 0.63%) -1,057k (- 0.55%) 190,018k 193,379k
Parse Time 0.81s (± 1.01%) 0.81s (± 0.59%) 0.00s ( 0.00%) 0.80s 0.82s
Bind Time 0.55s (± 0.73%) 0.55s (± 0.40%) 0.00s ( 0.00%) 0.55s 0.56s
Check Time 7.34s (± 0.34%) 7.41s (± 0.64%) +0.08s (+ 1.02%) 7.32s 7.49s
Emit Time 2.48s (± 0.90%) 2.49s (± 0.87%) +0.01s (+ 0.28%) 2.44s 2.54s
Total Time 11.18s (± 0.30%) 11.26s (± 0.45%) +0.08s (+ 0.73%) 11.14s 11.35s
Monaco - node (v14.15.1, x64)
Memory used 324,294k (± 0.00%) 324,178k (± 0.00%) -115k (- 0.04%) 324,157k 324,211k
Parse Time 1.50s (± 0.56%) 1.51s (± 0.48%) +0.01s (+ 0.47%) 1.49s 1.53s
Bind Time 0.76s (± 0.59%) 0.76s (± 0.66%) -0.00s (- 0.40%) 0.75s 0.77s
Check Time 5.29s (± 0.43%) 5.30s (± 0.41%) +0.01s (+ 0.15%) 5.24s 5.34s
Emit Time 3.25s (± 0.78%) 3.27s (± 0.63%) +0.03s (+ 0.80%) 3.22s 3.32s
Total Time 10.80s (± 0.35%) 10.83s (± 0.29%) +0.04s (+ 0.33%) 10.76s 10.91s
TFS - node (v14.15.1, x64)
Memory used 289,141k (± 0.00%) 289,115k (± 0.01%) -26k (- 0.01%) 289,058k 289,159k
Parse Time 1.22s (± 0.82%) 1.23s (± 0.81%) +0.01s (+ 0.82%) 1.22s 1.26s
Bind Time 0.73s (± 0.55%) 0.73s (± 0.99%) +0.00s (+ 0.14%) 0.72s 0.75s
Check Time 4.91s (± 0.33%) 4.94s (± 0.62%) +0.03s (+ 0.65%) 4.88s 5.03s
Emit Time 3.51s (± 0.53%) 3.51s (± 0.68%) +0.01s (+ 0.20%) 3.45s 3.58s
Total Time 10.38s (± 0.26%) 10.42s (± 0.51%) +0.05s (+ 0.44%) 10.29s 10.53s
material-ui - node (v14.15.1, x64)
Memory used 448,276k (± 0.01%) 448,182k (± 0.05%) -95k (- 0.02%) 447,218k 448,332k
Parse Time 1.82s (± 0.34%) 1.84s (± 0.44%) +0.02s (+ 0.99%) 1.81s 1.85s
Bind Time 0.67s (± 0.54%) 0.68s (± 0.50%) +0.00s (+ 0.45%) 0.67s 0.68s
Check Time 12.77s (± 0.39%) 12.88s (± 0.85%) +0.11s (+ 0.88%) 12.64s 13.15s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 15.26s (± 0.33%) 15.39s (± 0.73%) +0.13s (+ 0.88%) 15.15s 15.67s
xstate - node (v14.15.1, x64)
Memory used 532,756k (± 0.01%) 532,777k (± 0.01%) +22k (+ 0.00%) 532,713k 532,869k
Parse Time 2.55s (± 0.63%) 2.55s (± 0.37%) -0.00s (- 0.04%) 2.53s 2.57s
Bind Time 1.15s (± 1.10%) 1.15s (± 0.86%) +0.00s (+ 0.26%) 1.14s 1.18s
Check Time 1.48s (± 0.52%) 1.48s (± 0.63%) +0.01s (+ 0.47%) 1.46s 1.50s
Emit Time 0.07s (± 0.00%) 0.07s (± 3.14%) +0.00s (+ 1.43%) 0.07s 0.08s
Total Time 5.25s (± 0.50%) 5.26s (± 0.24%) +0.01s (+ 0.21%) 5.24s 5.30s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory10 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v10.16.3, x64)
  • node (v12.1.0, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v10.16.3, x64)
  • Angular - node (v12.1.0, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v10.16.3, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v10.16.3, x64)
  • Monaco - node (v12.1.0, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v10.16.3, x64)
  • TFS - node (v12.1.0, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v10.16.3, x64)
  • material-ui - node (v12.1.0, x64)
  • material-ui - node (v14.15.1, x64)
  • xstate - node (v10.16.3, x64)
  • xstate - node (v12.1.0, x64)
  • xstate - node (v14.15.1, x64)
Benchmark Name Iterations
Current 46981 10
Baseline main 10

Developer Information:

Download Benchmark

@weswigham
Copy link
Member

weswigham commented Mar 5, 2022

I'm want to merge this, because I think the early bail is fine to have regardless of anything else, however I think there's another underlying issue that probably needs fixing, so it'd be nice if we had a tweaked example that still triggered an issue and an issue to track fixing it. Likely something like

type Box = {
  content?: Foo | Box
};
declare const b: Box;
class Foo {
  get foo() {
    return {
      content: this as Foo | Box,
      ...b
    };
  }
}

will continue to trigger a new-ish circularity error.

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.

3 participants