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.

Summary

For UTF-8 and ASCII input (UTF-8, US-ASCII, ASCII-8BIT) with ASCII separators and quote, CSV::Parser now scans with a small pure-Ruby scanner, ByteScanner, instead of StringScanner and Regexp. It only uses String#getbyte, #byteindex and #byteslice.

The goal is to parse untrusted input without the strscan extension or Onigmo running over it. On this path the parser builds no Regexp at all. All the patterns the parser needs are literals or runs of ASCII bytes. In these encodings an ASCII byte is always a whole character, so a byte loop stops exactly where the Regexp would.

Everything else keeps the current StringScanner path:

  • other encodings;
  • non-ASCII separators;
  • Ruby < 3.2, which has no String#byteindex;
  • strip: true without a quote character (see Notes).

CSV_PARSER_BYTE_SCANNER=no turns the byte path off.

Commits

  1. Use skip where the matched text isn't used. This one stands alone: it makes the current strscan path ~18% faster on rows with escaped quotes.
  2. Add ByteScanner. For a run that stops at any of several bytes, it remembers where each stop byte next occurs, so parsing stays linear. The new tests check every pattern against the equivalent Regexp at every position of each input.
  3. Tune it for the interpreter, YJIT and ZJIT. These changes came from JIT exit stats and ZJIT's HIR. They also cut allocations.
  4. Read a run of plain fields in one call (scan_separated). This removes the per-field Ruby calls on rows that contain escaped quotes.
  5. Build no Regexp on the byte path. Splitting around strip or a " " separator, and trailing strip, now use bytes too.

Performance

CSV.parse time on Ruby 4.0, arm64, relative to main. Below 1.00 means faster.

Input Interpreter YJIT ZJIT
20k rows, unquoted 0.96 0.97 0.99
20k rows, all quoted 0.90 0.98 1.00
20k rows, escaped quotes 1.08 0.50 0.69
5k rows, long quoted text 0.94 0.89 0.98
20k rows, long unquoted fields 1.10 0.46 0.78
1 row, 100k × 30-byte fields 0.69 0.54 0.57
1 row, 200k × 1-byte fields 0.57 0.75 0.59

The ratios are medians of 3 alternating runs, each the best of 7 parses.

  • Under YJIT and ZJIT it is as fast as main or faster on every input.
  • Without a JIT, two inputs are slightly slower (8–10%). Both are rows with escaped quotes, where each quoted field still costs a few Ruby calls.

Testing

  • run-test.rb passes (545 tests) in four configurations, under the interpreter, YJIT and ZJIT:
    • default;
    • CSV_PARSER_SCANNER_TEST=yes;
    • CSV_PARSER_BYTE_SCANNER=no;
    • CSV_PARSER_BYTE_SCANNER=no with CSV_PARSER_SCANNER_TEST=yes.
  • A differential fuzz against the StringScanner path found no mismatches in 80k random inputs. It covers string, IO and parse_line input, 24 option sets (strip, col_sep: " ", quote_char: nil, liberal parsing and more), quotes, CR/LF, long runs and multibyte text.

Notes

  • Found while doing this, on main: with quote_char: nil, strip: true, the split pattern is / \t\f\v*,... rather than a character class, so "a , b,c" parses as one field. This PR keeps that behavior by leaving the combination on the StringScanner path. The fix belongs in its own PR.
  • Not caused by this PR: on main too, the first CSV::Parser gets a larger heap slot than later ones. Code ZJIT compiles during the first parse then fails its shape guards on every later parse. A warm-up parse avoids it.

Open questions

  • Is the 8–10% no-JIT cost on escaped-quote rows acceptable? Or should the byte path be opt-in, or enabled only under a JIT?
  • Keep CSV_PARSER_BYTE_SCANNER as the switch, or use something else?

🤖 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