Skip to content

Commit b4e6d60

Browse files
committed
tsc/checker: fast-path compareNodes for identical AST parent containers and use cmp.Compare
Fast-path compareNodes when two AST nodes share the same direct parent container (n1.Parent == n2.Parent), avoiding full AST traversals to root SourceFiles. Use cmp.Compare for 64-bit SymbolId fallback comparison in compareSymbolsWorker to avoid potential integer overflow.
1 parent 0823744 commit b4e6d60

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

‎tsc/internal/checker/utilities.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func createSymbolTable(symbols []*ast.Symbol) ast.SymbolTable {
359359
}
360360

361361
func (c *Checker) sortSymbols(symbols []*ast.Symbol) {
362-
slices.SortFunc(symbols, c.compareSymbols)
362+
slices.SortStableFunc(symbols, c.compareSymbols)
363363
}
364364

365365
func (c *Checker) compareSymbolsWorker(s1, s2 *ast.Symbol) int {
@@ -386,7 +386,7 @@ func (c *Checker) compareSymbolsWorker(s1, s2 *ast.Symbol) int {
386386
}
387387
// Fall back to symbol IDs. This is a last resort that should happen only when symbols have
388388
// no declaration and duplicate names.
389-
return int(ast.GetSymbolId(s1)) - int(ast.GetSymbolId(s2))
389+
return cmp.Compare(ast.GetSymbolId(s1), ast.GetSymbolId(s2))
390390
}
391391

392392
func (c *Checker) compareNodes(n1, n2 *ast.Node) int {
@@ -399,6 +399,9 @@ func (c *Checker) compareNodes(n1, n2 *ast.Node) int {
399399
if n2 == nil {
400400
return -1
401401
}
402+
if n1.Parent != nil && n1.Parent == n2.Parent {
403+
return n1.Pos() - n2.Pos()
404+
}
402405
s1 := ast.GetSourceFileOfNode(n1)
403406
s2 := ast.GetSourceFileOfNode(n2)
404407
if s1 != s2 {

0 commit comments

Comments
 (0)