You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/08/28 13:22:38 UTC
[groovy] branch GROOVY_3_0_X updated: Add option
`groovy.antlr4.sll.threshold` for better parsing performance
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 03f9d07 Add option `groovy.antlr4.sll.threshold` for better parsing performance
03f9d07 is described below
commit 03f9d075b167bc5e04c39df5ed3f27e59c99d61a
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Aug 28 20:20:43 2021 +0800
Add option `groovy.antlr4.sll.threshold` for better parsing performance
(cherry picked from commit be4058271cc538dabf607cef8052cc3f0df83419)
---
.../apache/groovy/parser/antlr4/AstBuilder.java | 26 ++++++++++++++++------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index e990877..cd1bd60 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -39,6 +39,7 @@ import org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy;
import org.apache.groovy.parser.antlr4.internal.atnmanager.AtnManager;
import org.apache.groovy.parser.antlr4.util.StringUtils;
import org.apache.groovy.util.Maps;
+import org.apache.groovy.util.SystemUtil;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.antlr.EnumHelper;
import org.codehaus.groovy.ast.ASTNode;
@@ -207,14 +208,23 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
// parsing have to wait util clearing is complete.
AtnManager.READ_LOCK.lock();
try {
- result = buildCST(PredictionMode.SLL);
- } catch (Throwable t) {
- // if some syntax error occurred in the lexer, no need to retry the powerful LL mode
- if (t instanceof GroovySyntaxError && GroovySyntaxError.LEXER == ((GroovySyntaxError) t).getSource()) {
- throw t;
- }
+ if (SLL_THRESHOLD > 0 && parser.getInputStream().size() > SLL_THRESHOLD) {
+ // The more tokens to parse, the more possibility SLL will fail and the more parsing time will waste.
+ // The option `groovy.antlr4.sll.threshold` could be tuned for better parsing performance, but it is disabled by default.
+ // if the token count is greater than `groovy.antlr4.sll.threshold`, use ALL directly.
+ result = buildCST(PredictionMode.LL);
+ } else {
+ try {
+ result = buildCST(PredictionMode.SLL);
+ } catch (Throwable t) {
+ // if some syntax error occurred in the lexer, no need to retry the powerful LL mode
+ if (t instanceof GroovySyntaxError && GroovySyntaxError.LEXER == ((GroovySyntaxError) t).getSource()) {
+ throw t;
+ }
- result = buildCST(PredictionMode.LL);
+ result = buildCST(PredictionMode.LL);
+ }
+ }
} finally {
AtnManager.READ_LOCK.unlock();
}
@@ -4462,6 +4472,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
private int visitingAssertStatementCount;
private int visitingArrayInitializerCount;
+ private static final int SLL_THRESHOLD = SystemUtil.getIntegerSafe("groovy.antlr4.sll.threshold", -1);
+
private static final String QUESTION_STR = "?";
private static final String DOT_STR = ".";
private static final String SUB_STR = "-";