Skip to content

Settle the Chunks schema one index chunk at a time - #1243

Open
quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/chunks-settle-by-chunk
Open

quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/chunks-settle-by-chunk

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Why

CSV.Chunks spent about half of its time before the first batch. To give every batch the same column types, its constructor validates every cell of each window (64 MiB by default). It did that one column at a time across the whole window: one task per column, so a two-column file kept two of ten threads busy, and each task fetched a line of input and a line of index from memory for every row of the window, so wide files reread each window once per column. On a 100,000-row, 500-column file the constructor took twice the CPU of a whole CSV.File read.

What changed

settlebatchschema! validates one index chunk (64 KiB to 1 MiB) at a time: each task takes a chunk and checks all of its selected columns while those bytes are in cache, and the chunks of a window run in parallel. Every chunk starts from the window's incoming types and widens a column only as far as its own cells need. Widening keeps every value a narrower type accepted, so the window's type is the join of the chunks' types (promote_kernel) in any order. Microsecond timestamps are the one exception: they cannot hold every nanosecond value, so a chunk that settled at nanoseconds is checked again under microseconds when its window widens. The column walk got the same result by restarting the window. Missing flags are OR-ed and text widths take the maximum, as before. The batches themselves are unchanged.

Verification

for f in CSV.Chunks(path), new/main, median of 5 interleaved fresh-process rounds (Apple M3 Max, Julia 1.12.6, load average 8 to 14). Ten threads (the default here): wall time and whole-process cycles. One task (ntasks=1): calling-thread cycles and instructions, the per-cell work.

file 10 threads, wall 10 threads, cycles 1 task, cycles 1 task, instructions
10M × 2 mixed 0.522× 0.880× 0.999× 1.001×
100k × 500 0.682× 0.663× 0.683× 1.003×
5M × 4, 80% empty 0.690× 0.992× 0.975× 0.994×
2M × 4 date-times 0.756× 0.997× 1.011× 1.002×
5M × 4 integers 0.763× 0.907× 1.016× 1.000×
10M × 4 Bool 0.775× 0.975× 0.991× 1.005×
5M × 4 dates 0.782× 0.920× 0.987× 0.993×
2M × 4 quoted text 0.908× 0.998× 0.987× 1.003×
1M × 50 mixed 0.645× 0.989×
1M × 4 long text 0.839× 0.939×

An earlier run of all 13 files at ten threads under heavier load (13 to 25) put every file at 0.63× to 0.93× of main's wall time except quoted text and the 80%-empty file, whose rounds swung from 0.6× to 1.6×; the rerun above, at lower load, puts them at 0.91× and 0.69×. One task does the same work as before (instructions within 1%, apart from 6% fewer on long text), and the wide files run in 0.65× to 0.68× of the cycles because each chunk stays in cache.

  • Full-content fingerprints of every CSV.Chunks batch (types, values, problems) on 13 generated files (1M to 10M rows: integers, floats, Bool, dates, date-times, short and long text, quoted text with embedded delimiters and newlines, missing-heavy, 2, 50 and 500 columns) are identical to main (checked on the merge of the five read PRs from this sweep: 221 fingerprints, zero differences).
  • Differential run: 60 seeded files (20k to 60k rows, eight column kinds) with type conflicts placed at random rows: Int128 and float cells in integer columns, nanosecond fractions and out-of-range years in timestamp columns, invalid dates and Bool spellings, values in all-missing columns, quoted newlines. Each is read with 256 KiB, 1 MiB and whole-file windows at ntasks 1 and 4, plus a types= request. Batches are identical to main in all 420 reads.
  • New test: one window whose conflicts sit in different index chunks (Int to Float64, a nanosecond fraction in one chunk and year 9999 in another, a late Int128 in a missing column, an invalid date), at ntasks 1 and 4: the batch's types and values equal CSV.File's. It passes on main too. With the nanosecond re-check removed, it fails: the column settles at microseconds and the nanosecond cell silently becomes missing.
  • Adversarial read: the join relies on each settled type accepting every value a narrower settled type accepted, which promote_kernel's ladder (Int64, Int128, Float64, String; nanosecond then microsecond timestamps, String otherwise) gives, apart from the microsecond case handled above. Requested types never widen; their chunks only report missing cells. Tasks write disjoint (column, chunk) cells of three result matrices, a Matrix{Bool} among them (one byte per cell, no shared words). Chunk order never matters to the fold.
  • Local full suite (--check-bounds=yes -t4) on the merge of this PR with the four other read PRs from this sweep: Julia 1.12.6, 333,323 of 333,323 pass; Julia 1.10.11, 332,877 of 332,877. CI green on every test job.
  • codecov/project shows -0.14% to -0.19% on all five read PRs from this sweep, including Inline value parsing for Bool, date-time and time columns #1241, whose diff is one @inline. Codecov counts 6,524 coverable lines on Inline value parsing for Bool, date-time and time columns #1241 against 5,885 on the base b9d8f5e, and an earlier PR on the same base counted 5,894, so the drop comes from how coverage is collected today, not from these diffs. codecov/patch passes (100%).

🤖 Generated with Claude Code

CSV.Chunks validates every cell of a window before the first batch, to
give every batch the same types. It did this one column at a time across
the whole window (64 MiB by default): at most one task per column, and
each task walked the window's bytes and index once more. A two-column
file used two of ten threads, and a wide file streamed the window from
memory once per column.

Each index chunk now validates all of its selected columns while its
bytes are in cache, and the chunks of a window run in parallel. Every
chunk starts from the window's incoming types and widens a column only
as far as its own cells need; the window's type is the join of the
chunks' types. A chunk that settled at nanosecond timestamps is checked
again when the window widens to microseconds, as the column walk did by
restarting. The settled types, missing flags and text widths are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.92%. Comparing base (b9d8f5e) to head (853ec06).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1243      +/-   ##
==========================================
- Coverage   96.05%   95.92%   -0.14%     
==========================================
  Files           9        9              
  Lines        5885     6553     +668     
==========================================
+ Hits         5653     6286     +633     
- Misses        232      267      +35     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant