From a70d77f0ada61b9955eb8d83ced3572a92926f72 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:25:15 -0700 Subject: [PATCH] Inline the common fast-fail of isSimpleTypeRelatedTo isSimpleTypeRelatedTo is called up to twice for nearly every relation check, and in the overwhelmingly common case (relating two structured or instantiable types where the target is not a union) it walks a long list of primitive flag tests only to return false. The function was far too large to inline, so each of those checks also paid for a call. Split it into a small wrapper that fits the inlining budget and rejects that case directly, and a worker holding the full logic. The worker additionally short-circuits two cases whose outcome is already decided by a couple of flag tests: two enum members of the same kind, and two structured or instantiable types (where only the unknown-like union check can apply). Results are unchanged. Co-authored-by: Steven Chong <25894545+teamchong@users.noreply.github.com> --- tsc/internal/checker/relater.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tsc/internal/checker/relater.go b/tsc/internal/checker/relater.go index 562ae0451cb43..abc0c4286d413 100644 --- a/tsc/internal/checker/relater.go +++ b/tsc/internal/checker/relater.go @@ -203,9 +203,27 @@ func (c *Checker) isTypeRelatedTo(source *Type, target *Type, relation *Relation return false } +// isSimpleTypeRelatedTo is small enough to be inlined into callers, so relating two structured or instantiable +// types when the target isn't a union, by far the most common case, fails without a function call. func (c *Checker) isSimpleTypeRelatedTo(source *Type, target *Type, relation *Relation, errorReporter ErrorReporter) bool { + if source.flags&^TypeFlagsStructuredOrInstantiable|target.flags&^(TypeFlagsStructuredOrInstantiable&^TypeFlagsUnion) == 0 { + return false + } + return c.isSimpleTypeRelatedToWorker(source, target, relation, errorReporter) +} + +func (c *Checker) isSimpleTypeRelatedToWorker(source *Type, target *Type, relation *Relation, errorReporter ErrorReporter) bool { s := source.flags t := target.flags + // The following two shortcuts produce the same results as the full set of checks below. + // Two enum members of the same kind: only the matching value and enum check can apply. + if s == t && (s == TypeFlagsNumberLiteral|TypeFlagsEnumLiteral || s == TypeFlagsStringLiteral|TypeFlagsEnumLiteral) { + return source.AsLiteralType().value == target.AsLiteralType().value && c.isEnumTypeRelatedTo(source.symbol, target.symbol, errorReporter) + } + // Two structured or instantiable types: only the unknown-like union check can apply. + if (s|t)&^TypeFlagsStructuredOrInstantiable == 0 { + return t&TypeFlagsUnion != 0 && (relation == c.assignableRelation || relation == c.comparableRelation) && c.isUnknownLikeUnionType(target) + } if t&TypeFlagsAny != 0 || s&TypeFlagsNever != 0 || source == c.wildcardType { return true }