Skip to content

parser: scan UTF-8/ASCII input without StringScanner or Regexp - #392

Draft
gurpreetg wants to merge 5 commits into
ruby:mainfrom
gurpreetg:pure-ruby-scanner
Draft

gurpreetg wants to merge 5 commits into
ruby:mainfrom
gurpreetg:pure-ruby-scanner

Conversation

@gurpreetg

@gurpreetg gurpreetg commented Nov 26, 2026 •

Copy link
Copy Markdown

Draft, to get feedback on the approach.

For UTF-8 and ASCII input with ASCII separators and quote, CSV::Parser now scans with a small pure-Ruby ByteScanner (getbyte, byteindex, byteslice) instead of StringScanner and Regexp. The goal is to parse untrusted input without strscan or Onigmo running over it; this path builds no Regexp.

Other encodings, non-ASCII separators, Ruby < 3.2 and strip: true without a quote char keep the current path. CSV_PARSER_BYTE_SCANNER=no turns it off.

Commits

  1. Use skip where the match isn't used (~18% faster on main's path by itself).
  2. Add ByteScanner, linear time, tested against the equivalent Regexp.
  3. Tune for the interpreter, YJIT and ZJIT.
  4. Read a run of plain fields in one call.
  5. Build no Regexp on the byte path.

Performance

CSV.parse vs main, Ruby 4.0, arm64. "same" is within 5%.

Input No JIT YJIT ZJIT
20k rows, unquoted same same same
20k rows, all quoted 10% faster same same
20k rows, escaped quotes 8% slower 2.0x faster 1.4x faster
5k rows, long quoted text 6% faster 1.1x faster same
20k rows, long unquoted fields 10% slower 2.2x faster 1.3x faster
1 row, 100k × 30-byte fields 1.5x faster 1.8x faster 1.8x faster
1 row, 200k × 1-byte fields 1.7x faster 1.3x faster 1.7x faster

Medians of 3 alternating runs, each the best of 7 parses.

Testing

  • All 545 tests pass under the interpreter, YJIT and ZJIT, with the byte path on and off, with and without CSV_PARSER_SCANNER_TEST.
  • A differential fuzz against the StringScanner path (80k inputs, 24 option sets, string/IO/parse_line) found no mismatches.

Notes

  • Bug on main: with quote_char: nil, strip: true, "a , b,c" parses as one field. This PR leaves that combination on the old path; the fix belongs in its own PR.
  • Open questions: is the no-JIT cost on escaped-quote rows acceptable, or should this be opt-in / JIT-only? Keep CSV_PARSER_BYTE_SCANNER as the switch?

🤖 Generated with Claude Code

gurpreetg and others added 5 commits November 26, 2026 13:38
Separators, strip runs, escape checks and runs of quotes are scanned only
for their length. StringScanner#skip answers that without building a
String, so the robust path allocates less: about 18% faster on rows with
escaped quotes (`"a""b"`) without YJIT.

Runs of quotes are rebuilt from @quote_character, whose byte size turns
the skipped length into a count of quotes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the input is UTF-8, US-ASCII or ASCII-8BIT and the column
separator, row separator, quote character and strip characters are
ASCII, the parser's patterns are now answered by CSV::Parser::BytePattern
and CSV::Parser::ByteScanner, which compare bytes with String#getbyte and
search with String#byteindex. Other encodings and non-ASCII separators
keep using StringScanner and Regexp, as do Rubies without
String#byteindex (before 3.2). CSV_PARSER_BYTE_SCANNER=no turns the byte
path off.

In these encodings an ASCII byte is always an ASCII character, so where
a byte run stops is where the equivalent Regexp match stops.
/[^...]+/ with several stop bytes keeps each stop's next position, so a
String is searched once per stop byte however many fields it has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Keep run_except stop positions in the scanner and search them inline
  in scan, so that ZJIT compiles the search with profiles.
- Read a short unquoted value's bytes before searching for its end;
  after a long value the search goes first.
- Branch on the stop table instead of negating it: `!` on true and
  false is a polymorphic call for ZJIT.
- scan checks run_except first and byte_until second.
- Keep the String's size, and drop kept positions when pos moves back.
- ByteKeepScanner#each_line sets the position without checking it.
- A quoted value with no escaped quotes is the scanned String itself,
  and an escaped quote is appended without building a String for it.
- Test a quote count's parity with `== 0` instead of Integer#zero?.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The byte path still built the Regexp patterns of the StringScanner path
and then replaced them, and two cases still matched a Regexp against
the input: String#split around stripped separators or a " " column
separator, and the trailing strip of unquoted values.

Build the Regexp patterns only for the StringScanner path, split those
lines with String#byteindex, strip trailing bytes with
BytePattern#trailing_length, and check strip against col_sep without a
Regexp.

Without a quote character, strip: true splits with a pattern that
isn't a character class, so it rarely splits. That combination keeps
the StringScanner path, so its behavior doesn't change.

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

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