Skip to content

Fix the flaky language-server scan timing test at its cause - #152

Merged
collin-thomas merged 4 commits into
mainfrom
pre-release-20261112
Nov 12, 2026
Merged

collin-thomas merged 4 commits into
mainfrom
pre-release-20261112

Conversation

@collin-thomas

Copy link
Copy Markdown
Collaborator

For the 1.0.0 release. Fixes the language-server timing test that failed four times in recent CI runs, plus a real offset bug found along the way. Independent of #151.

The flaky test

lsp/src/typescript-service.linear-time.test.ts checks that finding <script> blocks stays linear on half-typed markup. It times an input and one four times its size: linear work grows about 4×, quadratic about 16×, and the bar is 8.

All four CI failures were the same case, "a quote that never ends" (<script type="aaa… at 4 MB and 16 MB). Those runs read 12.2, 12.6, 16.5 and 16.6. It reads about 4 locally.

Two things were wrong.

1. The scanner copied the page on every scan. scanScripts lowercased the whole page to match tag names in any case. For this input that copy was most of the time measured, about 165 of 195 ns per KB, so the test was mostly timing a 16 MB allocation per run. The language server also runs this scan on keystrokes.

2. The check timed reading memory against a fixed bar. With the copy gone, this case is one native search over the text. Its cost per character depends on whether the text fits the runner's CPU caches. On one CI runner a plain native search read 5.2 to 5.6 at 1 MB vs 4 MB, where linear should read 4. Cache sizes differ between runners, and an earlier fix that raised the sizes from 256 KB to 4 MB only moved the edge.

I couldn't reproduce the 12–16 readings on the runners I sampled; the old code read linear there too. So I can't say how much of each failure was the copy and how much was the cache. The test change below holds either way.

Changes

  • typescript-blocks.ts: tag names are compared in place, in ASCII case, as an HTML parser compares them. No copy of the page is made.
  • typescript-service.linear-time.test.ts: the memory-bound case is now measured against a native search over the same two texts. Both pay the same cost of reading memory on any runner, so that cost cancels out. Linear work reads about 1, and the bar is 2. The other cases run JavaScript loops, have not flaked, and keep their bar of 8.

Readings of the new check, at the test's sizes (4 MB vs 16 MB):

Local (M1) CI runner
New scan 0.98–1.06 0.98–1.02
Deliberately quadratic control 3.27–3.45 3.29–3.39

The real bug found along the way

Unicode lowercasing can make a string longer: "İ".toLowerCase() is two characters. Positions found in the lowercased copy were used on the original page, so after every İ they pointed one character further on. A TypeScript block after "İstanbul" came back with a stray </ on its end, which then showed up in diagnostics and editor features. A new test fails without the fix. #151 fixes the same pattern in five other places.

Verification

  • Language-server unit tests (2,700) and linear-time tests: pass locally.
  • Language-server typecheck, format, spelling: clean.
  • The temporary probe that collected the CI readings was removed in the last commit.

🤖 Generated with Claude Code

collin-thomas and others added 4 commits November 12, 2026 08:16
scanScripts lowercased the whole page to find tag names in any case. That
copied the page on every scan, which the language server runs per
keystroke, and most of the scan's time went to the copy. Unicode
lowercasing can also lengthen a string ("İ" becomes two characters), so
every offset after one pointed past the real text: a block after "İ" came
back with "</" on its end.

Tag names are now compared in place, in ASCII case, as an HTML parser
compares them.

Also adds a temporary probe that prints growth readings on CI for the
linear-time case that keeps failing there; it is removed before merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The "quote that never ends" check times one native search over 4 MB and
16 MB of text, so its growth is the CPU's cost of reading memory, which
changes where an input stops fitting a cache. Where that is depends on the
runner: a plain search read 5.2 to 5.6 at 1 MB against 4 MB on one CI
runner, and this check read 12.2 to 16.6 at 4 MB against 16 MB on others,
for linear work.

It is now taken over the growth of a native search over the same two
texts, which pays the same cost of reading them. A linear scan reads about
1 and a scan that rescans the text reads about 3.3 here; the bar is 2.

The probe now also prints these ratios, with a quadratic control.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@collin-thomas
collin-thomas merged commit 91f0412 into main Nov 12, 2026
74 of 106 checks passed
@collin-thomas
collin-thomas deleted the pre-release-20261112 branch November 12, 2026 16:30
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.

1 participant