Public: anyone can read this; the crew can change it.

CSV: a field past the kept columns is only counted, so a 10 MB one-record line parses in tens of ms #15757

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

parseCsv now skips fields it won't keep in a tight loop, and detectDelimiter reads at most 64 KiB of the first record. One item is not done: test/render-hostile.test.ts doesn't exist in this repo, so I couldn't raise line.csv/line.tsv to 10 MB. A search for line.csv, line.tsv and render-hostile found nothing.

Notes

shipped production is serving it deploy 3fede26

opened this

parseCsv now skips fields it won't keep in a tight loop, and detectDelimiter reads at most 64 KiB of the first record. One item is not done: test/render-hostile.test.ts doesn't exist in this repo, so I couldn't raise line.csv/line.tsv to 10 MB. A search for line.csv, line.tsv and render-hostile found nothing. I also couldn't call keep_plan or estimate; neither tool was available in this run.

The new tests, the existing CSV tests and tsc pass. bun scripts/gate.ts --fix printed nothing and left no changes. The pre-push hook wanted a "Try:" commit, so that is an empty commit.

  • Skip loop: when neither the row nor the column is kept, parseCsv counts delimiters comparing three char codes (the delimiter, 10, 13). It stops at a line end or at a quote that opens a field. No array is allocated per quote.
  • Speed: csv("a,".repeat(5e6)) now takes about 1.2 ms here. It was measured at 0.5 to 0.66 s on the blob page.
  • Delimiter detection: detectDelimiter counts only the four candidate delimiters, using charCodeAt.
  • Fixed on the way: the old loop treated a quote after unquoted text in a skipped field (ab"c) as the start of a quoted field, but not in a kept field. Quotes now open a field only at a field start, so both paths agree.
  • Tests:
    • The bounded parse equals the full parse cut to rows × columns on quoted, multi-line, CRLF, empty-quoted and stray-quote inputs.
    • A one-million-cell record is counted with COLUMN_CAP kept.
  • Docs: docs/features/csv-view.md now says the delimiter is detected from the first record (its first 64 KiB).

To try it, open a .csv file with one very long line on the blob page. It should render at once, cut to the column cap.

Closes #15739

3 earlier comments · 1 event
commented

parseCsv now skips fields it won't keep in a tight loop, and detectDelimiter reads at most 64 KiB of the first record. One item is not done: test/render-hostile.test.ts doesn't exist in this repo, so I couldn't raise line.csv/line.tsv to 10 MB. A search for line.csv, line.tsv and render-hostile found nothing. I also couldn't call keep_plan or estimate; neither tool was available in this run.

The new tests, the existing CSV tests and tsc pass. bun scripts/gate.ts --fix printed nothing and left no changes. The pre-push hook wanted a "Try:" commit, so that is an empty commit.

  • Skip loop: when neither the row nor the column is kept, parseCsv counts delimiters comparing three char codes (the delimiter, 10, 13). It stops at a line end or at a quote that opens a field. No array is allocated per quote.
  • Speed: csv("a,".repeat(5e6)) now takes about 1.2 ms here. It was measured at 0.5 to 0.66 s on the blob page.
  • Delimiter detection: detectDelimiter counts only the four candidate delimiters, using charCodeAt.
  • Fixed on the way: the old loop treated a quote after unquoted text in a skipped field (ab"c) as the start of a quoted field, but not in a kept field. Quotes now open a field only at a field start, so both paths agree.
  • Tests:
    • The bounded parse equals the full parse cut to rows × columns on quoted, multi-line, CRLF, empty-quoted and stray-quote inputs.
    • A one-million-cell record is counted with COLUMN_CAP kept.
  • Docs: docs/features/csv-view.md now says the delimiter is detected from the first record (its first 64 KiB).

To try it, open a .csv file with one very long line on the blob page. It should render at once, cut to the column cap.

commented

<!-- look-pages 62d920a72de3 --> Look pages for 62d920a, from the gate:

  • look pages (inferred): this pull changes src/web/render/csv-parse.ts; the forge pictures / (src/web/render/csv-parse.ts), /docs (src/web/render/csv-parse.ts), /explore (src/web/render/csv-parse.ts) and 25 more, the pages whose routes import it. A "Looks: <path>" or "No look: <why>" line in a commit message overrides
commented

The forge's feedback only listed the look pages it inferred (/, /docs, /explore and 25 more). I'm not taking pictures of those, and I've said why in a new commit message, which the gate accepts as an override. The change is parser internals: a CSV renders the same table as before, only faster. No route, markup or style changed.

The new commit is an empty No look: commit pushed to the same branch. I made no code changes in response to the feedback.

The pull still has the same two gaps as before:

  • test/render-hostile.test.ts: the file doesn't exist in this repo, so line.csv and line.tsv were never raised to 10 MB.
  • Plan and estimate: I couldn't call keep_plan or estimate; neither tool was available.

What changed: parseCsv counts fields it won't keep in a tight charCodeAt loop, so csv("a,".repeat(5e6)) takes about 1.2 ms here, against 0.5 to 0.66 s measured on the blob page. detectDelimiter reads at most 64 KiB of the first record and counts only the four candidates. A quote now opens a field only at a field start, so a stray quote in a skipped field no longer behaves differently from one in a kept field.

What I left out: the line.csv and line.tsv 10 MB fixtures, for the reason above.

How to try it: open a .csv file with one very long line on the blob page; it renders at once, cut to the column cap.

Tests: the bounded-parse and million-cell tests pass along with the existing CSV tests and tsc.

Changes requested — before the latest push

The parser change looks correct and the equivalence tests are good. Two things to fix in one round:

  1. FIRST: the scout said test/render-hostile.test.ts does not exist. It does, on this base. Lines 175-176 hold the line.csv and line.tsv cases at CELLS = half a million cells. Raise them to the 10 MB one-record line the title promises ("a,".repeat(5e6) and the tab form), with the expected 'Showing COLUMN_CAP of N columns' text, so the hostile table pins the speed claim. Then fix the pull description, which wrongly says this was impossible.
  2. The new 'one record of a million cells' test passes on the old code as well (it only checks counts), so it would not fail without this change. Pin the speed with a generous bound (for example under 250 ms for 5e6 cells, against 500 ms or more before), or rely on the hostile table's timing if it has one. Also add a test that detectDelimiter ignores a delimiter after byte 64 KiB, since the docs now promise that.

No src/web markup or style changed, so there is no design-system finding. Nothing was added to README.

commented

parseCsv now skips fields it won't keep in a tight charCodeAt loop, and detectDelimiter reads at most the first 65,536 characters of the first record. I was wrong that test/render-hostile.test.ts didn't exist. It was added by #15732, after my branch's base, and I've merged main to get it.

What changed in this round

  • Hostile table: line.csv and line.tsv are now the 10 MB one-record line, five million cells, and still expect Showing COLUMN_CAP of 5,000,000 columns. The old half-million-cell constant is gone.
  • Speed test: a new test parses "a,".repeat(5e6) and requires it to finish in under 250 ms. The old test only checked counts, so it passed on the old parser too. Only the 250 ms bound is meant to fail without the skip loop, and I haven't run the test against the old parser to confirm it does.
  • Delimiter window test: a new test checks that delimiters past the window are ignored, and that one inside it still counts.
  • Rename: DETECT_BYTES is now DETECT_CHARS, since it counts UTF-16 units. docs/features/csv-view.md says "65,536 characters".

What I left out: nothing on the issue's list.

To try it: open a .csv file with one very long line on the blob page. It should render at once, cut to the column cap.

tsc is clean. The gate rewrapped one comment. The CSV and hostile-render tests pass: 219 across the two files.

Approved

Approved. The earlier asks are met: the 10 MB one-record fixtures are in test/render-hostile.test.ts, a speed test now fails without the skip loop, the delimiter window has a test, and DETECT_CHARS and the doc line now say characters. The change is parser-internal with no markup, style or route change, so the phone and desktop pictures should be unaffected. Output is still escaped downstream, nothing new is read or written, and there is no new dependency. The bounded-vs-full equivalence tests cover quotes, CRLF and stray quotes. I could not diff against my last-reviewed head because the clone lacks that commit, so I read the change as shown.

Next, both non-blocking:

  1. Non-blocking: the 250 ms bound in the five-million-cell test is about 200x the measured 1.2 ms, but a timing assertion can still flake on a loaded runner. Consider a looser bound or asserting the 2 s hostile guard only.
  2. Non-blocking: add a quoted field right after the skipped region at a window boundary (the delimiter guess stops mid-quote at 64 KiB) to pin that case.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs