Repository navigation
fix(csv): a row ends at LF, CRLF, a lone CR or LFCR, with quotes in the file or without - #1895
Conversation
3bb3cba to
0fa8911
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of 0fa8911: NEEDS CHANGES
The rule is implemented right on every read path, and the vector search is sound. Two things are off: the strict line count for one line-end mix, and the BREAKING CHANGE trailer, which misses a change.
Findings
-
Strict line numbers miss a lone CR that follows a CRLF (
csv_line_of,src/io/csv.c).csv_line_ofcounts every LF, plus every CR that has an LF on neither side. Under the new rule,\r\n\ris a CRLF and then a lone CR: two line ends. The second CR, though, has an LF before it (the CRLF's), so it is taken as the tail of an LFCR and not counted.- The rows are right; the reported line is one short. With
a\r\n\rx\nread{strict: true} [I64], the rows are["" "x"], soxis on line 3, but the error saysline 2, column a: "x" is not an I64. - The other mixes I tried report the right line:
\n\r\n,\r\r\n,\n\n\r,\r\n\r\n. - Fix: count line ends with the same walk the scans use,
csv_eol_lenfrom each LF or CR, so a byte already consumed as half of a pair is skipped. Adda\r\n\rxtocsv_line_ends.rflor the strict matrix.
-
The BREAKING CHANGE trailer covers quote-free files and LFCR headers, but rows also change in quoted files.
- Under dev's quoted path, LF then CR was two line ends. Every LFCR data row in a file with quotes was therefore followed by a spurious empty, null row. Now it is one line end. Examples, read
[I64 STR]:a,b\n\r1,"x"\n\r2,y\n\r: columnais[0Nl 1 0Nl 2 0Nl]on dev and[1 2]here. The leading null is the LFCR header the trailer names; the other two nulls are not covered.a,b\n1,"x"\n\n\r2,y\n:[1 0Nl 0Nl 2]on dev,[1 0Nl 2]here.
- In a quote-free file, a CRLF row followed by a lone-CR line,
a,b\r\n1,x\r\n\r2,y\r\n, reads[1 2]on dev and[1 0Nl 2]here: the blank line is now read. The trailer's "lines end in a lone CR" covers that one. - Fix: add a sentence to the trailer, e.g. "in a file with quotes, an LF followed by a CR now ends one row, not two; before, each such line end added an empty row".
- Under dev's quoted path, LF then CR was two line ends. Every LFCR data row in a file with quotes was therefore followed by a spurious empty, null row. Now it is one line end. Examples, read
Observation (not blocking)
At 28 workers, a quoted 276 MB file (6M rows, I64 F64 SYMBOL quoted STR DATE) reads 3–4% slower than dev, consistently across 12 interleaved rounds:
- minimum 82.3 against 79.3 ms;
- the PR's runs 82.3–87.9 ms, dev's 79.3–81.4 ms;
- load average 3.6–8.5.
Everything else is level with dev:
- the quoted file pinned at one worker: 823.2 against 828.7 ms;
- the quote-free file pinned: 654.4 against 652.8 ms;
- the quote-free file at 28 workers: 69.9 against 70.1 ms.
#1891 notes this load's sensitivity to code alignment, so this may be layout. It is worth one re-measure before merge.
What I checked
The vector search (csv_find_eol). I extracted it and built it as AVX2, SSE2 and scalar; each agrees with a bytewise search on 6.56M ranges per form.
- The ranges: every start 0–79 with every length 0–79, at the start of a buffer and ending at its last byte, plus 4M random ranges over random and line-end-free contents.
- Guard pages: the buffer is 4 pages with a
PROT_NONEpage on either side. There was no fault, and ASan+UBSan builds of all three forms were clean. - Callers: every caller scans a
ray_vm_map_fd_robuffer (mmapon POSIX, a 64 KB-aligned view on Windows). So an aligned 32-byte block never leaves the mapping, and the bytes it reads beforepand pastendare in the file's own pages.
The rule, against a reference implementation of it, written in Python with the csv_eol_len semantics.
- The files: 400 random files read headerless
[STR STR]and written back with.csv.write, then parsed with Python's csv module.- Each of LF, CRLF, lone CR and LFCR, alone and mixed.
- Blank lines, and a final line with and without its line end.
- Quoted cells holding commas, doubled quotes, CR, LF and CRLF; half the files quote-free.
- The parallel scan: 8 files of 600,000 rows each (over 4 MB): LFCR-only, CR-only and mixed, with and without quotes.
- Line-end-only files: 20 files made only of line-end pairs (
\n,\r\n,\r,\n\r,\n\r\n, mixes), at 3 lines and at over 5 MB, so the parallel reader's chunk boundaries fall on pairs. - Result: every one matches at 1 and 28 workers; dev differs on 261 of the 400.
Paths. Two 300,000-row mixed-line-end files (quote-free and quoted), at 1 and 28 workers:
- inferred types (header detection and sampling), strict,
[I64 STR], and names plus types (header matched and skipped) all read the same 300,000 rows; .csv.splayedand.csv.parted(rows-per-part 70000) read back equal.
The empty file answers io, and a header-only file reads as an empty table.
Tests and suite.
- Under ASan+UBSan:
csv_line_ends,csv_strict,strict_matrixand the csv tests (109, 1 skipped) pass. - The gcc suite: 5945 of 5949 passed, 4 skipped, 0 failed.
Commit message. It explains the rule, the vector search and its safety argument, and the timings. Its final paragraph is Fixes #1891 followed by the BREAKING CHANGE: footer, which needs the addition in finding 2.
Not covered
- Windows, macOS and other compilers.
- A big-endian host.
- WASM (it has neither AVX2 nor SSE2 here, so it takes the scalar form).
- Cold-disk loads.
- Timing of
.csv.splayedand.csv.parted.
0fa8911 to
225e49c
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of 225e49c: NEEDS CHANGES
The line count is fixed. One more input class reads differently from dev, and neither the trailer nor the docs list it.
Finding
- A quote-free file with LFCR line ends now reads each blank line as two empty rows.
-
In such a file a blank line is
\n\r\n\r. Dev's quote-free path read it as LFCR, LFCR: one empty row. Under the rule, the CR after the first LF starts a CRLF, so it reads as LF, CRLF, then a lone CR: two empty rows. -
Example (inferred read, column
a):…772,24\n\r\n\r440,728\n\r…gives[… 772 0Nl 440 …]on dev and[… 772 0Nl 0Nl 440 …]here. -
The trailer says nothing about quote-free LFCR files; they read the same in every other way. The docs' list of files that read differently does not mention it either.
-
How I found it: 258 random files built to avoid all four listed classes.
- Quote-free files: LF, CRLF and LFCR, never a lone CR.
- Quoted files: LF, CRLF and CR, never an LF directly followed by a CR outside a cell.
- No header ends in CR or LFCR. The files include parallel-sized ones of 400,000 rows.
Each was read typed, inferred and strict at 28 workers on dev (283f08f) and here. 29 files differ, and they are exactly the quote-free files containing
\n\r\n\r. No other file differs. -
The sequence is ambiguous: a CRLF file with an LF blank line, or an LFCR file with a blank line. The rule's CRLF-first reading is a reasonable choice and matches what quoted files already did. But it is a change for LFCR files.
-
Fix: add it to the trailer and the docs' list, e.g. "(5) in a file without quotes whose lines end in LFCR, a blank line (
\n\r\n\r) now reads as two empty rows, as it already did in a file with quotes (before, one)". Or decide that LFCR files keep one row, if that is the intent.
-
Observation (not blocking)
The quoted 276 MB file (6M rows, I64 F64 SYMBOL quoted STR DATE) at 28 workers is still slower here than dev on this machine. Over 6 interleaved rounds, each the minimum of 7 loads, it was 84.2 against 81.5 ms (+3.3%), with the PR's runs at 84.2–93.2 ms and dev's at 81.5–83.1, at a load average of 1.7–2.4. Pinned at one worker it is level: 821.0 against 823.1 ms. The out-of-line split did not move it here, and the author's machine shows the opposite sign, so I read it as layout-dependent and am not holding the change on it.
What I checked
Diff since 0fa8911. It holds only:
csv_line_of, rewritten to walk withcsv_find_eol/csv_eol_len;- the parallel scan's two loops moved into
csv_scan_chunk_fast/csv_scan_chunk_quoted, eachCSV_NOINLINE, with the bodies unchanged apart from a droppedp = cendbefore abreak, wherepis not read afterwards (CSV_NOINLINEmoved up to be defined first); - the docs paragraph and the trailer;
- the new test.
Strict line numbers. My repro a\r\n\rx\n now reports line 3. I also generated every sequence of one to three line ends from LF, CRLF, CR and LFCR between a header and a bad cell, plain and quoted: 168 cases. All report the line my reference counts.
The four listed classes.
- Every difference from dev in this run falls in class 5 above.
- Last round's 400-file comparison had dev differing on 261 files. Those mixed lone CRs, LFCR headers, quoted LFCR files and LFCR blank lines, all now covered by classes 1–4 plus class 5.
The line counter's cost. A strict error on the last row of the 6M-row quoted file answers in 72.6 ms at 28 workers and 491 ms at one. A successful strict read of the same file takes 89 / 534 ms, so the walk is cheap. The message names line 6000002, which is right.
Tests. csv_line_ends, csv/strict_line_ends and the csv tests (110, 1 skipped) pass.
Not covered
- ASan and a full suite this round.
- Windows, macOS and other compilers.
225e49c to
ec4544a
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of ec4544a: NEEDS CHANGES
The greedy rule is implemented right everywhere I looked. One performance cliff remains, in the new chunk-boundary move.
Finding
csv_scan_split_atwalks back to the start of the line-end run at every chunk boundary, so a long run of blank lines costs run × chunks (src/io/csv.c).-
When a boundary falls inside a run of LF/CR bytes, the move first walks back to the start of the run (
while (r > 0 && (buf[r - 1] == '\n' || …)) r--), then re-reads it forward. With a pool of 28 that is 112 chunks, so a run spanning many boundaries is re-read once per boundary. -
Measured, release builds, row counts right in every case:
file dev, 1 worker this dev, 28 workers this a\n1\n+ 400 MB of\n+2\n5.9 s 6.1 s 2.4 s 45.3 s a\n1\n+ 200M ×\n\r+2\n3.5 s 3.5 s 1.8 s 29.7 s -
The parallel read is 12–19× slower than dev, and slower than the serial one. A file with millions of blank lines is unusual, but this is a load of untrusted input that goes superlinear.
-
Why it can be cheap: a position whose byte equals the previous one always starts a line end. A two-byte line end is two different bytes, so
\n\nor\r\rresets the reading. The walk back can therefore stop at the nearest equal adjacent pair, which keeps an LF-only or CR-only run O(1) per boundary. -
The alternating case (
\n\r\n\r…) does depend on parity from the run's start. Options:- bound the walk and fall back to placing the boundary after the run (let the chunk holding the run's start own the whole run);
- or have a worker whose chunk starts inside a run skip forward to the run's end, the previous chunk finishing the run past its
cend. Each byte is then read at most twice.
-
Test: a 64-byte-chunk test with a run of a few thousand line-end bytes, asserting the boundary move's cost (for example a step counter in the test seam), or a timing guard.
-
Observation (not blocking)
The quoted 276 MB file (6M rows) at 28 workers is again slower here than dev: 86.4 against 81.9 ms over 6 interleaved rounds (minimum of 7 each), at a load average of 4.1–6.0. Pinned at one worker this round's runs were noisy, and this head led: 787.7 against 828.3 ms. It is the same layout-dependent gap as last round, and I'm not holding the change on it.
What I checked
The greedy rule against my reference. I wrote a tokenizer from the rule: at an LF or CR, take two bytes if the next one is the other of the pair, else one.
- The files: 306 random files, quoted and plain. Rows end in random runs of 1 to 4 line-end bytes (
\n \r\n \r \n\r \n\n \r\r \n\r\n\r \r\n\r\n \n\r\n \r\n\r \n\n\r \r\r\n), and some quoted cells hold CR, LF and\n\r. They include six files of 700,000 rows, so the parallel reader's real chunk boundaries fall inside runs. - The method: each was read headerless
[STR STR], written with.csv.writeand parsed back with Python's csv module. - Result: every file matches the reference at 1 and at 28 workers.
Against dev (283f08f), at 28 workers. I classified each file with the reference tokenizer (outside quotes) into the docs' four classes: a lone CR in a quote-free file, an LFCR in a quoted file, a header ending in LFCR, a header ending in CR.
- 202 files differ from dev, all in a listed class.
- 66 files are in no class, and none of them differ.
- 38 listed files read the same.
No difference falls outside the listed classes.
Strict line numbers. Every run of 1 to 4 line-end bytes between a header and a bad cell, plain and quoted: 60 cases. All report the line the greedy reading gives.
The boundary move's bounds.
- The backward walk stops at
r > 0; it readsbuf[r - 1]only whiler > 0. - The forward walk advances by
csv_eol_len(1 or 2, bounded bybuf + file_size). It terminates atsors + 1, never past the file. - The function returns early unless both
buf[s - 1]andbuf[s]are line-end bytes.
So it cannot read outside the mapping or loop forever; only its cost is the problem above.
Tests. The line_end tests pass (4, including csv/line_end_reference).
Not covered
- ASan and a full suite this round.
- Windows and macOS.
- Other compilers.
ec4544a to
bd1c42c
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of bd1c42c: NEEDS CHANGES (the quoted-file timing at 28 workers only; the split fix holds)
The split fix is correct and fast. The one thing left is the quoted file at 28 workers, which this time I measured against a dev control.
Finding
- The quoted 276 MB file reads about 5% slower than dev at 28 workers, beyond the control spread.
-
The method: a byte-identical copy of dev's binary as the control; dev, the copy and this head interleaved over 6 rounds, each the minimum of 7 loads. The file is 6M rows of I64, F64, SYMBOL, a quoted STR (
"q,<i>") and DATE. Load average 2.0–5.4.binary 28 workers: min, all runs (ms) 1 worker pinned: min (ms) dev 80.5: 83.3 82.2 82.1 83.2 80.5 83.2 827.9 dev copy (control) 80.3: 81.0 81.9 81.7 80.7 80.3 81.8 824.3 this head 84.5: 84.5 85.9 91.6 88.4 85.7 88.5 791.3 -
At 28 workers: the control spread is 0.2% (80.3 against 80.5). This head is +5.0% on the minimum, and every one of its runs is slower than every dev and control run. It exceeds the spread, so it is a real difference on this machine. It is also the third round I have seen it (+3.3%, +5.5%, now +5.0%).
-
Pinned: this head is 4% faster than both controls (791.3 against 824.3 / 827.9). That is outside the spread too, in the good direction.
-
The two directions suggest a cost in a part that runs per worker or per chunk, rather than per row: the quoted chunk scan or the parity pass.
-
I could not profile here: no
perfon this machine. -
Ask: profile the 28-worker quoted load against dev, and either recover it or record the measured cost and its cause in the commit message. If the author's machine keeps showing it inside its spread, a third machine's run would settle it.
-
What I checked
The parity argument.
- Where the walk stops:
ksteps back fromswhilebuf[k-1]is a line-end byte different frombuf[k]. It therefore stops at one of three points:- a run's first byte, which starts a line end;
- a
kwithbuf[k-1] == buf[k], which starts a line end, because the byte before ends one: a single byte, or the second of a pair, since a pair is two different bytes; lo.
lostarts a line end: by induction. Each resolved boundary is either outside a run, the first byte of a run, or decided by this same parity.bound[0]is the data start.- Between the stop and
s: the bytes alternate, so the greedy reading pairs them fromk. An odds - kmeanss - 1starts a pair coverings, and the first iteration already checkedbuf[s-1] != buf[s]. So the boundary moves one byte, else stays. - No read outside the mapping: it reads only
buf[lo .. s]with0 <= lo < s < file_size. The caller clampssto at least the previous boundary, so the walks are disjoint.
Against my reference, with real chunk sizes. I built six 14 MB files of rows separated by runs of 1–8 bytes. About 2% of the runs are 1 to 300,000 bytes long, mixing long alternating stretches, equal runs and random blocks. At 28 workers a chunk is about 125 KB, so long runs cross several boundaries.
- Each was read headerless
[STR STR]at 1, 2, 4, 8 and 28 workers. - Each was written with
.csv.writeand compared row for row with a greedy tokenizer written from the rule. - All 30 reads match.
The pathological files. 28 workers, release builds, row counts equal to dev:
| file | dev | this |
|---|---|---|
| 400 MB of LF | 2.37 s | 2.25 s |
| 200M LFCR | 1.43 s | 1.46 s |
40,000 runs of \n + 2,500 × \r\n + \r (5,002 alternating bytes each) |
0.61 s | 0.65 s |
The alternating file's 100,040,001 rows are also what the greedy rule gives.
Diff since ec4544a. It holds only csv_scan_split_at, its caller in build_row_offsets_par (the clamp now happens before the call, and lo is passed), and the longer runs in csv/line_end_reference.
Not covered
- Profiling (no
perfhere). - ASan, the new test and a full suite this round.
- Windows and macOS.
…he file or without The CSV row splitter has two paths. For a file with quotes it walks the bytes and ends a row at LF, CRLF or a lone CR; for a file without quotes it searched for LF only. The cell scanner ends a cell at CR in both, so in a quote-free file with CR line ends everything after a row's first CR was never read: `a,b\r1,2\r3,4\r` read as one row, and with one quoted cell anywhere in the file the same text read as two. And the header reader consumed a CR, then an LF -- CRLF, but not LFCR -- so after an `a,b\n\r` header the first data row started at the CR and read as empty (`a,b\n\r1,2\n\r3,4\n\r` read [0N 0N] and [3 4], the 1,2 lost). The rule now, for the header and every row, with quotes in the file or without: line ends are read greedily from left to right, a CRLF or an LFCR whenever the byte at the position starts one, else the single LF or CR (so \n\r\n\r is two line ends, as dev read it in a quote-free file); inside a quoted cell these bytes are text. One helper, csv_eol_len, says how long the line end at a byte is, and every reader of line ends uses it: the fast and the byte-at-a-time scans, serial and parallel, the header readers, and the strict line counter. A parallel chunk boundary that falls inside a run of line-end bytes is moved, by at most one byte, to where a line end starts, so every chunk begins where the serial scan would begin a row. It is found without reading the run from its start: read greedily, a line end starts at a run's first byte and at every byte equal to the one before it (that byte ended a line end), and between such points the bytes alternate and pair up, so the nearest such point at or before the boundary -- or the boundary before it, already resolved -- decides by parity. The walk back never passes the boundary before, so over all boundaries each byte is read at most once, and in a run of LF or of CR not at all: 400 MB of LF reads in 3.7 s at 28 workers (dev 3.9 s; a walk to the run's start at each boundary took 45.5 s), 200M LFCR in 2.2-2.3 s (dev 1.9-2.4 s; it took 25.4 s), and a file of 40000 runs of 5000 alternating bytes in 0.94-1.05 s (dev 0.96-1.03 s). The fast path for a file without quotes searched for LF with memchr; it now searches for LF and CR in the same pass and takes the line end there by csv_eol_len. csv_find_eol reads aligned 32-byte blocks with AVX2, as the C library's memchr does (an aligned block never leaves its page, so the bytes before and after the range in its first and last block, all in the file's mapping, are read and masked off), 16 unaligned bytes at a time with SSE2, and bytewise otherwise; a version with unaligned 32-byte loads ran 1-2% more cycles on a one-worker load, the aligned one runs dev's. So an LF or CRLF file costs one search per line, as before, and no pass is added to find out which kind of file it is. The three versions agree with a bytewise search on 400000 random ranges each. The byte-at-a-time path, for files with quotes, already stopped at both and now reads LFCR as one line end; the parallel scan's chunk boundaries never split a CRLF or an LFCR, in either path. Strict mode's refusal of text after a lone CR (#1888) cannot happen any more and is gone, and its line counter reads line ends the same way. What .csv.write writes is unaffected: it ends rows with LF and quotes every cell holding a CR or an LF, so its output takes the same path and reads back cell for cell, CR and LF in cells included (checked). Interleaved timings, best of 7 (5 at 28 workers, 7 for the imports), ms, clean gcc release builds of dev (1691a57) and this, one worker pinned to a performance core and all 28; the machine was shared, load average 4 to 12. The files are those of #1888's and #1894's measurements -- 1 GB of 13M rows with LF line ends (I64 F64 SYM STR DATE TIMESTAMP I32 B8), the same with CRLF, 520 MB of 6.5M rows with quoted cells, 520 MB of 6M rows of eight types, an 8000-row file read 2000 times (the serial scan) -- and .csv.splayed imports of the two 1 GB files: | file | workers | dev | this | |---|---|---|---| | 1 GB, LF | 1 | 1737 | 1745 (+0.4%) | | 1 GB, LF | all | 384 | 385 (+0.3%) | | 1 GB, CRLF | 1 | 1741 | 1744 (+0.2%) | | 1 GB, CRLF | all | 414 | 411 (-0.6%) | | quoted, 520 MB | 1 | 1245 | 1251 (+0.5%) | | quoted, 520 MB | all | 223 | 221 (-0.6%) | | 8 types, 520 MB | 1 | 826 | 823 (-0.3%) | | 8 types, 520 MB | all | 200 | 200 (+0.1%) | | 8000 rows x2000 | 1 | 1522 | 1521 (-0.0%) | | .csv.splayed 1 GB, LF | all | 2189 | 2218 (+1.3%) | | .csv.splayed 1 GB, CRLF | all | 2192 | 2138 (-2.5%) | The scan for a file with quotes no longer walks bytes one at a time either. Inside a quoted cell it jumped to the closing quote with memchr before; outside one it now jumps to the next quote or line end with csv_find_qeol (csv_find_eol with the quote as a third byte), in the parallel chunk scan, the serial scan and the windowed scan of the imports. The reviews measured the quoted 276 MB file (6M rows, I64 F64 SYMBOL "q,<i>" DATE) 3-5% slower than dev at 28 workers, which held on a clang build (the Makefile's default compiler) but not on gcc: phase timers put the whole difference in the quoted pass of the parallel scan (12.9 against 17.8 ms), whose byte loop clang compiled about a third slower here than dev's identical loop, whether out of line or not and with or without the LFCR test. Without the byte loop that cost is gone. As the reviews measured it -- each round the minimum of 7 loads in one process, 6 rounds interleaved with a byte copy of dev's binary as the control, load 5-8, the fixture regenerated from the review's recipe: | build | workers | dev | dev copy | this | |---|---|---|---|---| | clang | 28 | 81.9 | 81.4 | 75.1 (-8%) | | clang | 1 | 471.8 | 467.5 | 399.0 (-15%) | | gcc | 28 | 78.7 | | 72.8 (-7%) | | gcc | 1 | 565.9 | | 400.6 (-29%) | The other files, best of 3, gcc / clang: 1 GB LF at one worker 1728 -> 1737 / 1855 -> 1810 ms, at 28 workers 392 -> 394 / 390 -> 392; CRLF 1730 -> 1737 / 1845 -> 1818, 411 -> 405 / 402 -> 405; the 520 MB quoted file 1262 -> 880 / 1171 -> 927 at one worker, 212 -> 202 / 218 -> 199 at 28. CPU cycles of a one-worker load, against dev: 1 GB LF -0.6%, CRLF +1.4%, quoted +0.9%, the serial file +0.3% (instructions +0.5%, +0.6%, +0.1%, +0.5%). No shape is slower beyond run-to-run noise. Test: test/rfl/io/csv_line_ends.rfl, which fails on dev (the CR-only file reads one row): CR line ends with and without quotes, an LFCR header and rows, every kind of line end mixed in one file (LF, CRLF, CR, LFCR, and LF followed by CRLF for an empty line), CR and LF inside quoted cells under CR line ends, a .csv.write round trip of cells holding CR, LF and CRLF, 400000-row files past the parallel scan's 4 MB with CR, LFCR, CR and quotes, and CRLF line ends, the same files through .csv.splayed and .csv.parted, and a header-only file and a last line without its CR. csv/strict_matrix's lone-CR case now expects the text after the CR as the next row, one line on; csv_strict.rfl reads the quote-free CR file whole. csv/strict_line_ends puts every pair and triple of the four line ends back to back between a valid row and a bad one, with quotes in the file and without, and expects the strict error at the line the reader's row count gives: the line counter reads line ends as the row scans do (csv_line_of walks the file with csv_find_eol and csv_eol_len), so a CRLF followed by a lone CR is two lines, not one. csv/line_end_reference checks the rule itself against a reference tokenizer written from the sentence above: files of cells (digits, quoted digits, quoted text holding line ends) between runs of 1 to 4 line-end bytes -- every one of the 30 such runs, then random ones, and in a third set runs of up to 3000 bytes, alternating, of one byte or random, so that several chunk boundaries fall inside one run -- read as one column, rows and cells compared with the reference, and a malformed last row reported at the reference's line; 20 seeds each under the serial scan (60 rows) and the parallel one (3000 rows, which the test build cuts into 64-byte chunks, so boundaries fall inside runs). It fails with the previous rule, and with the greedy rule but the previous boundary handling (5820 rows where the reference has 5818), and with the boundary not moved inside a run (5840 rows). Against dev, 400 random small files (half with quotes, half with a header, rows separated by random runs of 1 to 4 line-end bytes) read alike at 1 and 28 workers on this head; 302 read differently from dev, and every one falls in a class the trailer lists. Fixes #1891 BREAKING CHANGE: CSV reads (.csv.read, .csv.splayed, .csv.parted) can return different rows from before for exactly these inputs: (1) a file without quotes in which, read by the rule above, a CR stands alone (rows were split at LF only, a CR right after an LF was absorbed, and the text after any other CR in a line was dropped: a CR-only file read as one row, and \r\n\r read as one line end, now two); (2) a file with quotes that holds an LFCR outside its quoted cells (each read as two line ends, an empty, null row after each: [0N 1 0N 2 0N] for two rows, now [1 2]); (3) a file whose header ends in LFCR (its first data row read as nulls, starting at the CR), also when names are given and the matching header is skipped; (4) a file whose header ends in a CR, whose delimiter is now chosen from the header alone (the count of commas and tabs ran on into the data up to the first LF). Under {strict: true} a lone CR now ends a row instead of raising "text after a CR inside the row".
bd1c42c to
320e57b
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of 320e57b: READY
The quoted scan no longer walks bytes one at a time, the rule is unchanged, and the quoted file is now faster than dev beyond the control spread.
csv_find_qeol. I extracted it from this head and built its three forms: AVX2, SSE2, and bytewise (-mno-sse2 -mgeneral-regs-only), each with gcc and clang. Each was compared with a bytewise search for LF, CR or ".
- The buffer: 4 pages, with a
PROT_NONEguard page on both sides. - The ranges: every start 0–79 with every length 0–79, at the buffer's start and ending at its last byte, plus 4M random ranges. Contents were random at varying densities of the three bytes, empty of them, or a single quote as the last byte.
- Result: 6.56M ranges per form and compiler, no mismatch and no fault, also under ASan+UBSan.
Its callers scan the same file mappings as csv_find_eol, so the aligned blocks stay inside the mapping.
The quoted scan. The state machine reads the same as the byte walk:
- inside a cell,
memchrjumps to the closing quote; - outside one,
csv_find_qeoljumps to the next quote or line end; - a doubled quote closes the cell and opens it again;
- a chunk that ends inside a cell with no closing quote stops, as before.
I checked it against my reference tokenizer, written from the rule and quote-aware, on 300 small files and six 14 MB files.
- What the files contain:
- doubled quotes, and cells that are one quote or two;
- cells holding commas and all four line ends,
\n\r\n\rincluded; - runs of 1–4 line-end bytes between rows;
- files ending in an unclosed quote as the last byte, files ending inside an unclosed quoted cell, and files with no final line end;
- cells of 150–600 KB, longer than a 28-worker chunk of about 125 KB, so boundaries fall inside quoted cells.
- Against the reference: each file was read headerless
[STR STR]at 1, 2, 8 and 28 workers, written with.csv.writeand parsed back. All 306 files match at every worker count. - Against the previous head bd1c42c: the written output is byte-identical on all 306 files at all four worker counts, so the rule did not change.
- The imports:
.csv.splayedand.csv.parted(rows-per-part 50000) on the six large files read back equal to.csv.read, at 2 and 28 workers. - Strict line numbers: 120 cases, runs of 1–4 line-end bytes after a plain or a quoted cell holding CRLF, with the bad cell plain or quoted. All name the expected line.
Timing, against the control. Clang release builds (the Makefile's default) of dev 283f08f, a byte-identical copy of it, and this head. The file is the quoted 276 MB one (6M rows, I64 F64 SYMBOL quoted STR DATE); 6 interleaved rounds, each the minimum of 7 loads; load average 5.3–8.3.
| binary | 28 workers, min (ms) | 1 worker pinned, min (ms) |
|---|---|---|
| dev | 83.0 | 831.7 |
| dev copy (control) | 82.3 | 829.6 |
| this head | 75.0 (−9%) | 691.2 (−17%) |
The control spread is under 1%. This head is faster than both controls, beyond that spread, which agrees with the claim.
Sanitizers. Under ASan+UBSan these pass:
- the csv tests (111, 1 skipped);
- the
line_endtests (5); csv_strict(2);strict_matrix(1).
Not covered:
- A full suite this round.
- A gcc timing of this head; only the clang build was timed.
- Windows and macOS.
The CSV row splitter has two paths. For a file with quotes it walks the
bytes and ends a row at LF, CRLF or a lone CR; for a file without quotes
it searched for LF only. The cell scanner ends a cell at CR in both, so
in a quote-free file with CR line ends everything after a row's first CR
was never read:
a,b\r1,2\r3,4\rread as one row, and with one quotedcell anywhere in the file the same text read as two. And the header
reader consumed a CR, then an LF -- CRLF, but not LFCR -- so after an
a,b\n\rheader the first data row started at the CR and read as empty(
a,b\n\r1,2\n\r3,4\n\rread [0N 0N] and [3 4], the 1,2 lost).The rule now, for the header and every row, with quotes in the file or
without: line ends are read greedily from left to right, a CRLF or an
LFCR whenever the byte at the position starts one, else the single LF or
CR (so \n\r\n\r is two line ends, as dev read it in a quote-free file);
inside a quoted cell these bytes are text. One helper, csv_eol_len, says
how long the line end at a byte is, and every reader of line ends uses
it: the fast and the byte-at-a-time scans, serial and parallel, the
header readers, and the strict line counter. A parallel chunk boundary
that falls inside a run of line-end bytes is moved, by at most one byte,
to where a line end starts, so every chunk begins where the serial scan
would begin a row. It is found without reading the run from its start:
read greedily, a line end starts at a run's first byte and at every byte
equal to the one before it (that byte ended a line end), and between such
points the bytes alternate and pair up, so the nearest such point at or
before the boundary -- or the boundary before it, already resolved --
decides by parity. The walk back never passes the boundary before, so
over all boundaries each byte is read at most once, and in a run of LF or
of CR not at all: 400 MB of LF reads in 3.7 s at 28 workers (dev 3.9 s; a
walk to the run's start at each boundary took 45.5 s), 200M LFCR in
2.2-2.3 s (dev 1.9-2.4 s; it took 25.4 s), and a file of 40000 runs of
5000 alternating bytes in 0.94-1.05 s (dev 0.96-1.03 s).
The fast path for a file without quotes searched for LF with memchr; it
now searches for LF and CR in the same pass and takes the line end there
by csv_eol_len. csv_find_eol reads aligned 32-byte blocks with AVX2, as
the C library's memchr does (an aligned block never leaves its page, so
the bytes before and after the range in its first and last block, all in
the file's mapping, are read and masked off), 16 unaligned bytes at a time
with SSE2, and bytewise otherwise; a version with unaligned 32-byte loads
ran 1-2% more cycles on a one-worker load, the aligned one runs dev's. So
an LF or CRLF file costs one search per line, as before, and no pass is
added to find out which kind of file it is. The three versions agree
with a bytewise search on 400000 random ranges each. The byte-at-a-time path, for files with
quotes, already stopped at both and now reads LFCR as one line end; the
parallel scan's chunk boundaries never split a CRLF or an LFCR, in either
path. Strict mode's refusal of text after a lone CR (#1888) cannot happen
any more and is gone, and its line counter reads line ends the same way.
What .csv.write writes is unaffected: it ends rows with LF and quotes
every cell holding a CR or an LF, so its output takes the same path and
reads back cell for cell, CR and LF in cells included (checked).
Interleaved timings, best of 7 (5 at 28 workers, 7 for the imports), ms,
clean gcc release builds of dev (1691a57) and this, one worker pinned to
a performance core and all 28; the machine was shared, load average 4 to
12. The files are those of #1888's and #1894's measurements -- 1 GB of
13M rows with LF line ends (I64 F64 SYM STR DATE TIMESTAMP I32 B8), the
same with CRLF, 520 MB of 6.5M rows with quoted cells, 520 MB of 6M rows
of eight types, an 8000-row file read 2000 times (the serial scan) -- and
.csv.splayed imports of the two 1 GB files:
The scan for a file with quotes no longer walks bytes one at a time
either. Inside a quoted cell it jumped to the closing quote with memchr
before; outside one it now jumps to the next quote or line end with
csv_find_qeol (csv_find_eol with the quote as a third byte), in the
parallel chunk scan, the serial scan and the windowed scan of the
imports. The reviews measured the quoted 276 MB file (6M rows, I64 F64
SYMBOL "q," DATE) 3-5% slower than dev at 28 workers, which held on a
clang build (the Makefile's default compiler) but not on gcc: phase
timers put the whole difference in the quoted pass of the parallel scan
(12.9 against 17.8 ms), whose byte loop clang compiled about a third
slower here than dev's identical loop, whether out of line or not and
with or without the LFCR test. Without the byte loop that cost is gone.
As the reviews measured it -- each round the minimum of 7 loads in one
process, 6 rounds interleaved with a byte copy of dev's binary as the
control, load 5-8, the fixture regenerated from the review's recipe:
The other files, best of 3, gcc / clang: 1 GB LF at one worker 1728 ->
1737 / 1855 -> 1810 ms, at 28 workers 392 -> 394 / 390 -> 392; CRLF 1730
-> 1737 / 1845 -> 1818, 411 -> 405 / 402 -> 405; the 520 MB quoted file
1262 -> 880 / 1171 -> 927 at one worker, 212 -> 202 / 218 -> 199 at 28.
CPU cycles of a one-worker load, against dev: 1 GB LF -0.6%, CRLF +1.4%,
quoted +0.9%, the serial file +0.3% (instructions +0.5%, +0.6%, +0.1%,
+0.5%). No shape is slower beyond run-to-run noise.
Test: test/rfl/io/csv_line_ends.rfl, which fails on dev (the CR-only file
reads one row): CR line ends with and without quotes, an LFCR header and
rows, every kind of line end mixed in one file (LF, CRLF, CR, LFCR, and LF
followed by CRLF for an empty line), CR and LF inside quoted cells under
CR line ends, a .csv.write round trip of cells holding CR, LF and CRLF,
400000-row files past the parallel scan's 4 MB with CR, LFCR, CR and
quotes, and CRLF line ends, the same files through .csv.splayed and
.csv.parted, and a header-only file and a last line without its CR.
csv/strict_matrix's lone-CR case now expects the text after the CR as
the next row, one line on; csv_strict.rfl reads the quote-free CR file
whole. csv/strict_line_ends puts every pair and triple of the four
line ends back to back between a valid row and a bad one, with quotes in
the file and without, and expects the strict error at the line the reader's
row count gives: the line counter reads line ends as the row scans do
(csv_line_of walks the file with csv_find_eol and csv_eol_len), so a CRLF
followed by a lone CR is two lines, not one. csv/line_end_reference
checks the rule itself against a reference tokenizer written from the
sentence above: files of cells (digits, quoted digits, quoted text holding
line ends) between runs of 1 to 4 line-end bytes -- every one of the 30
such runs, then random ones, and in a third set runs of up to 3000 bytes,
alternating, of one byte or random, so that several chunk boundaries fall
inside one run -- read as one column, rows and cells
compared with the reference, and a malformed last row reported at the
reference's line; 20 seeds each under the serial scan (60 rows) and the
parallel one (3000 rows, which the test build cuts into 64-byte chunks,
so boundaries fall inside runs). It fails with the previous rule, and
with the greedy rule but the previous boundary handling (5820 rows where
the reference has 5818), and with the boundary not moved inside a run
(5840 rows).
Against dev, 400 random small files (half with quotes, half with a
header, rows separated by random runs of 1 to 4 line-end bytes) read
alike at 1 and 28 workers on this head; 302 read differently from dev,
and every one falls in a class the trailer lists.
Fixes #1891
BREAKING CHANGE: CSV reads (.csv.read, .csv.splayed, .csv.parted) can
return different rows from before for exactly these inputs: (1) a file
without quotes in which, read by the rule above, a CR stands alone (rows
were split at LF only, a CR right after an LF was absorbed, and the text
after any other CR in a line was dropped: a CR-only file read as one row,
and \r\n\r read as one line end, now two); (2) a file with quotes that
holds an LFCR outside its quoted cells (each read as two line ends, an
empty, null row after each: [0N 1 0N 2 0N] for two rows, now [1 2]); (3) a
file whose header ends in LFCR (its first data row read as nulls, starting
at the CR), also when names are given and the matching header is skipped;
(4) a file whose header ends in a CR, whose delimiter is now chosen from
the header alone (the count of commas and tabs ran on into the data up to
the first LF). Under {strict: true} a lone CR now ends a row instead of
raising "text after a CR inside the row".