Repository navigation
perf(csv): escaped text cells share blocks instead of a page mapping each - #1894
Conversation
singaraiona
left a comment
There was a problem hiding this comment.
Review of 6625ef6: NEEDS CHANGES
The ownership change is sound and the speed-up is real. One finding: a failed block allocation silently changes the data.
Finding
- An allocation failure for an escaped cell makes the load succeed with that cell empty (
csv_keep_escapedcallers incsv_parse_fn,csv_parse_fn_strictandcsv_parse_serial:if (!fld) flen = 0;).- The commit message states this, and it is better than dev, where a failed
ray_sys_allocleft the strref pointing at the dead stack buffer. But the load still answers a table whose text differs from the file, with no error. Under{strict: true}that contradicts the mode's own rule: a non-empty cell is stored as the value its text says. - Repro: I made
csv_keep_escapedreturn NULL on its third block allocation, in a temporary build since reverted. Reading 6 rows[I64 STR]with three 300,000-byte escaped cells answers[1 300000 300000 0 1 1]for the lengths: row 3's cell is silently"". - Not caught by the sweeps: the allocation-failure sweeps inject into
ray_alloconly (heap.c), neverray_sys_alloc, so none of them reaches this branch. - Fix: have each worker record a failure (a per-worker flag, as
worker_had_nullis). After the dispatch, the load takes its error path, freeing the blocks as it does now, and answersoom. The same applies to the serial parse. - Test: a test-only failure hook on the block allocation, or a
ray_sys_allocfailure counter, to pin this.
- The commit message states this, and it is better than dev, where a failed
What I checked
Ownership.
- Who reads the text: only
csv_finalize_run/csv_fill_str_coland the SYM interning (csv_intern_dicts) read the strrefs, and both run beforecsv_esc_free. That holds in bothcsv_materialize_rows(the batches of.csv.splayedand.csv.parted) andcsv_read_named_opts_inner. Each pairscsv_esc_free(esc)with thestr_ref_hdrsfrees on every exit: success, the strict error, a failed finalize, cancel, and thefail_parsed/fail_parsed_cancellabels. - The parallel route merges every worker's list into the caller's before its scratch is freed, so each block has one owner and is freed once. The serial strict parse takes the caller's list as worker 0.
- No other consumer: type inference and the header do not keep strrefs, and no other
csv_par_ctx_tis built. - Large cells: a cell over a quarter block takes a block of its own, linked behind the current one, so the current block keeps filling. Cells of 330 KB, 840 KB and 3.2 MB (over a whole block) read in full as STR and SYM.
- The 8 KB stack buffer: a cell past it, unescaped into
dyn_esc, is copied, anddyn_escis freed with the row.
Values against dev (1691a57). Every output is byte-identical by ser, at 1, 4 and 28 workers.
- The cells: doubled quotes alone, at the start, at the end, many in a row, with a space; with an embedded newline; with commas; with
ff/febytes; withé; past the stack buffer; past a quarter block. The header was"id""x", and escaped cells sat in the first, middle and last rows plus every 97th, in a 20-row file (serial) and a 30,000-row file (parallel). - The reads: inferred types;
[I64 STR SYMBOL I64 DATE], with escaped cells in the I64 and DATE columns;[I64 SYMBOL SYMBOL STR STR];{strict: true}both valid and failing;.csv.splayedread back;.csv.partedread back, plus its strict failure. - Strict messages: they quote escaped cells correctly (
"1\"2" is not an I64), and the parted note is kept.
ASan+UBSan. These pass with no report:
csv_escaped_cells, the csv tests (106, 1 skipped) and thealloc_failtests (13);- my 3.2 MB-cell probe at 1 and 4 workers: STR, SYM, a strict failure at line 15002, a parted strict failure with retained output, splayed, inferred.
Timing. Release builds, interleaved, 4 rounds of 5 loads each (minimum taken); load average 5–19.
| file | 1 worker, pinned: dev | this | 28 workers: dev | this |
|---|---|---|---|---|
| issue's escaped file, 2M rows | 8285.9 | 155.1 | 8393.3 | 14.4 |
| the same without escapes | 116.1 | 119.5 | 11.9 | 11.4 |
| 6M rows, 5 types with quoted cells, no escapes | 860.1 | 837.2 | 83.8 | 85.5 |
- A re-run of the no-escape file pinned, 8 rounds of 9, gave dev 114.4 and this 115.7 ms (+1.1%), with overlapping runs. That is within noise, consistent with the claimed 1%.
- Peak RSS on the escaped file at 4 workers: dev 8.16 GB, this 178 MB.
Suite and commit. The gcc suite: 5943 of 5947 passed, 4 skipped, 0 failed. The commit message is one commit ending Fixes #1890; it describes the change and the out-of-memory behaviour, and has no trailers beyond that.
Not covered
- Windows and macOS.
- Real memory exhaustion, rather than the injected failure.
- The serial strict parse under an injected failure.
- Cold-disk loads.
6625ef6 to
58a6116
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of 58a6116: NEEDS CHANGES (the new sweep only; the fix itself holds)
The finding is fixed: a failed block allocation now fails the load with oom. The new sweep, though, fails as soon as it runs with more than two workers.
Finding
csv/escaped_alloc_failfails at 4 or more workers: its committed-bytes check measures heap warm-up, not leaks (test/test_csv.c,test_csv_escaped_alloc_fail).- The failure: at
RAYFORCE_CORES=4the sweep failed 4 of 6 runs (-j 4), and once in 4 at-j 1. At 28 it failed every time, always with
rows 20000 shape 1 strict 0, block 2 of 4 fails: 33554432 bytes kept
(the parallel.csv.splayedimport). Under ASan with leak detection on, thatFAIL(strdup(msg))also shows up as a 69-byte leak. At the suite's defaultTEST_CORES=2it passed 12 of 12, which is why the suite is green. - Not a leak in the change: I repeated the parallel splayed import 30 times at 28 workers with no failure injected. Committed bytes still grow (505 MB, 740, 841, 874, 975 MB) and then stay at 975 MB. With escape-free input they stay flat from there. The read shape stays at 35 MB whether runs succeed or fail. So the growth is the workers' heap pools claimed on first use (it comes in 32 MiB steps). One unarmed warm-up run does not cover them when many workers take part, and the check reads that growth as kept bytes.
- Fix: measure what the change owns instead of total committed bytes. For example, a hook counting live escape blocks (allocated minus freed) that must be 0 after each run; or warm up until committed bytes stop moving; or pin the sweep's pool size. Then the sweep holds at any
RAYFORCE_CORES.
- The failure: at
What I checked
My injected-failure repro.
- The repro, 6 rows
[I64 STR]with three 300,000-byte escaped cells and the third block allocation failing (now throughray_csv_esc_fail_for_test(3)), answersoomlenient and strict, where 6625ef6 answered a table with that cell empty. oomcomes with no message, as #1886 allows foroom.
Cleanup on the oom exit.
- The read path: both
csv_materialize_rowsandcsv_read_named_opts_innertake the flag after the parse and before the strict-error check. They free the block list, the strref arrays and the columns once (the latter viafail_parsed, with the row offsets and the mapping), then answeroom. - Under ASan+UBSan, across my probe, the sweep and the csv tests:
- no use-after-free and no double free;
- no leak other than the sweep's own
strdupon its failure path.
.csv.partedwith rows-per-part 5000 and a late block failing (4 of 6), lenient or strict, answersoomwithcsv.parted: import failed; partial output retained in <root>.csv-partial. The completed partitions0/t,1/tand2/tare left in the staging directory and no destination is created, as for any batch error..csv.splayedanswersoomwith.csv.splayed: the import failed (oom): "<src>" into "<dir>/", and leaves no directory.
The hook is #ifdef DEBUG in csv.c and csv.h, and the call site folds to false otherwise. The release binary has no csv_esc_fail, csv_esc_allocs or g_csv_esc symbol.
Diff since 6625ef6. It holds only:
- the
esc_oomflag (_Atomic(bool)*in the context, set in the parallel, strict and serial parses), and the twooomexits; - the DEBUG hook;
- the sweep, and the
mem/sys.hinclude for it.
Tests. Under ASan+UBSan: csv_escaped_cells (2) and the csv tests (108, 1 skipped) pass, and so does the sweep at 2 cores.
Not covered:
- A gcc suite this round: the source change since my last suite is the flag and the exits.
- Real memory exhaustion.
- Windows and macOS.
…each A quoted STR or SYM cell with a doubled quote (`"say ""hi"""`) is unescaped into a stack buffer, so the parse kept a copy of it until the strings were filled in: `ray_sys_alloc(flen)`, which maps whole pages, and a later unmap per cell. At a few microseconds and at least 4 KB of mapping a cell, a 2M-row file with one such cell per row read in 4.8 s against 0.09 s without the escapes, and held 8.3 GB of mappings at its peak. Each worker now keeps its copies in 1 MB blocks it fills in turn (a copy over a quarter block gets a block of its own), the workers' blocks go to the load, and the load frees them where it freed the copies, on success, failure and cancel alike. A cell past the 8 KB stack buffer, which the scanner unescapes into its own allocation, is copied the same way and its allocation freed with the cell. The per-column "had escaped" flags and the per-row "strref initialized" marks existed only so the old cleanup could find the copies among the strrefs, and are gone, with the n_rows array the marks needed. The strict parse (#1888) keeps its copies the same way. If a block cannot be allocated the load fails with `oom`: the worker marks a flag the load shares (serial, parallel and strict parses alike), the load checks it once the parse is over, frees every block and the rest of what it built, and answers the error -- in .csv.read, and in each batch of .csv.splayed and .csv.parted, which then fail as on any other batch error. A cell is never kept wrong. On dev the same failure was worse than an empty cell: the strref kept pointing at the dead stack buffer, so the cell read as whatever later rows left there, and the cleanup then passed that stack address to the allocator's free (with a failure injected into dev's per-cell copy, a 6-row read stops under UBSan in src/mem/sys.c, reading the freed "header" at the stack address). Interleaved timings, best of 7 (5 at 28 workers), 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 13. The escaped-cell file is 2M rows of `id,"n,<i>""x"` read as [I64 STR]; the others are the files of #1888's measurements: 1 GB of 13M rows (I64 F64 SYM STR DATE TIMESTAMP I32 B8), 520 MB of 6.5M rows with quoted STR cells, 520 MB of 6M rows of eight types with TIME and TIMESTAMP, and an 8000-row file read 2000 times (the serial parse): | file | workers | dev | this | |---|---|---|---| | escaped cells, 2M rows | 1 | 4756 | 110-116 (-97.6%) | | escaped cells, 2M rows | all | 8438-9137 | 24.1-26.6 (-99.7%) | | 1 GB | 1 | 1758 | 1766 (+0.5%) | | 1 GB | all | 408 | 411 (+0.7%) | | quoted, 520 MB | 1 | 1270 | 1257 (-1.0%) | | quoted, 520 MB | all | 225 | 223 (-0.8%) | | 8 types, 520 MB | 1 | 840 | 844 (+0.5%) | | 8 types, 520 MB | all | 203 | 201 (-0.6%) | | 8000 rows x2000 | 1 | 1518 | 1489 (-1.9%) | Anonymous memory peak of the read (`.sys.mem` anon-peak, after less before): 100k escaped cells 410 MB -> 13.8 MB; 2M escaped cells 8.29 GB -> 130 MB. Instructions of a default load without escapes, against dev: 1 GB +0.19%, quoted +0.10%, the serial file -1.2% (no per-row marks to write); no shape is slower beyond run-to-run noise. Test: test/rfl/io/csv_escaped_cells.rfl, which fails on dev (the read raises the anonymous peak by about 400 MB) and checks the values of escaped cells small, past the 8 KB stack buffer and past a quarter block, as STR and SYM, across the parallel parse and through .csv.splayed. It runs first in a fresh worker under -j (test/main.c fresh_tests), so the peak it bounds is its own. csv/escaped_alloc_fail (test_csv.c) fails each block allocation in turn through a test hook (ray_csv_esc_fail_for_test, DEBUG only: the blocks come from the system allocator, which the heap's failure sweeps do not reach) in a 50-row file (the serial parse) and a 20000-row one (the parallel parse), each read lenient and strict and imported with .csv.splayed and .csv.parted, and expects `oom`, and no block live after any run, failed or not (the hook counts blocks allocated less blocks freed: they are mapped pages, which the leak checker does not see). With the flag not checked it answers success at the first failure; with one free left out it reports the blocks left live. It passed 20 runs of 20 at each of RAYFORCE_CORES 1, 2, 4, 8 and 28. (The second row of timings is after the oom change: the escaped-cell file runs the same instructions, 2131599106 against 2131595748; the ordinary files stay within 0.7% of dev.) Fixes #1890
58a6116 to
cf9c7b5
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of cf9c7b5: READY
The test finding is fixed.
Diff since 58a6116. It touches only:
- the DEBUG hook in
src/io/csv.c: ag_csv_esc_livecounter,ray_csv_esc_live_for_test, andCSV_ESC_LIVE(±1)at the block allocation and incsv_esc_free; - the hook's declaration in
csv.h, under#ifdef DEBUG; - the sweep in
test/test_csv.c.
Without DEBUG, CSV_ESC_LIVE is ((void)0) and the hook functions do not exist, so the release path is unchanged.
The sweep. csv/escaped_alloc_fail now requires 0 live blocks after the unarmed run and after every oom run, in place of the committed-bytes comparison.
- Debug build (
make testflavour): 6 of 6 passes atRAYFORCE_CORES=4, and 6 of 6 at 28. - ASan+UBSan (
make test-full-bin,setarch -R): 3 of 3 at 4 and 3 of 3 at 28, with no sanitizer report. At 58a6116 the sweep failed 4 of 6 runs at 4 cores and every run at 28. - The check still catches a leak: with the
csv_esc_free(esc)removed fromcsv_materialize_rows'oomexit (a temporary edit, since reverted), the sweep fails withrows 50 shape 1 strict 0, block 1 of 3 fails: 3 blocks live.
Not covered: a full suite this round, since the change is test-side only; 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: a line ends at LF, CRLF, a lone CR, or LFCR (an LF and the CR right after it, unless that CR starts a CRLF), in any mix; inside a quoted cell they are text. One helper, csv_eol_len, says how long the line end at a byte is, and the byte-at-a-time scans, the parallel scan's chunk boundaries and the header readers use it. 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. 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%) | 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. Fixes #1891 BREAKING CHANGE: a CSV file without quotes whose lines end in a lone CR, or whose header ends in LFCR, now reads every row; before, the rows after a CR in the same LF-delimited line were dropped, and the first row after an LFCR header read as nulls.
…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: a line ends at LF, CRLF, a lone CR, or LFCR (an LF and the CR right after it, unless that CR starts a CRLF), in any mix; inside a quoted cell they are text. One helper, csv_eol_len, says how long the line end at a byte is, and the byte-at-a-time scans, the parallel scan's chunk boundaries and the header readers use it. 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 parallel scan's two loops are separate out-of-line functions (csv_scan_chunk_fast, csv_scan_chunk_quoted), so the quoted byte loop is not compiled around the inlined vector search; a review saw a quoted 276 MB file 3-4% slower at 28 workers. Measured here against dev 283f08f on such a file (6M rows, I64 F64 SYMBOL quoted STR DATE, 267 MB), 15 interleaved rounds at 28 workers on a loaded machine (load 11-13): dev 168.4 ms, the reviewed head 165.6 (-1.7%), this 164.3 (-2.4%), medians 179.5 / 179.2 / 179.4; one worker pinned: 709.1, 697.6, 702.6. The slowdown did not show here. 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. Fixes #1891 BREAKING CHANGE: CSV reads (.csv.read, .csv.splayed, .csv.parted) return different rows for these inputs, all of which lost or added rows before: (1) a file without quotes whose lines end in a lone CR now reads every row (before, a CR-only file read as one row, and the text after a lone CR in an LF line was dropped); (2) a file whose header ends in LFCR now reads its first data row (before, it read as nulls, the row starting at the CR), and so does one read with names whose matching LFCR header is skipped; (3) a file with quotes whose lines end in LFCR no longer reads an empty, null row after each one (before, [0N 1 0N 2 0N] for two rows, now [1 2]); (4) the delimiter of a file whose header ends in a CR is now chosen from the header alone (before, the count of commas and tabs ran on into the data up to the first LF). Under {strict: true} the text after a lone CR is now a row of its own rather than the "text after a CR inside the row" error.
…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 to the end of the line end that covers it, found by reading the run greedily from its start, so every chunk begins where the serial scan would begin a row. 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 parallel scan's two loops are separate out-of-line functions (csv_scan_chunk_fast, csv_scan_chunk_quoted), so the quoted byte loop is not compiled around the inlined vector search; a review saw a quoted 276 MB file 3-4% slower at 28 workers. Measured here against dev 283f08f on such a file (6M rows, I64 F64 SYMBOL quoted STR DATE, 267 MB), 15 interleaved rounds at 28 workers on a loaded machine (load 11-13): dev 168.4 ms, the reviewed head 165.6 (-1.7%), the split 164.3 (-2.4%); on this head, 12 rounds at load 3-6: dev 147.5, this 146.6 (-0.6%), medians 150.3 / 149.9. The slowdown did not show here. On this head, against dev 283f08f (best of 5, load 3-6): 1 GB LF 1738.6 -> 1744.4 ms (+0.3%) at one worker, 386.6 -> 394.0 (+1.9%, medians 393.8 / 397.9) at 28; CRLF 1742.7 -> 1748.1 (+0.3%), 403.1 -> 406.2 (+0.8%); quoted 1256.0 -> 1204.5 (-4.1%), 210.7 -> 208.4 (-1.1%); the serial file 1494.9 -> 1491.5 (-0.2%). 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 -- 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). 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".
…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 parallel scan's two loops are separate out-of-line functions (csv_scan_chunk_fast, csv_scan_chunk_quoted), so the quoted byte loop is not compiled around the inlined vector search; a review saw a quoted 276 MB file 3-4% slower at 28 workers. Measured here against dev 283f08f on such a file (6M rows, I64 F64 SYMBOL quoted STR DATE, 267 MB), 15 interleaved rounds at 28 workers on a loaded machine (load 11-13): dev 168.4 ms, the reviewed head 165.6 (-1.7%), the split 164.3 (-2.4%); and, as the reviews measured it (each round the minimum of 7 loads in one process, rounds interleaved, 12 rounds, load 7-9), dev 76.6 ms (median 76.8), a second copy of dev's binary 76.1 (-0.7%, median 77.1), 0fa8911 76.1 (-0.7%), ec4544a 76.7 (+0.1%), this 77.5 (+1.1%, median 78.8). On these builds every head is within the spread of dev against its own copy, so the 3-5% the reviews saw does not reproduce here; the quoted scan's one added cost is the LFCR test at each line end, +0.13% instructions on a one-worker read of the file. On this head, against dev 283f08f (best of 5, load 3-6): 1 GB LF 1738.6 -> 1744.4 ms (+0.3%) at one worker, 386.6 -> 394.0 (+1.9%, medians 393.8 / 397.9) at 28; CRLF 1742.7 -> 1748.1 (+0.3%), 403.1 -> 406.2 (+0.8%); quoted 1256.0 -> 1204.5 (-4.1%), 210.7 -> 208.4 (-1.1%); the serial file 1494.9 -> 1491.5 (-0.2%). 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".
…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".
A quoted STR or SYM cell with a doubled quote (
"say ""hi""") isunescaped into a stack buffer, so the parse kept a copy of it until the
strings were filled in:
ray_sys_alloc(flen), which maps whole pages, anda later unmap per cell. At a few microseconds and at least 4 KB of
mapping a cell, a 2M-row file with one such cell per row read in 4.8 s
against 0.09 s without the escapes, and held 8.3 GB of mappings at its
peak.
Each worker now keeps its copies in 1 MB blocks it fills in turn (a copy
over a quarter block gets a block of its own), the workers' blocks go to
the load, and the load frees them where it freed the copies, on success,
failure and cancel alike. A cell past the 8 KB stack buffer, which the
scanner unescapes into its own allocation, is copied the same way and its
allocation freed with the cell. The per-column "had escaped" flags and
the per-row "strref initialized" marks existed only so the old cleanup
could find the copies among the strrefs, and are gone, with the n_rows
array the marks needed. The strict parse (#1888) keeps its copies the
same way.
If a block cannot be allocated the load fails with
oom: the workermarks a flag the load shares (serial, parallel and strict parses alike),
the load checks it once the parse is over, frees every block and the rest
of what it built, and answers the error -- in .csv.read, and in each
batch of .csv.splayed and .csv.parted, which then fail as on any other
batch error. A cell is never kept wrong. On dev the same failure was
worse than an empty cell: the strref kept pointing at the dead stack
buffer, so the cell read as whatever later rows left there, and the
cleanup then passed that stack address to the allocator's free (with a
failure injected into dev's per-cell copy, a 6-row read stops under UBSan
in src/mem/sys.c, reading the freed "header" at the stack address).
Interleaved timings, best of 7 (5 at 28 workers), 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 13. The
escaped-cell file is 2M rows of
id,"n,<i>""x"read as [I64 STR]; theothers are the files of #1888's measurements: 1 GB of 13M rows (I64 F64
SYM STR DATE TIMESTAMP I32 B8), 520 MB of 6.5M rows with quoted STR cells,
520 MB of 6M rows of eight types with TIME and TIMESTAMP, and an 8000-row
file read 2000 times (the serial parse):
Anonymous memory peak of the read (
.sys.memanon-peak, after lessbefore): 100k escaped cells 410 MB -> 13.8 MB; 2M escaped cells 8.29 GB
-> 130 MB. Instructions of a default load without escapes, against dev:
1 GB +0.19%, quoted +0.10%, the serial file -1.2% (no per-row marks to
write); no shape is slower beyond run-to-run noise.
Test: test/rfl/io/csv_escaped_cells.rfl, which fails on dev (the read
raises the anonymous peak by about 400 MB) and checks the values of
escaped cells small, past the 8 KB stack buffer and past a quarter block,
as STR and SYM, across the parallel parse and through .csv.splayed. It
runs first in a fresh worker under -j (test/main.c fresh_tests), so the
peak it bounds is its own. csv/escaped_alloc_fail (test_csv.c) fails each
block allocation in turn through a test hook (ray_csv_esc_fail_for_test,
DEBUG only: the blocks come from the system allocator, which the heap's
failure sweeps do not reach) in a 50-row file (the serial parse) and a
20000-row one (the parallel parse), each read lenient and strict and
imported with .csv.splayed and .csv.parted, and expects
oom, and noblock live after any run, failed or not (the hook counts blocks allocated
less blocks freed: they are mapped pages, which the leak checker does not
see). With the flag not checked it answers success at the first failure;
with one free left out it reports the blocks left live. It passed 20 runs
of 20 at each of RAYFORCE_CORES 1, 2, 4, 8 and 28. (The second row of
timings is after the oom change: the escaped-cell file runs the same
instructions, 2131599106 against 2131595748; the ordinary files stay
within 0.7% of dev.)
Fixes #1890