Skip to content

Return Rows and lazy text cells without allocating - #1242

Open
quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/rows-inline-cells
Open

quinnj-bot[bot] wants to merge 1 commit into
mainfrom
claude/rows-inline-cells

Conversation

@quinnj-bot

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

Copy link
Copy Markdown
Contributor

Why

Reading a text cell from CSV.Rows allocated 32 bytes per cell, in the default and strict modes and through every access form (row.b, row[2], row[:b], Tables.getcolumn). Iterating a CSV.lazy text column allocated 192 bytes per cell, and indexing one allocated 32. A 5M-row, 4-column CSV.Rows scan allocated 773 MB beyond the source and its index.

The cause: each of these readers returned the cell through a function call typed Union{Missing, DataString}. A DataString holds a reference to its bytes, so that union is not bits-only, and Julia boxes the value on the heap at every such call.

What changed

The cell readers (_cellview, _strictview, _lazyview, _lazynextview) now return the DataString's two fields, its payload and its byte vector, with PAYLOAD_MISSING standing for missing. Those are concrete types, so the call returns them in registers. A small inlined wrapper (_viewvalue) builds the DataString or missing at the access site. The readers' bodies are the same code as before (inlined into these functions), so every value is the same DataString as before. Typed cells (types=Int, ...) already returned bits-only unions and are unchanged. A separate PR ("Hold the Rows reader by reference in every row view") shrinks the row view these calls receive; the two are independent and merge cleanly.

Verification

Single task, calling-thread cycles, new/main, median of 5 interleaved fresh-process rounds (Apple M3 Max, Julia 1.12.6, load average 7 to 31 from other jobs):

file CSV.Rows, every cell allocated CSV.lazy, every cell by iteration allocated
5M × 4 integers (read as text) 0.881× 773 → 133 MB 0.220× (1.79 s → 0.38 s) 3.97 GB → 133 MB
5M × 4 short text 0.884× 774 → 134 MB 0.274×
1M × 4 long text 0.960× 158 → 30 MB 0.513×
2M × 4 quoted text 0.846× 355 → 99 MB 0.270×
5M × 4, 80% empty 0.986× 260 → 132 MB 0.178×
10M × 2 mixed 0.816× 814 → 174 MB 0.239×
100k × 500 0.885× 1.86 GB → 257 MB 0.649× 9.83 GB → 230 MB

The remaining allocation is the structural index. Empty cells were never boxed, so the 80%-empty file changes least.

  • Full-content fingerprints (column types and every value) of CSV.Rows (default, strict, stringtype=String, typed, typed strict) 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 on this branch, and together with CSV.lazy and every CSV.File option set on the merge of the five read PRs (221 fingerprints, zero differences).
  • New test: text cells through all six access forms in both modes, and a lazy text column by index and by iteration, allocate the same for 5,000 rows as for 500. It fails on main (13 of 60: 32 or 192 bytes per cell) and passes on this branch, on Julia 1.12 and 1.10.
  • Adversarial read of the diff: the DataString(p, data, Val(:unchecked)) rebuild uses the payload and buffer of a DataString the reader built through the checked constructor, so it skips only a repeated check. PAYLOAD_MISSING has length -1, which no cell payload has. The lazy iterator marks its end with a negative count in the state, which a live state never has. _rowcell(::Type{DataString}, ..., ::Val{:error}) resolves the ambiguity with the generic strict method. @inline at the call sites needs Julia 1.8+; the floor is 1.10.
  • 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

A call that returns Union{Missing, DataString} heap-allocates the value,
because the union holds a struct with a reference. Every Rows text cell
(default and strict modes, every access form) and every lazy text cell
crossed such a call, so reading a cell allocated 32 bytes, and iterating
a lazy text column allocated 192 bytes per cell.

The cell readers now return the DataString's payload and buffer, with
PAYLOAD_MISSING for missing, and a small inlined wrapper builds the
DataString or missing at the access site. Values 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.89%. Comparing base (b9d8f5e) to head (e955e25).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1242      +/-   ##
==========================================
- Coverage   96.05%   95.89%   -0.17%     
==========================================
  Files           9        9              
  Lines        5885     6557     +672     
==========================================
+ Hits         5653     6288     +635     
- Misses        232      269      +37     

☔ 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