Skip to content

Commit 1db8572

Browse files
committed
perf: skip redundant lookahead simulations in Condition dispatch and expression lists
The main branch of Condition/SQLCondition is the last alternative of its choice: entering it and letting the productions reject is equivalent to the deep jj_2 phase simulation that ran for every plain condition first, so gate it with a cheap !interrupted check instead. The named-parameter alternatives of ComplexExpressionList only ever match a single-token name directly followed by their assignment token, so a first check on the second token is equivalent to their 2-token lookahead scan. parseSQLStatements: 21.3-21.4 vs 24.6-24.8 ms/op against master in 3 interleaved JMH pairs (-13% to -14%, error intervals not overlapping); behavior verified identical by 26 EXISTS/named-parameter probes, the full 10,344-test suite (same 35 pre-existing environment failures as master) and a byte-identical AST dump of performance.sql. Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent bdf8ac3 commit 1db8572

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,17 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
12651265
}
12661266
}
12671267

1268+
/**
1269+
* Both named-parameter forms match only a single-token name (or K_OUTER) directly followed
1270+
* by their assignment token; the 2-token lookahead fails for anything else, so this first
1271+
* check is equivalent to running that scan.
1272+
*/
1273+
private boolean isNamedParameterAssignmentAhead() {
1274+
int kind = getToken(2).kind;
1275+
return kind == K_ORACLE_NAMED_PARAMETER_ASSIGNMENT
1276+
|| kind == K_POSTGRES_NAMED_PARAMETER_ASSIGNMENT;
1277+
}
1278+
12681279
/** Distinguish EXISTS identifiers from predicates before suffixes and clause boundaries. */
12691280
private boolean isExistsIdentifierAhead() {
12701281
if (getToken(1).kind != K_EXISTS) {
@@ -11048,6 +11059,10 @@ Expression Condition():
1104811059
LOOKAHEAD(2, { !isExistsIdentifierAhead() })
1104911060
result=ExistsExpression()
1105011061
|
11062+
// last alternative of the choice: entering it and letting the productions
11063+
// themselves reject is equivalent to scanning first, so skip the deep
11064+
// lookahead simulation (this path runs for every plain condition)
11065+
LOOKAHEAD({ !interrupted })
1105111066
[ LOOKAHEAD(2, { getToken(1).kind == K_PRIOR }) <K_PRIOR> { oraclePrior = EqualsTo.ORACLE_PRIOR_START; } ]
1105211067
left=SimpleExpression() { result = left; }
1105311068

@@ -11198,7 +11213,10 @@ Expression SQLCondition():
1119811213
|
1119911214
LOOKAHEAD(2, { !isExistsIdentifierAhead() })
1120011215
result=ExistsExpression()
11201-
| left = SimpleExpression() { result = left; }
11216+
|
11217+
// last alternative of the choice: skip the redundant lookahead simulation
11218+
LOOKAHEAD({ !interrupted })
11219+
left = SimpleExpression() { result = left; }
1120211220
[
1120311221
LOOKAHEAD(2) (
1120411222
LOOKAHEAD(2, <K_OVERLAPS>) result=OverlapsCondition(left)
@@ -11663,9 +11681,9 @@ ExpressionList ComplexExpressionList():
1166311681
}
1166411682
{
1166511683
(
11666-
LOOKAHEAD(2) expr=OracleNamedFunctionParameter()
11684+
LOOKAHEAD(2, { isNamedParameterAssignmentAhead() }) expr=OracleNamedFunctionParameter()
1166711685
|
11668-
LOOKAHEAD(2) expr=PostgresNamedFunctionParameter()
11686+
LOOKAHEAD(2, { isNamedParameterAssignmentAhead() }) expr=PostgresNamedFunctionParameter()
1166911687
|
1167011688
expr=Expression()
1167111689
)
@@ -11676,9 +11694,9 @@ ExpressionList ComplexExpressionList():
1167611694
(
1167711695
LOOKAHEAD(2, {!interrupted}) ","
1167811696
(
11679-
LOOKAHEAD(2) expr=OracleNamedFunctionParameter()
11697+
LOOKAHEAD(2, { isNamedParameterAssignmentAhead() }) expr=OracleNamedFunctionParameter()
1168011698
|
11681-
LOOKAHEAD(2) expr=PostgresNamedFunctionParameter()
11699+
LOOKAHEAD(2, { isNamedParameterAssignmentAhead() }) expr=PostgresNamedFunctionParameter()
1168211700
|
1168311701
LOOKAHEAD( RelObjectName() "->", { !isJsonArrowDialect() } ) expr=LambdaExpression()
1168411702
|

0 commit comments

Comments
 (0)