Skip to content

test(lint): bound lint growth by counted string work, not a busy runner's clock - #8858

Open
miguel-heygen wants to merge 2 commits into
mainfrom
fix/lint-linear-time-test
Open

miguel-heygen wants to merge 2 commits into
mainfrom
fix/lint-linear-time-test

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

The two linear-time tests in packages/lint/src/rules/gsap.test.ts now measure how much work lint does, not how long it takes:

  • lints script tags naming one file in linear time
  • lints timeline scripts beside named elements in linear time

They still fail when lint goes quadratic. A busy runner can no longer fail them.

Why

Both tests compared wall-clock time at n and 4n tags and allowed a ratio up to 8. Linear lint grows about 4x and quadratic about 16x. On a loaded merge-queue runner, the script-tags test measured 8.92 and ejected a release from the queue, although lint itself had not changed. A ratio of two timings moves with whatever else the machine is doing.

How

growth now counts work. While lint runs, the test wraps the string and regex methods lint scans with (charCodeAt, slice, split, indexOf, match, replace, exec, test and the like). It counts each call plus the characters the call returns, then restores every method. For a given page the count is the same on every run, whatever the load. The test runs each size once and compares the two counts.

The bound is now 6, not 8. A deterministic count needs no headroom for noise, so the bound only has to separate linear and n log n growth (about 4.8x here) from quadratic growth (about 16x).

I measured the ratios on the same two pages:

Lint code script tags (n=1250 → 5000) timeline scripts (n=250 → 1000)
main 3.99 3.98
before #8551 (the quadratic code these tests were written for) 15.67 15.06
stand-in: document ids collected again for every script (SVG draw-on rule) 9.01 under 6
stand-in: source positions found by rescanning the prefix for every finding 3.98 12.95

Two runs of the same page differ by less than 0.1%. In the same before-#8551 tree, the old wall-clock measurement read 16.06 for the script-tags page.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

fails without the fix: this PR changes tests only. The regressions they guard against were checked by making lint quadratic: the stand-in that re-collects document ids for every script fails lints script tags naming one file in linear time at 9.01 against 6. The lint code from before #8551 grows 15.67 and 15.06 on the two pages.

On main both tests pass three runs in a row. They also passed 10 of 10 runs with eight CPU-bound processes running beside them. The full lint suite passes (30 files, 1,359 tests) and lint tsc is clean.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 · Keep coverage of the source-position regression independent of its string API. The counter in packages/lint/src/rules/gsap.test.ts:3299–3305 misses direct string indexing. In a test-only mutation of the source-position helper, a per-finding prefix rescan visited 16.06× as many code units on the larger timeline fixture, while the current counter reported 4.12× and both tests passed. The slice-and-split version of the same regression is caught. Please keep a deterministic assertion that guards the source-position traversal itself so an equivalent quadratic implementation also fails.

I verified the original CI failure in the named script-tag test, at 8.92 against 8. The build, lint typecheck, all 1,359 lint tests, and ten contention runs passed on this head.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Addressed at 48f0a617. You're right: the method counter can't see a scan done by plain indexing.

packages/lint/src/sourceCoordinates.test.ts now guards the source-position traversal itself. It passes sourcePositions a proxied String that charges every read, whatever API makes it:

  • an index, or charCodeAt, charAt, codePointAt or at, costs one code unit;
  • any other method, or turning the source back into a plain string, costs the whole length.

reads the source a number of times that grows with its length, not with the positions asked for asks for a position on every line, at 1,000 and then 4,000 lines, and bounds the growth in reads at 6. A second test checks that lines and columns through the charged source match the plain one.

Stand-ins, each a per-lookup prefix rescan inside the helper:

Rescan done with sourceCoordinates.test.ts gsap.test.ts work counter
plain indexing (source[i], your mutation) fails at 15.98 passes
slice + split fails at 15.98 fails at 11.98 (timeline)
lastIndexOf fails at 15.98 passes

On the current code, all 7 tests in these two files pass, three runs in a row. The work counter in gsap.test.ts stays: it catches a caller that rebuilds the positions for every finding, which a test of the helper alone can't see.

Analyze (python): on c35b5650 it failed in "Perform CodeQL Analysis" with no error line in the log. This PR changes only TypeScript tests, and CodeQL on main passed for every recent head. The push re-runs it, and I'll look again if it repeats.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The source-position test now closes the direct-indexing coverage gap. It measured 4.00× growth on this head and rejected the quadratic rescan at 16.01× against the 6× bound. The named original timing test failed in CI before the change. All 1,361 lint tests passed, along with typecheck, lint, and formatting.

— tai (automated review)

This branch has not been deployed

No deployments
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.

2 participants