Skip to content

Commit 975e07a

Browse files
Fix perfromance when there's lots of error (#2283)
1 parent 0b6d133 commit 975e07a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

internal/ast/diagnostic.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"slices"
66
"strings"
77

8+
"github.com/microsoft/typescript-go/internal/collections"
89
"github.com/microsoft/typescript-go/internal/core"
910
"github.com/microsoft/typescript-go/internal/diagnostics"
1011
"github.com/microsoft/typescript-go/internal/locale"
@@ -140,8 +141,10 @@ func NewCompilerDiagnostic(message *diagnostics.Message, args ...any) *Diagnosti
140141
}
141142

142143
type DiagnosticsCollection struct {
143-
fileDiagnostics map[string][]*Diagnostic
144-
nonFileDiagnostics []*Diagnostic
144+
fileDiagnostics map[string][]*Diagnostic
145+
fileDiagnosticsSorted collections.Set[string]
146+
nonFileDiagnostics []*Diagnostic
147+
nonFileDiagnosticsSorted bool
145148
}
146149

147150
func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) {
@@ -150,18 +153,20 @@ func (c *DiagnosticsCollection) Add(diagnostic *Diagnostic) {
150153
if c.fileDiagnostics == nil {
151154
c.fileDiagnostics = make(map[string][]*Diagnostic)
152155
}
153-
c.fileDiagnostics[fileName] = core.InsertSorted(c.fileDiagnostics[fileName], diagnostic, CompareDiagnostics)
156+
c.fileDiagnostics[fileName] = append(c.fileDiagnostics[fileName], diagnostic)
157+
c.fileDiagnosticsSorted.Delete(fileName)
154158
} else {
155-
c.nonFileDiagnostics = core.InsertSorted(c.nonFileDiagnostics, diagnostic, CompareDiagnostics)
159+
c.nonFileDiagnostics = append(c.nonFileDiagnostics, diagnostic)
160+
c.nonFileDiagnosticsSorted = false
156161
}
157162
}
158163

159164
func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic {
160165
var diagnostics []*Diagnostic
161166
if diagnostic.File() != nil {
162-
diagnostics = c.fileDiagnostics[diagnostic.File().FileName()]
167+
diagnostics = c.GetDiagnosticsForFile(diagnostic.File().FileName())
163168
} else {
164-
diagnostics = c.nonFileDiagnostics
169+
diagnostics = c.GetGlobalDiagnostics()
165170
}
166171
if i, ok := slices.BinarySearchFunc(diagnostics, diagnostic, CompareDiagnostics); ok {
167172
return diagnostics[i]
@@ -170,10 +175,18 @@ func (c *DiagnosticsCollection) Lookup(diagnostic *Diagnostic) *Diagnostic {
170175
}
171176

172177
func (c *DiagnosticsCollection) GetGlobalDiagnostics() []*Diagnostic {
178+
if !c.nonFileDiagnosticsSorted {
179+
slices.SortStableFunc(c.nonFileDiagnostics, CompareDiagnostics)
180+
c.nonFileDiagnosticsSorted = true
181+
}
173182
return c.nonFileDiagnostics
174183
}
175184

176185
func (c *DiagnosticsCollection) GetDiagnosticsForFile(fileName string) []*Diagnostic {
186+
if !c.fileDiagnosticsSorted.Has(fileName) {
187+
slices.SortStableFunc(c.fileDiagnostics[fileName], CompareDiagnostics)
188+
c.fileDiagnosticsSorted.Add(fileName)
189+
}
177190
return c.fileDiagnostics[fileName]
178191
}
179192

0 commit comments

Comments
 (0)