Skip to content

Commit 9e8229d

Browse files
committed
Performance improvements
1 parent 070ad50 commit 9e8229d

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8949,36 +8949,36 @@ namespace ts {
89498949
target = (<LiteralType>target).regularType;
89508950
}
89518951
// both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases
8952-
if (source === target) return Ternary.True;
8952+
if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) {
8953+
return Ternary.True;
8954+
}
89538955

8954-
const comparisonId = getRelationKey(source, target, relation);
8955-
if (relation.has(comparisonId)) {
8956-
const result = relation.get(comparisonId);
8956+
const id = getRelationKey(source, target, relation);
8957+
const related = relation.get(id);
8958+
if (related !== undefined) {
89578959
// If we need to report errors, and the result is RelationComparisonResult.Failed, then we need
89588960
// to redo our work to generate an error message. Otherwise, we can just return the cached result.
8959-
if (!reportErrors || result !== RelationComparisonResult.Failed) {
8960-
if (reportErrors && result !== RelationComparisonResult.Succeeded) {
8961+
if (!reportErrors || related !== RelationComparisonResult.Failed) {
8962+
if (reportErrors && related !== RelationComparisonResult.Succeeded) {
89618963
reportElaboratedRelationError(headMessage, source, target);
89628964
}
8963-
return result === RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False;
8965+
return related === RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False;
89648966
}
8965-
if (reportErrors && result === RelationComparisonResult.Failed) {
8966-
relation.set(comparisonId, RelationComparisonResult.FailedAndReported);
8967+
if (reportErrors && related === RelationComparisonResult.Failed) {
8968+
relation.set(id, RelationComparisonResult.FailedAndReported);
89678969
}
89688970
}
89698971

89708972
if (relation === identityRelation) {
8971-
return cacheResult(isIdenticalTo(source, target), comparisonId, /*reportErrors*/ false);
8973+
return cacheResult(isIdenticalTo(source, target, id), id, /*reportErrors*/ false);
89728974
}
89738975

8974-
if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) return Ternary.True;
8975-
89768976
if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && source.flags & TypeFlags.FreshLiteral) {
89778977
if (hasExcessProperties(<FreshObjectLiteralType>source, target, reportErrors)) {
89788978
if (reportErrors) {
89798979
reportRelationError(headMessage, source, target);
89808980
}
8981-
return cacheResult(Ternary.False, comparisonId, reportErrors);
8981+
return cacheResult(Ternary.False, id, reportErrors);
89828982
}
89838983
// Above we check for excess properties with respect to the entire target type. When union
89848984
// and intersection types are further deconstructed on the target side, we don't want to
@@ -9052,7 +9052,7 @@ namespace ts {
90529052
result = someTypeRelatedToType(<IntersectionType>source, target, /*reportErrors*/ false);
90539053
}
90549054
if (!result && (source.flags & TypeFlags.StructuredOrTypeVariable || target.flags & TypeFlags.StructuredOrTypeVariable)) {
9055-
if (result = recursiveTypeRelatedTo(source, target, reportErrors)) {
9055+
if (result = recursiveTypeRelatedTo(source, target, id, reportErrors)) {
90569056
errorInfo = saveErrorInfo;
90579057
}
90589058
}
@@ -9064,7 +9064,7 @@ namespace ts {
90649064
reportElaboratedRelationError(headMessage, source, target);
90659065
}
90669066

9067-
return cacheResult(result, comparisonId, reportErrors);
9067+
return cacheResult(result, id, reportErrors);
90689068
}
90699069

90709070
function cacheResult(result: Ternary, comparisonId: string, reportErrors?: boolean) {
@@ -9074,10 +9074,10 @@ namespace ts {
90749074
return result;
90759075
}
90769076

9077-
function isIdenticalTo(source: Type, target: Type): Ternary {
9077+
function isIdenticalTo(source: Type, target: Type, id: string): Ternary {
90789078
let result: Ternary;
90799079
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) {
9080-
return recursiveTypeRelatedTo(source, target, /*reportErrors*/ false);
9080+
return recursiveTypeRelatedTo(source, target, id, /*reportErrors*/ false);
90819081
}
90829082
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union ||
90839083
source.flags & TypeFlags.Intersection && target.flags & TypeFlags.Intersection) {
@@ -9238,11 +9238,10 @@ namespace ts {
92389238
// Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are
92399239
// equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion
92409240
// and issue an error. Otherwise, actually compare the structure of the two types.
9241-
function recursiveTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
9241+
function recursiveTypeRelatedTo(source: Type, target: Type, id: string, reportErrors: boolean): Ternary {
92429242
if (overflow) {
92439243
return Ternary.False;
92449244
}
9245-
const id = getRelationKey(source, target, relation);
92469245
if (!maybeKeys) {
92479246
maybeKeys = [];
92489247
sourceStack = [];

0 commit comments

Comments
 (0)