Repository navigation
Return Rows and lazy text cells without allocating - #1242
Open
quinnj-bot[bot] wants to merge 1 commit into
Open
quinnj-bot[bot] wants to merge 1 commit into
quinnj-bot[bot] wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Reading a text cell from
CSV.Rowsallocated 32 bytes per cell, in the default and strict modes and through every access form (row.b,row[2],row[:b],Tables.getcolumn). Iterating aCSV.lazytext column allocated 192 bytes per cell, and indexing one allocated 32. A 5M-row, 4-columnCSV.Rowsscan 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}. ADataStringholds 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 theDataString's two fields, its payload and its byte vector, withPAYLOAD_MISSINGstanding formissing. Those are concrete types, so the call returns them in registers. A small inlined wrapper (_viewvalue) builds theDataStringormissingat the access site. The readers' bodies are the same code as before (inlined into these functions), so every value is the sameDataStringas 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):
CSV.Rows, every cellCSV.lazy, every cell by iterationThe remaining allocation is the structural index. Empty cells were never boxed, so the 80%-empty file changes least.
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 withCSV.lazyand everyCSV.Fileoption set on the merge of the five read PRs (221 fingerprints, zero differences).DataString(p, data, Val(:unchecked))rebuild uses the payload and buffer of aDataStringthe reader built through the checked constructor, so it skips only a repeated check.PAYLOAD_MISSINGhas 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.@inlineat the call sites needs Julia 1.8+; the floor is 1.10.--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.@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