Repository navigation
Replace StringScanner with plain Ruby scanning - #2150
Draft
girachawda wants to merge 3 commits into
Draft
girachawda wants to merge 3 commits into
girachawda wants to merge 3 commits into
Conversation
Liquid needs the strscan gem only to scan template sources in the
Tokenizer, the Lexer and Expression.parse_number. strscan is a native
extension, so every application that bundles Liquid must lock, build
and install it for a few byte operations.
The Tokenizer now finds "{{", "{%" and "%}" with String#byteindex and
reads single bytes with String#getbyte. In UTF-8, US-ASCII and binary
sources an ASCII byte is always an ASCII character, so a byte search
finds the same delimiters as the old regular expressions. Other
encodings keep the regular expression search. The Lexer scans
whitespace, identifiers, numbers and strings with byte tables, and
Expression.parse_number reads bytes from the markup.
The tokens, the errors and their messages do not change. The
string_scanner arguments stay optional and unused, and Lexer.tokenize,
Lexer.raise_syntax_error and Parser.new still take a StringScanner and
move it as before. Liquid::HAS_STRING_SCANNER_SCAN_BYTE is removed, and
the gem requires Ruby 3.2 for String#byteindex.
Co-authored-by: Gira Chawda <gira.chawda@shopify.com>
main moved CI to ruby/setup-ruby v1.338.0 and .ruby-version to 4.0.9. Neither touches the files this branch changes. Co-authored-by: Gira Chawda <gira.chawda@shopify.com>
Resolve the conflict with Shopify#2148: keep its valid_encoding? check in Tokenizer#initialize and drop this branch's invalid-source handling in the tokenizer, which can no longer run. Keep both sets of tokenizer tests, minus the two that pinned the old ArgumentError and invalid-variable behaviour. Co-authored-by: Gira Chawda <gira.chawda@shopify.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.
Why
Liquid depends on the
strscangem only for its tokenizer, its lexer andExpression.parse_number.strscanis a native extension. Applications that bundle Liquid must lock, build and install it, although these three scanners need only a few byte operations. This change does the same work with plain RubyStringmethods (getbyte,byteindex,byteslice), so Liquid no longer needsstrscan. Under YJIT the plain Ruby code is also faster.What changes
Tokenizerfinds{{,{%and%}withString#byteindexand reads single bytes withString#getbyte. For sources in UTF-8, US-ASCII or binary, an ASCII byte is always an ASCII character, so a byte search finds the same delimiters as the old regular expression search. For other encodings the tokenizer still uses the old regular expressions, throughString#byteindex.Lexer.tokenizescans whitespace, identifiers, numbers and strings byte by byte with lookup tables, as the old jump tables already did for the first byte.Expression.parse_numberreadsmarkup.getbyteinstead of a reset scanner.ParseContextandContextno longer create a sharedStringScanner.liquid.gemspecdropsstrscanand requires Ruby 3.2, the first version withString#byteindex. CI already tests 3.3 and later.Compatibility
The behavior stays the same, including the errors and their messages:
Liquid::SyntaxErrorfromTokenizer.new, asmaindoes since fix(tokenizer): raise Liquid::SyntaxError for invalid UTF-8 whatever the first token is #2148, and the lexer raisesLiquid::SyntaxErrorfor one, as before;Encoding::CompatibilityError, as before;{%inside{{, and multibyte characters next to delimiters give the same tokens as before.The public signatures stay compatible.
Tokenizer.new(string_scanner:), and the scanner arguments ofExpression.parse,Expression.safe_parseandVariableLookup.parse, are optional and not used.RangeLookup.parsekeeps its scanner argument and passes it on unused.Lexer.tokenize,Lexer.raise_syntax_errorandParser.newtake a String or aStringScanner, and a scanner ends at the same position as before.Three changes are visible to callers, and History.md lists them:
Liquid::HAS_STRING_SCANNER_SCAN_BYTEis removed, because Liquid no longer loadsStringScanner. Code that usesStringScannermustrequire "strscan"itself.Lexer.tokenizeraisesTypeErrorfor input that is not a String or a scanner. It raisedNoMethodErrorbefore;Parser.newalready raisedTypeError.This branch is merged with
mainafter #2148, which makes the tokenizer raiseLiquid::SyntaxErrorfor every source with invalid bytes. The merge keeps #2148'svalid_encoding?check inTokenizer#initializeand drops this branch's own invalid-source handling in the tokenizer (a rescue and two regular expression calls), which could no longer run. It keeps #2148's tests and drops the two tests of this branch that pinned the old behavior.Validation
Tokenizer,Lexer(with a String and with aStringScanner) andExpression.parse_numberon 1,100,000 random inputs: UTF-8, binary, US-ASCII, Shift_JIS (including characters whose second byte is{or}), EUC-JP, UTF-16LE and invalid UTF-8. It compared tokens, token encodings, line numbers, error classes, error messages and their encodings, and the final scanner position. There were no differences.mainand with this branch, and compared the output, warnings and errors of all 100,170 cases. There were no differences.mainwithout it.strscan.Benchmarks
Ruby 4.0.9, the median time of alternating runs of
mainand this branch, in separate processes, on theperformance/teststemplates. A negative change is faster. The host was shared and loaded (load average about 50 on 4 CPUs), so a change within about ±10% in one case is noise:render corpusruns no changed code except 14Expression.parsecalls, and a 5-round YJIT run measured it +12.8% and an 8-round run −4.5%.main→ branchYJIT figures are from 5 rounds unless noted. The malformed templates are unterminated variables, tags and strings, 5,000
{, an invalid byte, 300 unclosediftags and a bad filter. Each benchmark sampled 5 timings a round, and the allocations are per iteration.