Skip to content

Conversation

@GabrielCastro
Copy link
Contributor

DiagnosticsCollection#Add() uses InsertSort, this causes a ton of memcopys on every error. This is O(n) for every error.

Instead I moved the sort to lazily happen just before each GetDiagnostics call this is O(nlogn), but gets happen way less often than Inserts.

This happens on codebases with lots of usages of react-hook-form due to #988 . While this isn't a fix for that it allows builds to fail instead of hanging for hours

DiagnosticsCollection#Add() used InsertSort, this causes a ton of
memcopys on every error. This is O(n) for every error.

Instead I moved the sort to lazily happen just before each GetDiagnostics call this is not only O(nlogn), but gets happen way less often than Inserts
@jakebailey
Copy link
Member

This looks fine, but you should run hereby format.

c.fileDiagnostics = make(map[string][]*Diagnostic)
}
c.fileDiagnostics[fileName] = core.InsertSorted(c.fileDiagnostics[fileName], diagnostic, CompareDiagnostics)
if c.fileDiagnosticsSorted == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need sorting here as diagnostics are anyways sorted and deduplicated at end points?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sorted so lookup can find diags later; it's a weird system but yeah

@jakebailey jakebailey enabled auto-merge December 9, 2025 07:12
@jakebailey jakebailey added this pull request to the merge queue Dec 9, 2025
Merged via the queue into microsoft:main with commit 975e07a Dec 9, 2025
22 checks passed
@jakebailey
Copy link
Member

This turns out to be unsafe; it causes a data race when concurrently accessing processor diagnostics. We don't see the problem in main, though, only on a WIP branch I have for bringing back #2197.

@jakebailey
Copy link
Member

And, since it sorts the list on the fly, if something previously grabbed a list of diags (say, the global diags), and then they changed later, the list would potentially be concurrently modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants