Skip to content

CSV reading is about 100x slower for cells with doubled-quote escapes: one page mapping per escaped cell #1890

Done

Description

@singaraiona

Summary

A STR cell with a doubled-quote escape ("say ""hi""") costs about 6.5 µs to read, about 100× an ordinary quoted cell: a 520 MB file of 6.5M rows with one such cell per row reads in 32 s; the same file without the escapes reads in 0.4 s.

Repro (dev 2ed4637, release build)

awk 'BEGIN{print "id,note"; for(i=1;i<=2000000;i++) printf "%d,\"n,%d\"\"x\"\n", i, i}' > esc.csv
sed 's/""/x/' esc.csv > noesc.csv
(get (.mem.ts (.csv.read [I64 STR] "esc.csv")) 'time-ns)    ; 4.81 s
(get (.mem.ts (.csv.read [I64 STR] "noesc.csv")) 'time-ns)  ; 0.09 s

(run pinned to one CPU.) With all cores the mappings still dominate: an 8-column file with one escaped cell per row reads in 3.4 / 6.6 / 13.1 s at 0.5M / 1M / 2M rows, 32 s at 6.5M (520 MB), and in 0.11 s at 2M rows without the escapes.

Cause

scan_field_quoted unescapes into a stack buffer, so the STR case of csv_parse_fn copies the cell out to keep it past the row: ray_sys_alloc(flen) per cell. ray_sys_alloc maps whole pages (ray_heap_sys_map, at least 4 KB plus a header), so every escaped cell is one mmap, and csv_free_escaped_strrefs later unmaps each one: millions of map/unmap pairs.

Fix direction

Keep the escaped copies in per-worker bump blocks (a few MB each) and free the blocks after the strings are filled in, instead of one mapping per cell.

Also seen

The quoted-file load is sensitive to code alignment: building src/io/csv.c with -falign-loops=64 read a quoted 520 MB file in 1.25 s against 2.5 s on the same (shared) core with the default flags. Noted only; no build-flag change proposed here.

Activity

  1. singaraiona commented on Nov 28, 2026

    @singaraiona
    CollaboratorAuthor

    Fixed in #1894: each worker keeps its escaped copies in shared 1 MB blocks freed with the load. 2M escaped cells read in 110 ms instead of 4.8 s at one worker (27 ms instead of 9.1 s on all cores), with an anonymous peak of 130 MB instead of 8.3 GB; files without escapes are unchanged within noise.

  2. added 2 commits that reference this issue on Nov 28, 2026
    58a6116
    cf9c7b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions