Repository navigation
Conversation
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
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.
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::Parsernow scans with a small pure-Ruby scanner,ByteScanner, instead ofStringScannerandRegexp. It only usesString#getbyte,#byteindexand#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
StringScannerpath:String#byteindex;strip: truewithout a quote character (see Notes).CSV_PARSER_BYTE_SCANNER=noturns the byte path off.Commits
skipwhere the matched text isn't used. This one stands alone: it makes the current strscan path ~18% faster on rows with escaped quotes.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 equivalentRegexpat every position of each input.scan_separated). This removes the per-field Ruby calls on rows that contain escaped quotes.stripor a" "separator, and trailing strip, now use bytes too.Performance
CSV.parsetime on Ruby 4.0, arm64, relative tomain. Below 1.00 means faster.The ratios are medians of 3 alternating runs, each the best of 7 parses.
mainor faster on every input.Testing
run-test.rbpasses (545 tests) in four configurations, under the interpreter, YJIT and ZJIT:CSV_PARSER_SCANNER_TEST=yes;CSV_PARSER_BYTE_SCANNER=no;CSV_PARSER_BYTE_SCANNER=nowithCSV_PARSER_SCANNER_TEST=yes.StringScannerpath found no mismatches in 80k random inputs. It covers string, IO andparse_lineinput, 24 option sets (strip,col_sep: " ",quote_char: nil, liberal parsing and more), quotes, CR/LF, long runs and multibyte text.Notes
main: withquote_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.maintoo, the firstCSV::Parsergets 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
CSV_PARSER_BYTE_SCANNERas the switch, or use something else?🤖 Generated with Claude Code