From 96496c22ca442f92817b2ebe90540ef2a013d293 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:35:44 -0800 Subject: [PATCH] checker: add string-literal fast path in CompareTypes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CompareTypes previously fell through the full type-comparison dispatch even when both operands were string literal types. Short-circuit string-literal pairs directly with a string comparison. benchstat (-pgo=off, parent vs commit, n=6): goos: linux goarch: amd64 │ old │ new │ │ sec/op │ sec/op vs base │ Tsc/vscode 6.248 ± 2% 6.192 ± 2% ~ (p=0.240 n=6) Tsc/mui-docs 3.732 ± 2% 3.742 ± 3% ~ (p=0.699 n=6) geomean 4.829 4.814 -0.32% │ old │ new │ │ B-rss │ B-rss vs base │ Tsc/vscode 4.576Gi ± 2% 4.546Gi ± 3% ~ (p=0.589 n=6) Tsc/mui-docs 2.174Gi ± 4% 2.177Gi ± 4% ~ (p=1.000 n=6) geomean 3.154Gi 3.146Gi -0.27% --- tsc/internal/checker/utilities.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tsc/internal/checker/utilities.go b/tsc/internal/checker/utilities.go index d8ef84a30e09d..c73c6a53f48c5 100644 --- a/tsc/internal/checker/utilities.go +++ b/tsc/internal/checker/utilities.go @@ -431,6 +431,18 @@ func CompareTypes(t1, t2 *Type) int { if c := getSortOrderFlags(t1) - getSortOrderFlags(t2); c != 0 { return c } + // Fast path for two unaliased string literal types. + if t1.flags == TypeFlagsStringLiteral && t2.flags == TypeFlagsStringLiteral && t1.alias == nil && t2.alias == nil { + s1 := t1.AsLiteralType().value.(string) + s2 := t2.AsLiteralType().value.(string) + if s1 != s2 { + if s1 < s2 { + return -1 + } + return 1 + } + return int(t1.id) - int(t2.id) + } // Order named types by name and, in the case of aliased types, by alias type arguments. if c := compareTypeNames(t1, t2); c != 0 { return c