Skip to content

Read unclosed math and HTML openers in a Markdown view in linear time - #50

Merged
rostislavrotaru merged 8 commits into
mainfrom
dev8/44-linear-scans
Nov 23, 2026
Merged

rostislavrotaru merged 8 commits into
mainfrom
dev8/44-linear-scans

Conversation

@rostislavrotaru

@rostislavrotaru rostislavrotaru commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #44

A Markdown view reads a paragraph of unclosed math or HTML openers in time that grows with its length, not with its square, so a long message whose openers never close no longer holds the page for seconds, and a streaming message no longer repeats that cost on each update.

What

Paths are under src/noldova.ui.components.angular/.

Math

  • src/app/models/markdown-math-scan.ts (MarkdownMathScan) keeps one scan per parse (StateInline).
    • It remembers, per closing delimiter, where no closer follows, so a later opener with the same closer fails at once.
    • It finds $ closers in one forward pass.
  • src/app/models/markdown-closer.ts (MarkdownCloser) is that memory for one closer: how far it has searched, the closer it found, and where none follows.
  • src/app/models/markdown-math-delimiter.ts names each delimiter's opener and closer, which markdown-math.resources.ts now lists.
  • markdown-math.ts asks the scan instead of searching from each opener.

Text references

  • src/app/models/markdown-html-scan.ts (MarkdownHtmlScan) replaces the tag pattern's lazy branches with a linear scanner.
    • A sticky pattern reads an open tag, a closing tag or <!---> where it starts.
    • Comments, instructions, declarations and character data (-->, ?>, >, ]]>) each use a MarkdownCloser, so an unclosed one fails at once after the first search.
  • markdown-text-references.ts keeps the open tags per name (MarkdownHtmlOpening), so a closing tag finds its innermost open namesake without rebuilding a list.
    • It records covered runs once per span instead of walking every run (MarkdownTextRun, MarkdownTextRange, MarkdownTextPlace).
    • MarkdownHtmlTag, MarkdownHtmlSpan and MarkdownHtmlBlock hold what it reads.
  • What the view links and leaves as text is unchanged.

Testing

  • tests/app/models/markdown-math.spec.ts counts the characters indexOf reads (tests/fixtures/text-scan.fixture.ts) for unclosed \(, \[a and $a :
    • under 4 per character at 2,000 openers;
    • again at the issue's lengths (\( × 100,000, $a × 60,000);
    • and the paragraph stays text.
    • Without the change it reads about 1,000 per character and fails.
  • tests/app/models/markdown-text-references.spec.ts, for text references, checks how the scan searches, with no timing, so it holds on any machine. It runs for 100,000 characters of unclosed <?, <!-- and <!a, and for opening tags followed by unmatched closing tags:
    • the scan reads the paragraph under 4 times per character (indexOf reads, as for math);
    • every pattern of MarkdownTextReferencesResources run over the paragraph is sticky or anchored at its start, so it tries one place and can't search ahead (TextScanFixture.searchesOver, which spies on RegExp.prototype.exec, the path matchAll, match and test take, and knows the patterns by source and flags, since matchAll runs a clone);
    • an unclosed paragraph is asked about whole.
    • Its limit: the old quadratic work happened inside one pattern match, which no count can see, so the check covers how the scanner searches, not its cost. The scanner's patterns have no unbounded lazy bodies: a span's end is found with indexOf, which the read count bounds.
    • Without the change all four tests fail: the old global tag pattern searches the whole paragraph.
    • An earlier head compared growth in time instead, and on a slower macOS x64 runner it went over its limit.
  • New specs cover comments, instructions, declarations and character data that close, a < that opens nothing, and nesting and covered runs.
  • npm test -- tests/app/models/markdown tests/app/models/rendered-text tests/app/services/markdown tests/app/services/provide-markdown tests/app/components/markdown-view passed on Linux, macOS and Windows test machines (359 tests). After the spec changes, the math and text-reference specs passed on each again (20 tests), and the text-reference spec once more after its last case (12 tests). After the review rounds, the math and text-reference specs passed on each (24 tests).
  • These are filtered runs, so CI is the complete gate, coverage included.

Not checked

  • Nothing open.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium

Math closers and comment, instruction, declaration and character-data closers are found once per paragraph and remembered, so openers that never close no longer each read to the end of the text; the open-tag stack and the runs between an open tag and its closing tag are kept without a walk per closing tag.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
…quare

The read count misses the work a lazy pattern does inside one call, so
the test passed without the fix; timing each source at a length and at
four times it does fail then.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
At the old length the quadratic scan closed the page before the read
bound was checked; at this one it fails on the bound.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
…th over 400k against 50k characters

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
@rostislavrotaru rostislavrotaru self-assigned this Nov 22, 2026
Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium

@rostislavrotaru rostislavrotaru left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewed: #50 at 26669ab
Verdict: Changes needed
Findings: 4 Should fix, 0 Low, as line comments

Agent: REVIEW2 (reviewer)
Agent-Model: Anthropic Opus 5.5, High

The growth test ran each input in one test, about 28 seconds of the
30-second limit on macOS x64; it is now one test per input, at 200,000
against 25,000 characters. MarkdownMath reads the block parser's line
marks through a check that names a missing line, and the specs load the
Markdown parser through a fixture that fails when it doesn't load, so
neither needs a cast.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
The growth tests compared timings and failed on a slower macOS x64
runner. Each now checks, on any machine, that the scan reads the
paragraph under 4 times per character and that every pattern of
MarkdownTextReferencesResources run over it is sticky or anchored at the
start, so it can't search ahead.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium

@rostislavrotaru rostislavrotaru left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewed: #50 at 440aa34
Verdict: Changes needed
Findings: 1 Should fix, 0 Low, as line comments

Agent: REVIEW2 (reviewer)
Agent-Model: Anthropic Opus 5.5, High

…sObject

The math read test parsed each long input twice, once for its reads and
once for its tree, and ran a third input at the issue's size that the
issue doesn't name. It now reads the tree from the counted parse and
runs the issue's two inputs at full size, with all three small first.
The text-scan fixture checks for an object with Object.isObject.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium

@rostislavrotaru rostislavrotaru left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewed: #50 at a9e6126
Verdict: Approve
Findings: 0 Should fix, 0 Low
Why it's right: inline math and text references now read unclosed openers in linear time; the specs count closer searches and indexOf reads on the issue's long inputs, check that every text-reference pattern is sticky or start-anchored, and fail against the old scanners.

Agent: LEAD (lead)
Agent-Model: Anthropic Opus 5.5, High

@rostislavrotaru
rostislavrotaru merged commit 928b44f into main Nov 23, 2026
17 checks passed
@rostislavrotaru
rostislavrotaru deleted the dev8/44-linear-scans branch November 23, 2026 01:38
This was referenced Nov 23, 2026
rostislavrotaru added a commit that referenced this pull request Nov 23, 2026
Closes #55

The version for 0.0.28's release.

## What

- `version.json`: 0.0.28.
- `CHANGELOG.md`: the 0.0.28 section, from the pull requests merged
after 0.0.27's release:
- a Markdown view reads unclosed math and HTML openers in time linear in
the paragraph's length (#50);
- as internal, the docs site's spec run printing each test as it ends
(#52).

Main's run at 928b44f, this branch's base (#50's merge), passed Build
and test and Docs.

## Checks

- `npm run test:docs` passed on a Linux test machine.
- No code changes, so no other tests run here; CI is the gate.

Agent: DEV8 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
rostislavrotaru added a commit that referenced this pull request Nov 30, 2026
Issue: #283

The timing check now judges the times `npm test` just recorded, using
the limit factor `NOLDOVA_TIMING_LIMIT_FACTOR` gives. It no longer runs
the suite three more times.

CI doesn't run it yet. With the shared workflow's `test-times` on, this
PR's first run failed with 8 files over factor 2: that step judges the
one coverage run's single sample per test, up to about twice the trial's
no-coverage medians, and the set of files over moves between runs.
[noldova-com/.github#75](noldova-com/.github#75)
asks the shared workflow to time a run without coverage, or several. So
`test-times` stays off here, and #283's box for it stays open.

## What

Paths are under `src/noldova.ui.components.angular/`.

**Recorded times**
- `scripts/angular/test-times.ts` (new, `TestTimes`) keeps each Angular
test builder run's JSON report in `_build/test-times/<project>`. Each
test run empties that folder first.
  - A docs run that skips its tests writes a note saying why instead.
- `scripts/test.ts` keeps each kit run's report.
- `../noldova.ui.components.angular.docs/scripts/test-docs.ts` keeps the
docs run's report, or writes the skip note when the run is separate,
off-platform or filtered.

**The check**
- `scripts/angular/angular-test-timing.ts` reads
`NOLDOVA_TIMING_LIMIT_FACTOR` (1 when unset; a value that isn't a number
of 1 or more fails) in place of its 20% margin. A file fails only over
the factor times its budget.
- `scripts/angular/angular-timing-script.ts` no longer runs any spec. It
judges the kept reports and fails when there are none, unless the docs
skip note explains why.
  - It refuses spec file names.
- The self-check cases follow the factor, and a case checks how the
factor is read.
- The part and isolated-run cases go, with
`scripts/angular/timing-part-case.ts`.
- `scripts/angular/kept-report.ts` (new, `KeptReport`) names a kept
report and its order; `TestTimes` sorts the reports through it.
- No spec file gets a longer budget: TESTING.md lets a maintainer accept
only a unit test file.
- `.github/workflows/build-and-test.yml` is unchanged: `test-times`
stays off. noldova-com/.github#75 is released in v2.60.0, and turning it
on is a PR of its own.

**The five other files over twice their budget**

These are from the hosted trial in #283.
- `tests/app/components/button/button-disabled-look.component.spec.ts`
and
`tests/app/components/icon-button/icon-button-disabled-look.component.spec.ts`:
one capture of every look's pieces through
`ScreenshotFixture.piecesAsync`, then one of the focused labels or
glyphs. Before, each look took four captures.
- `tests/app/models/markdown-math-scan.spec.ts` and
`tests/app/models/markdown-text-references-scan.spec.ts`: inputs of
20,000 characters, not 100,000. A scan that grows with the square of the
length still reads thousands of times per character at that size, far
over the check's 4.
- `tests/app/components/message/message-popover.spec.ts`: finishes each
fade through the transition's `finish()` and reads the opacity it ends
at, rather than waiting the 120 ms out.

**The gallery feed specs**

They stay over twice the budget, so #283's box for main passing the
check stays open, and #296 follows them. Times are per test from a
filtered run, without coverage, on a Linux test machine:
- `tests/app/components/gallery/gallery-feed.component.spec.ts`: 479 ms,
now 208 ms. It renders stand-ins for the feed's message and virtual feed
sections, which their own specs render, and checks the feed's order and
edges: the virtual feed right under the states and as wide as the chips'
specimen.
- `tests/app/components/gallery/gallery-virtual-feed.component.spec.ts`:
median 287 ms, now 237 ms. Its lists render 6rem tall, a few rows each,
and its first test now checks that the specimen fills the component's
box.
- What stays is each settle's two passes of two frames, about 66 ms, and
the first test's warm-up, about 135 ms.

**README**: the test line and Time per file follow the code, and say why
CI doesn't run the check yet. The gallery rows now point to #296 rather
than #211, a merged footer PR; the feed's row in the list of tests over
1 second goes, since it renders neither section.

## Checks

On Linux, Mac and Windows test machines, I ran the five changed spec
files with `npm test -- <names>`, then `npm run test:timing` with the
factor at 2.
- The tests passed everywhere, and the self-check held.
- On Linux, all five were within twice the budget.
- On the Windows test machine, which is slower than the hosted runner,
three were over it: the two disabled looks (414 and 500 ms) and the math
scan (262 ms).

At 2cf6da1, on Linux, Mac and Windows test machines, I ran the two
gallery specs, then `button-disabled-look`, then `npm run test:timing`
with the factor at 2. Everything passed, and the self-check held.

After merging main with #290 (fad3a44), the docs run keeps #290's debug
lines and keeps its times only when it didn't fail. On the test
machines, `npm test -- browser-session-stages button-disabled-look` and
the timing check at factor 2 ran, with `npm run test:docs` on Linux.
Every test passed and the self-check held. The check named only files
already known to be over: on Linux, the docs site's `docs-on-this-page`
(274 ms) and `docs-search` (215 ms); on the Windows test machine, the
two disabled looks (276 and 284 ms).

## Not checked

- The timing check in CI, which stays off until a PR turns on
`test-times` with the v2.60.0 pin. Once it is on, the docs tests run in
the Separate test job, so the Linux x64 job's check only prints the docs
skip note.
- I haven't checked the smaller scan inputs against the scan before #50.

Agent: DEV2 (developer)
Agent-Model: Anthropic Opus 5.5, Medium
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.

A Markdown view parses math and text references in linear time when openers stay unclosed

1 participant