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/20 18:18:39 UTC

[groovy] branch danielsun/tune-parser-performance created (now 2f6356b)

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a change to branch danielsun/tune-parser-performance
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 2f6356b  Tune the performance of parrot parser

This branch includes the following new commits:

     new 2f6356b  Tune the performance of parrot parser

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[groovy] 01/01: Tune the performance of parrot parser

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch danielsun/tune-parser-performance
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 2f6356bd15b2b9cbd3c668d71873bc423dc91520
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Aug 21 02:18:16 2021 +0800

    Tune the performance of parrot parser
---
 .../groovy/parser/antlr4/Antlr4ParserPlugin.java   | 42 +++++++++++++++++++---
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 37 ++++++++++++++-----
 2 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java b/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
index 819165e..9436593 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
@@ -20,6 +20,8 @@ package org.apache.groovy.parser.antlr4;
 
 import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.ModuleNode;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.ErrorCollector;
 import org.codehaus.groovy.control.ParserPlugin;
 import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.control.io.StringReaderSource;
@@ -28,6 +30,8 @@ import org.codehaus.groovy.syntax.Reduction;
 
 import java.io.IOException;
 import java.io.Reader;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 
 /**
  * A parser plugin for the new parser.
@@ -51,10 +55,38 @@ public class Antlr4ParserPlugin implements ParserPlugin {
 
     @Override
     public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) {
-        AstBuilder builder = new AstBuilder(sourceUnit,
-                sourceUnit.getConfiguration().isGroovydocEnabled(),
-                sourceUnit.getConfiguration().isRuntimeGroovydocEnabled()
-        );
-        return builder.buildAST();
+        CompletableFuture<ModuleNode> allFuture = buildAstAsync(sourceUnit, AstBuilder.ALL);
+        SourceUnit su = new SourceUnit(sourceUnit.getName(), sourceUnit.getSource(), sourceUnit.getConfiguration(),
+                                        sourceUnit.getClassLoader(), new ErrorCollector(sourceUnit.getConfiguration()));
+        CompletableFuture<ModuleNode> sllFuture = buildAstAsync(su, AstBuilder.SLL);
+
+        try {
+            ModuleNode moduleNode = sllFuture.get();
+            try {
+                allFuture.cancel(true);
+            } catch (Throwable ignored) {
+            }
+            return moduleNode;
+        } catch (Throwable ignored) {
+        }
+
+        try {
+            return allFuture.get();
+        } catch (InterruptedException e) {
+            throw new GroovyBugError("Interrupted while parsing", e);
+        } catch (ExecutionException e){
+            throw (CompilationFailedException) e.getCause();
+        }
+    }
+
+    private CompletableFuture<ModuleNode> buildAstAsync(final SourceUnit sourceUnit, final int predictionMode) {
+        return CompletableFuture.supplyAsync(() -> {
+            AstBuilder builder = new AstBuilder(sourceUnit,
+                    sourceUnit.getConfiguration().isGroovydocEnabled(),
+                    sourceUnit.getConfiguration().isRuntimeGroovydocEnabled(),
+                    predictionMode
+            );
+            return builder.buildAST();
+        });
     }
 }
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 9a7aa5d..256ca32 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -370,7 +370,15 @@ import static org.codehaus.groovy.runtime.DefaultGroovyMethods.last;
  * Builds the AST from the parse tree generated by Antlr4.
  */
 public class AstBuilder extends GroovyParserBaseVisitor<Object> {
+    public static final int SLL = 0;
+    public static final int ALL = 1;
+    public static final int AUTO = -1;
+
     public AstBuilder(final SourceUnit sourceUnit, final boolean groovydocEnabled, final boolean runtimeGroovydocEnabled) {
+        this(sourceUnit, groovydocEnabled, runtimeGroovydocEnabled, AUTO);
+    }
+
+    public AstBuilder(final SourceUnit sourceUnit, final boolean groovydocEnabled, final boolean runtimeGroovydocEnabled, final int predictionMode) {
         this.sourceUnit = sourceUnit;
         this.moduleNode = new ModuleNode(sourceUnit);
         CharStream charStream = createCharStream(sourceUnit);
@@ -381,6 +389,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
 
         this.groovydocManager = new GroovydocManager(groovydocEnabled, runtimeGroovydocEnabled);
         this.tryWithResourcesASTTransformation = new TryWithResourcesASTTransformation(this);
+
+        this.predictionMode = predictionMode;
     }
 
     private CharStream createCharStream(final SourceUnit sourceUnit) {
@@ -404,14 +414,24 @@ 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 == predictionMode) {
+                    result = buildCST(PredictionMode.SLL);
+                } else if (ALL == predictionMode) {
+                    result = buildCST(PredictionMode.LL);
+                } else if (AUTO == predictionMode) {
+                    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);
+                    }
+                } else {
+                    throw new GroovyBugError("Unknown prediction mode: " + predictionMode);
+                }
             } finally {
                 AtnManager.READ_LOCK.unlock();
             }
@@ -424,11 +444,11 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
 
     private GroovyParserRuleContext buildCST(final PredictionMode predictionMode) {
         parser.getInterpreter().setPredictionMode(predictionMode);
+        parser.getInputStream().seek(0);
 
         if (PredictionMode.SLL.equals(predictionMode)) {
             this.removeErrorListeners();
         } else {
-            parser.getInputStream().seek(0);
             this.addErrorListeners();
         }
 
@@ -5020,6 +5040,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
     private final GroovyLangParser parser;
     private final GroovydocManager groovydocManager;
     private final TryWithResourcesASTTransformation tryWithResourcesASTTransformation;
+    private final int predictionMode;
 
     private final List<ClassNode> classNodeList = new LinkedList<>();
     private final Deque<ClassNode> classNodeStack = new ArrayDeque<>();