Skip to main content
Lightning Jar - Web Studio Lightning Jar Wordmark

Findings No. 108

A per-heading scan of every section made empty headings quadratic

Barmark's error message lookup rescanned every section per heading, making empty headings parse quadratically; normalizing names once and caching the next same name section fixed it.

Barmark Tooling verified

A loop that rescans every section for each heading in a chart turns linear work quadratic, however small the thing it is computing. In Barmark, unmatchedReplay ran sections.find(...) across every section for each heading that played no bars, renormalizing each section's name again just to choose one of two wordings for an error message. A CPU profile put 86% of the time in normalize. On a chart of 8,000 such headings, parseChart took about 2.3 seconds, and 8,000 of them followed by 8,000 one row sections took 10 to 13 seconds across two machines. A chart is user input, so a file of a few hundred kilobytes could hold a server, and every scale test had passed because they timed valid charts, not this error path.

The fix normalizes each section's name once, as sections are built (Section.key), and builds, in one pass from the end of the chart, each section's nearest later section of the same name (writtenLater), so a message just looks it up. 4,000 empty headings now take about 4 times as long as 1,000, down from 13 to 19 times before. Review found two more of the same shape, in isReplay and in how replays walked lines that play nothing, and both are fixed the same way, with no change to any chart's output.

The general rule: a scan of the whole document inside a loop over the document is quadratic no matter how rare the case it serves, so build the lookup once per parse, and scale tests must time erroring input, not only valid charts, since a hostile file is mostly errors.

Receipt: Barmark (packages/barmark/src/chart/expand.ts: writtenLater, unmatchedReplay; direction.ts: isReplay; tests/chart/headings-scale.test.ts).

Ask Eljay

Ask about the studio's work, research, packages, or writing. A few starters:

Answers come from this site's own content and link their sources. For anything that matters, email hello@lightningjar.com.