Skip to content

Commit b530152

Browse files
test(ui): assert file-label lookup cost stays linear
Add a deterministic performance-contract regression for shortestFileLabels: count Map#get calls via vi.spyOn while the trie runs at two path-count scales and assert the growth stays linear (generous band) rather than the pre-fix O(n^2) full-list rescan's quadratic signature. No wall-clock timing, so it can't flake under CI load; verified locally that it fails deterministically against the pre-fix implementation and passes clean against the fix.
1 parent c985d53 commit b530152

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎ui/src/components/markdown-links.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Control UI tests cover markdown link rendering: autolinking, file links, and link marks.
2-
import { describe, expect, it } from "vitest";
2+
import { describe, expect, it, vi } from "vitest";
33
import { shortestFileLabels } from "./file-kind.ts";
44
import { toSanitizedMarkdownHtml } from "./markdown.ts";
55

@@ -450,6 +450,38 @@ describe("toSanitizedMarkdownHtml links", () => {
450450
}
451451
});
452452

453+
it("keeps per-path lookup cost linear as path count grows (performance contract)", () => {
454+
// Wall-clock timing flakes under CI load, so this asserts the actual
455+
// performance contract structurally: count every Map#get call made while
456+
// shortestFileLabels runs. The trie makes a fixed number of child
457+
// lookups per path segment (one per segment on insert, one per resolved
458+
// suffix depth on lookup), so total lookups scale with path count, not
459+
// its square. The pre-fix full-list rescan (#124230) re-read every other
460+
// path's segments inside `unique.some(...)` at every depth, which cost
461+
// O(n^2) lookups for this same all-unique-basename shape -- 8x the paths
462+
// there costs ~64x the lookups, far outside the linear band asserted
463+
// below, so a regression back to that scan fails this test every run.
464+
const countMapLookups = (pathCount: number): number => {
465+
const paths = Array.from(
466+
{ length: pathCount },
467+
(_, i) => `src/pkg${i % 50}/mod${i}/file${i}.ts`,
468+
);
469+
const getSpy = vi.spyOn(Map.prototype, "get");
470+
try {
471+
shortestFileLabels(paths);
472+
return getSpy.mock.calls.length;
473+
} finally {
474+
getSpy.mockRestore();
475+
}
476+
};
477+
478+
const small = countMapLookups(500);
479+
const large = countMapLookups(4000); // 8x the paths
480+
481+
expect(large).toBeGreaterThan(small * 4);
482+
expect(large).toBeLessThan(small * 16);
483+
});
484+
453485
it.each([
454486
["README.md", "markdown"],
455487
["package.json", "package"],

0 commit comments

Comments
 (0)