Skip to content

Reduce CPU time spent parsing very long build output lines - #5144

Merged
snehara99 merged 3 commits into
mainfrom
dev/hanniavalera/diagnostic-parsers-long-lines
Nov 5, 2026
Merged

snehara99 merged 3 commits into
mainfrom
dev/hanniavalera/diagnostic-parsers-long-lines

Conversation

@hanniavalera

Copy link
Copy Markdown
Contributor

Context

Build output goes through the diagnostic parsers one line at a time on the extension host, and a few of their regexes get very slow on long lines that don't match. xcodebuild (Xcode generator) and ninja -v can print compile commands that run to many KB, and the CPU profiles in #4828 and #4229 both land on the first GCC pattern. This is related to those two, but I'm not claiming it closes either.

Changes

  • Anchor the two GCC template-diagnostic patterns with ^ and the m flag. . can't cross a line terminator, so the leftmost match always started at a line start anyway. What matches doesn't change; the engine just stops retrying from every offset.
  • Only run the MSVC linker regex on lines that contain LNK. It backtracks badly on lines that start with a lot of whitespace, like caret lines.
  • Only run the GHS and Diab regexes on lines that contain the text every match needs: ", plus (col. or At end of source for GHS, ", plus ): for Diab.
  • Add backend tests and a CHANGELOG entry.

Verification & Testing

  • Tested locally on an Apple Silicon Mac (Node 22) by running yarn lint and yarn backendTests. Both pass, 496 tests, 25 of them new.
  • The new tests cover behavior (every line terminator the anchors have to handle, last marker wins, tab-separated GHS/Diab fields) and timing, using lines the old code needs over a second for. All 11 timing tests fail on main and pass here.
  • Rough numbers from the same machine, main vs this branch: single calls on synthetic lines went from about 1.6 s to 0.3 ms for a 64 KiB quoted -D command line in the GCC parser, and from about 3.9 s to 0.2 ms for a 512-space caret line in the MSVC parser. Replaying about 1 MB of real xcodebuild output through the build consumer went from 1.1-1.6 s to 34-37 ms of process CPU (warm medians).
  • Compared parser results against main on several million generated and mutated lines and found no differences. The existing diagnostics tests also pass with a stubbed vscode in place of the extension host. I haven't tested this change in an extension host or on Windows/Linux.
  • Lines that contain the marker text but aren't diagnostics can still be slow in the GHS, Diab and MSVC linker parsers. This doesn't try to make the whole pipeline linear.
Build output reaches the diagnostic parsers line by line on the extension host,
and some tools print very long lines: xcodebuild (Xcode generator) and
`ninja -v` echo whole compile commands, and some compilers print caret lines
with hundreds of leading spaces. Several parser patterns cost far more than
linear time on such a line when it does not match:

- gcc: the template-instantiation and "required from" patterns start with
  `(.*)` and were unanchored, so they were retried from every offset (the first
  GCC regex dominates the profiles in #4828 and #4229). They are now anchored
  with `^` and the `m` flag. `.` cannot cross a line terminator, so the
  leftmost match always starts where `^` matches with `m`: what is matched is
  unchanged.
- msvc: LINKER_REGEX backtracks very heavily on whitespace-led lines. It is now
  only tried on lines that contain `LNK`, which every match needs.
- ghs and diab: the patterns are unanchored and were retried from every quote.
  They are now only tried on lines that contain text every match needs (`",`
  plus a location marker for GHS, `",` plus `):` for Diab).

Single-call wall timings on synthetic lines, Apple Silicon Mac with Node 22,
main versus this change: a 64 KiB quoted -D command line in the GCC parser
1.6 s -> 0.3 ms, a 512-space caret line in the MSVC parser 3.9 s -> 0.2 ms, a
128 KiB command line in the GHS and Diab parsers 0.75 s and 0.96 s -> 0.1 ms.
Replaying real xcodebuild output (about 1 MB) through the build output
consumer used 1.1-1.6 s of process CPU before and 34-37 ms after (warm
medians). The literal checks are necessary conditions of the unchanged
patterns, so results are identical (compared with main over several million
generated and mutated lines). Lines that contain the required text but are not
diagnostics can still be slow in the GHS, Diab and MSVC linker parsers.

Adds backend tests: behavior tests that pin what the parsers still recognize
(including every line terminator the anchors must handle) and budget tests with
lines long enough for the unfixed patterns to need seconds.

Related CPU profiles: #4828, #4229
@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.
@snehara99
snehara99 merged commit 41d981e into main Nov 5, 2026
33 checks passed
@snehara99
snehara99 deleted the dev/hanniavalera/diagnostic-parsers-long-lines branch November 5, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants