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 12:21:28 UTC
[groovy] 01/01: 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 danielsun/parrot-statistics
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit d300ef8df14a151803807599b964834b8cca73cf
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
---
.../apache/groovy/parser/antlr4/AstBuilder.java | 26 ++++++++++++++++------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 14e2380..53adba7 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -210,6 +210,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;
@@ -404,14 +405,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 waste lots of parsing time.
+ // 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 `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();
}
@@ -5031,6 +5041,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 = "-";