From e12249e593169d5c183bc6cae639e7914b35d51b Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 18 Oct 2026 10:41:02 +0200 Subject: [PATCH] Read and write unclosed backtick runs and HTML openers in time linear in the text --- CHANGELOG.md | 3 ++ docs/reading-cost.md | 17 ++++++++ src/markdown/commonmark/backtick-runs.test.ts | 24 +++++++++++ src/markdown/commonmark/backtick-runs.ts | 30 ++++++++++++++ src/markdown/commonmark/grammar.ts | 5 ++- src/markdown/emit/line-escaping.ts | 14 ++++--- src/markdown/inline-tokens.ts | 41 +++++++++++++++---- src/markdown/parse/inline-content.ts | 5 ++- todo.md | 34 +++++++-------- 9 files changed, 138 insertions(+), 35 deletions(-) create mode 100644 src/markdown/commonmark/backtick-runs.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b981785..f632ee8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ table cell: `[alt](href) ([image](url))`, where it wrote `[alt](url)` and dropped the link's target. - `losslessMarkdownToAdf` names the CommonMark spelling to write when it refuses a directive whose node CommonMark spells, such as `[![alt](url)](href)` for a linked `!adf:mediaSingle`. +- Every reader and writer takes time linear in a text holding many unclosed backtick runs, or many + `` is quadratic, in the reader and the emitter's escape walk alike. -commonmark.js avoids the backtick cost by caching backtick runs by length. - ### 98. Read blank lines inside deeply nested list items in time linear in the markdown. `matchContainers` in `src/markdown/parse/blocks.ts` continues every open item holding a block on @@ -136,11 +128,11 @@ parts by ADF and format true as they grow. ### 92. Lower the reading cost of the files a chunk reads most before its first commit. The last five chunks' median is 113495 new input tokens before their first commit, the five before -them 96564. Files read most over the last five chunks, counting opens and searches: `README.md` 38, -`todo.md` 36, `spec/flavour.md` 22, `docs/decisions.md` 20, -`src/markdown/parse/markdown-to-adf.test.ts` 18, `src/markdown/emit/adf-to-markdown.test.ts` 14, -`CHANGELOG.md` 12, `src/markdown/directive-syntax.ts` 10, `src/conformance/adf-schema.test.ts` 8, -`src/markdown/parse/inline-content.ts` 7. +them 108874. Files read most over the last five chunks, counting opens and searches: `README.md` 39, +`todo.md` 36, `docs/decisions.md` 20, `src/markdown/parse/markdown-to-adf.test.ts` 18, +`CHANGELOG.md` 14, `spec/flavour.md` 13, `src/markdown/emit/adf-to-markdown.test.ts` 8, +`src/markdown/parse/inline-content.ts` 7, `MIGRATION.md` 6, +`src/conformance/commonmark-spec.test.ts` 6. ### 102. Place a block a portable task list refuses at its path in the returned document without replaying `splitTaskList` on stand-in nodes. @@ -175,6 +167,16 @@ posting, set `converter-comparison/run.sh`'s pins to each package's latest versi its versions and its date in step with the output. We draft each post for the place it goes; the maintainer posts them once `0.3.0` is published. Found while planning discoverability, 2026-10-04. +### 119. Read a directive's content holding many unclosed backtick runs in time linear in the text. + +`readCodeSpanEnd` (`src/markdown/directive-syntax.ts`) calls `closingBacktickRun`, which searches +the rest of the text at every opener, so backtick runs of lengths 1 to k with no closers inside an +inline directive's `[content]` cost about k·n. Measured on 2026-10-18: 24 ms at 40k characters, +126 ms at 160k and 1004 ms at 640k, where the same runs outside a directive take 6, 18 and 63 ms. +The inline reader reads such runs through `backtickRuns` and `closingRun` +(`src/markdown/commonmark/backtick-runs.ts`), kept once per text; the directive reader would need +them kept for the source it reads from. + ### 81. Fail the gate unless the README's error tables list exactly the codes `ConvertErrorCode` holds. `src/result.test.ts` checks the union against the call sites only, so a code added or removed