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/21 03:22:31 UTC

[groovy] branch danielsun/tune-parser-performance updated: Try to fix the failing build

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


The following commit(s) were added to refs/heads/danielsun/tune-parser-performance by this push:
     new dc13381  Try to fix the failing build
dc13381 is described below

commit dc13381d81003ac5aa5c5ab388a2e94b97f3fd7c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Aug 21 11:19:41 2021 +0800

    Try to fix the failing build
---
 .../groovy/parser/antlr4/Antlr4ParserPlugin.java   | 36 ++++++++++++++++------
 1 file changed, 27 insertions(+), 9 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 645f062..4184e6f 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
@@ -32,6 +32,11 @@ import java.io.IOException;
 import java.io.Reader;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 /**
  * A parser plugin for the new parser.
@@ -55,26 +60,28 @@ public class Antlr4ParserPlugin implements ParserPlugin {
 
     @Override
     public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) {
-        CompletableFuture<ModuleNode> allFuture = buildAstAsync(sourceUnit, AstBuilder.ALL);
         SourceUnit su = new SourceUnit(sourceUnit.getName(), sourceUnit.getSource(), sourceUnit.getConfiguration(),
-                                        sourceUnit.getClassLoader(), new ErrorCollector(sourceUnit.getConfiguration()));
+                sourceUnit.getClassLoader(), new ErrorCollector(sourceUnit.getConfiguration()));
         CompletableFuture<ModuleNode> sllFuture = buildAstAsync(su, AstBuilder.SLL);
+        CompletableFuture<ModuleNode> allFuture = buildAstAsync(sourceUnit, AstBuilder.ALL);
 
         try {
-            ModuleNode moduleNode = sllFuture.get();
-            try {
-                allFuture.cancel(true);
-            } catch (Throwable ignored) {
-            }
+            ModuleNode moduleNode = sllFuture.get(10, TimeUnit.SECONDS);
+//            try {
+//                allFuture.cancel(true);
+//            } catch (Throwable ignored) {
+//            }
             moduleNode.setContext(sourceUnit);
             return moduleNode;
         } catch (Throwable ignored) {
         }
 
         try {
-            return allFuture.get();
+            return allFuture.get(10, TimeUnit.SECONDS);
         } catch (InterruptedException e) {
             throw new GroovyBugError("Interrupted while parsing", e);
+        } catch (TimeoutException e) {
+            throw new GroovyBugError("Timeout while parsing", e);
         } catch (ExecutionException e){
             throw (CompilationFailedException) e.getCause();
         }
@@ -88,6 +95,17 @@ public class Antlr4ParserPlugin implements ParserPlugin {
                     predictionMode
             );
             return builder.buildAST();
-        });
+        }, THREAD_POOL);
     }
+
+    private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {
+        private int seq;
+
+        @Override
+        public Thread newThread(Runnable r) {
+            Thread t = new Thread(r);
+            t.setName("parser-thread-" + seq++);
+            return t;
+        }
+    });
 }