Skip to content

Cache the regularized form of union types #37749

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 3 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13459,7 +13459,7 @@ namespace ts {

function getRegularTypeOfLiteralType(type: Type): Type {
return type.flags & TypeFlags.Literal ? (<LiteralType>type).regularType :
type.flags & TypeFlags.Union ? getUnionType(sameMap((<UnionType>type).types, getRegularTypeOfLiteralType)) :
type.flags & TypeFlags.Union ? ((<UnionType>type).regularType || ((<UnionType>type).regularType = getUnionType(sameMap((<UnionType>type).types, getRegularTypeOfLiteralType)) as UnionType)) :
type;
}

Expand Down Expand Up @@ -15457,6 +15457,16 @@ namespace ts {
return isIdenticalTo(source, target);
}


// We fastpath comparing a type parameter to exactly its constraint, as this is _super_ common,
// and otherwise, for type parameters in large unions, causes us to need to compare the union to itself,
// as we break down the _target_ union first, _then_ get the source constraint - so for every
// member of the target, we attempt to find a match in the source. This avoids that in cases where
// the target is exactly the constraint.
if (source.flags & TypeFlags.TypeParameter && getConstraintOfType(source) === target) {
return Ternary.True;
}

// Try to see if we're relating something like `Foo` -> `Bar | null | undefined`.
// If so, reporting the `null` and `undefined` in the type is hardly useful.
// First, see if we're even relating an object type to a union.
Expand Down Expand Up @@ -15787,7 +15797,16 @@ namespace ts {
function eachTypeRelatedToType(source: UnionOrIntersectionType, target: Type, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
let result = Ternary.True;
const sourceTypes = source.types;
for (const sourceType of sourceTypes) {
for (let i = 0; i < sourceTypes.length; i++) {
const sourceType = sourceTypes[i];
if (target.flags & TypeFlags.Union && (target as UnionType).types.length === sourceTypes.length) {
// many unions are mappings of one another; in such cases, simply comparing members at the same index can shortcut the comparison
const related = isRelatedTo(sourceType, (target as UnionType).types[i], /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure I see why this would help. Union constituents are sorted by their type ids and those are pretty random even when one union is a mapping of the other. Am I missing something?

Copy link
Member Author

@weswigham weswigham Apr 3, 2020

Choose a reason for hiding this comment

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

Type IDs are assigned as they are made, and unions are sorted; ergo, unions are ordered by the order the types within them were created in - for a union that results from a mapping on another union, that means the resulting union will share the input union's order. We exploit this here by checking those corresponding elements against one another to make this common case linear in comparison time (when the result is assignable), rather than quadratic.

Copy link
Member Author

Choose a reason for hiding this comment

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

This change is responsible for most of the performance improvements from the last commit, not the other one. The other one allows us to skip one silly quadratic comparison of a union to itself, while this is allowing us to compare BigUnion to DiscriminateType<BigUnion, "name", T> in a linear way (DiscriminateType is a distributive conditional, so produces a union of types which are ultimately identical to BigUnion, as T is constrained to the values of "name").

Copy link
Member

Choose a reason for hiding this comment

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

Right, in cases where the mapping creates new type IDs for each element I can see how this helps.

if (related) {
result &= related;
continue;
}
}
const related = isRelatedTo(sourceType, target, reportErrors, /*headMessage*/ undefined, intersectionState);
if (!related) {
return Ternary.False;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4651,6 +4651,8 @@ namespace ts {
export interface UnionType extends UnionOrIntersectionType {
/* @internal */
resolvedReducedType: Type;
/* @internal */
regularType: UnionType;
}

export interface IntersectionType extends UnionOrIntersectionType {
Expand Down
Loading