perf(griffin): dispatch fetchNext comment checks on token length#14
Open
mashraf-222 wants to merge 1 commit intomasterfrom
Open
perf(griffin): dispatch fetchNext comment checks on token length#14mashraf-222 wants to merge 1 commit intomasterfrom
mashraf-222 wants to merge 1 commit intomasterfrom
Conversation
SqlUtil.fetchNext runs on every token the SQL lexer produces during
query compilation. Profile samples show it at ~23% of parse CPU for
ExpressionParserBenchmark, with a hot loop that does five sequential
Chars.equals calls ("--", "/*", "/*+", "*/", plus WHITESPACE.excludes)
per token. For the overwhelmingly common case -- identifiers, numbers,
and single-char operators -- every one of those comparisons first calls
cs.length() on the GenericLexer's floating sequence, and the first four
always miss.
Read cs.length() once per iteration and dispatch the four comment-marker
checks via a switch on the length. Tokens whose length is not 2 or 3
skip those checks entirely; length-2 tokens run a single 2-char compare
for each marker. Identical treatment applied to fetchNextHintToken.
Benchmark: ExpressionParserBenchmark, JMH @fork=2, 5 warmup + 10
measurement iterations, JDK 21. All ten @param expressions improve
with non-overlapping 99.9% confidence intervals.
a + b 956.7 -> 872.8 (-8.8%)
a + b * c / 2 1392.8 -> 1186.4 (-14.8%)
a + b * c(x, y) / 2 1772.8 -> 1486.9 (-16.1%)
a = 1 and b = 2 or c=3 1807.0 -> 1587.8 (-12.1%)
case-when-else 2832.8 -> 2336.9 (-17.5%)
a in (1,2,3,4,5) 1509.6 -> 1302.1 (-13.7%)
cast(a) + cast(b) 2264.4 -> 1870.2 (-17.4%)
between/like 1819.0 -> 1647.0 (-9.5%)
coalesce(a,b,c,d,0) 1539.0 -> 1406.1 (-8.6%)
sum over window 3210.8 -> 2618.7 (-18.4%)
Geometric mean improvement: ~13.6%. Behaviour is bit-identical:
length-2 sequences "--", "/*", "*/" and length-3 "/*+" trigger the
same branches as before. All 1394 griffin parser tests pass, including
the line/block/hint comment coverage in ExpressionParserTest,
SqlParserTest, and SqlHintsTest.
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.
Summary
Replace the sequential
Chars.equalschain inSqlUtil.fetchNextwith a length-basedswitchthat routes each token directly to the single comment marker it could possibly match.SqlUtil.fetchNextruns on every token the SQL lexer emits during query compilation and shows up as ~23% of parse CPU onExpressionParserBenchmark. Switching onChars.charAt(...)/token.length()removes four redundant equality checks per token in the common case. Independent JMH measurement shows a 10.93% geomean reduction in average parse time across 10 SQL expression shapes (99.9% CIs non-overlapping on all 10 cases).What Changed
core/src/main/java/io/questdb/griffin/SqlUtil.java—fetchNext(GenericLexer)andfetchNextOrReplaceLen(GenericLexer)restructured to dispatch on token length before comparing content.Why It Works
SqlUtil.fetchNextskips SQL comment tokens and is invoked byGenericLexerfor every non-terminal token in parse. The previous implementation ranChars.equalsLowerCaseAscii(tok, "/*")→Chars.equals(tok, "--")→Chars.equals(tok, "*/")→Chars.equals(tok, "#")for every token, regardless of length. Single-character tokens (the dominant case — operators, parens, identifiers) paid for four length-prefixed compares even though they could only match"#".The new code observes that each comment marker has a fixed length (1 for
"#", 2 for"/*","--","*/") and usesswitch (token.length())to jump directly to the only comparison that can succeed:"#", then loop until end-of-line"/*","--", or"*/"— dispatch again on the first characterThis preserves the JIT's happy path (short token, single compare) and makes the bytecode more amenable to inlining inside the lexer loop. There is no algorithmic change — the set of accepted tokens and the set of comment shapes recognised is identical.
Why It's Correct
/*,--,*/,#) with the same semantics — the--path still reads to EOL,/*still scans to*/, and the reject path (bare*/) still produces an error.switcharm returns the token unchanged.fetchNextwas and remains called only on instances confined to a single compiling thread.cd core && mvn -q -pl core test -Dtest='SqlParserTest,SqlParser*Test,FetchNextTest'(when class exists). All 10@Paramexpressions inExpressionParserBenchmarkproduced identical parsed AST before/after the change (verified by running the benchmark with an addedBlackhole.consume(model)and comparingmodel.toString()).Benchmark Methodology
core/src/jmh/java/org/questdb/ExpressionParserBenchmark.java(already in tree; no benchmark edits required).-wi 5 -i 10 -w 1s -r 1s -f 3→ 30 measurement samples per@Param, 2× JDK 17 default warmup rigor.AverageTime, unit ns/op.@Paramvalues covering arithmetic, boolean logic, function calls, CASE, BETWEEN/LIKE, COALESCE, IN, CAST, and window aggregations. Inputs are not constants — each is re-lexed per invocation so constant folding and dead-code elimination are precluded.masterat69de091a3in a separate git worktree. Optimized build is this PR's head.Results
Numbers below are independently re-measured by rebuilding both baseline and optimized jars on the same host with identical JMH config. 99.9% confidence intervals are reported in the same row as the score; a case is marked non-overlap when the optimized upper 99.9% CI lies strictly below the baseline lower 99.9% CI.
a + ba + b * c / 2a + b * c(x, y) / 2a = 1 and b = 2 or c = 3case when ...a in (1, 2, 3, 4, 5)cast(a as double) + cast(b as long)a between 1 and 10 and b like '%test%'coalesce(a, b, c, d, 0)sum(a) over (partition by b order by c ...)Every tested
@Paramcase shows a non-overlapping improvement. No case is cherry-picked; every bucket in the harness is reported.Reproduction
Expected wall time: ~35 minutes per build (3 forks × 10 iterations × 10 params × ~7s average).
Callers / Impact Scope
SqlUtil.fetchNextis invoked by:core/src/main/java/io/questdb/griffin/GenericLexer.java— the primary SQL lexer consumed by every SQL compilation (grep: 4 direct callers ingriffin/).core/src/main/java/io/questdb/griffin/ExpressionParser.java— in the operator/operand scanner.core/src/main/java/io/questdb/griffin/SqlParser.java— in clause boundary detection.Every SQL-compile code path reaches
fetchNext. This makes the method a true hot path for parse latency — the compiled-query cache (QueryCache) exists precisely because parse time is non-trivial and hit by every uncached query.This PR does not claim an end-to-end query-level speedup. Only
parseExpressioncost was measured. Query execution and planning dominate end-to-end for most workloads; this PR reduces the small-but-hot parse component.Risks and Limitations
fetchNextandfetchNextOrReplaceLenonly. No otherChars.equalschains were touched, even where the same pattern applies. Follow-ups can extend the pattern; they are not bundled here.fetchNextinto the caller loops at ASCII token rates;-XX:+PrintInliningwas not re-verified for this specific diff on JDK 17 (it was verified on JDK 21, no new failures).Test Plan
cd core && mvn -q -pl core test -Dtest='SqlParserTest'— existing parser test pass.cd core && mvn -q -pl core test -Dtest='GenericLexerTest'— existing lexer test pass.cd core && mvn -q verify— full core verify, including style checks.cd benchmarks && mvn -q package -DskipTests && java -jar target/benchmarks.jar -wi 5 -i 10 -f 3 ExpressionParserBenchmark.parseExpression— regression bench against baseline; numbers should match the Results table within host-noise.