groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Minor refactoring for atn managers
Date Fri, 21 Sep 2018 22:47:05 GMT
Repository: groovy
Updated Branches:
  refs/heads/master be7d6aa4e -> 6cc11028c


Minor refactoring for atn managers


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6cc11028
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6cc11028
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6cc11028

Branch: refs/heads/master
Commit: 6cc11028c516fe4a5dcdfed8fdb41c56d0142c48
Parents: be7d6aa
Author: Daniel Sun <sunlan@apache.org>
Authored: Sat Sep 22 06:45:39 2018 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Sat Sep 22 06:45:39 2018 +0800

----------------------------------------------------------------------
 subprojects/parser-antlr4/README.adoc           |   1 +
 .../apache/groovy/parser/antlr4/AstBuilder.java |   6 +-
 .../groovy/parser/antlr4/GroovyLangLexer.java   |  23 +---
 .../groovy/parser/antlr4/GroovyLangParser.java  |   4 +-
 .../parser/antlr4/internal/AtnManager.java      | 110 -------------------
 .../antlr4/internal/atnmanager/AtnManager.java  |  88 +++++++++++++++
 .../internal/atnmanager/LexerAtnManager.java    |  31 ++++++
 .../internal/atnmanager/ParserAtnManager.java   |  24 ++++
 8 files changed, 152 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/README.adoc
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/README.adoc b/subprojects/parser-antlr4/README.adoc
index 4075afb..4d08312 100644
--- a/subprojects/parser-antlr4/README.adoc
+++ b/subprojects/parser-antlr4/README.adoc
@@ -57,6 +57,7 @@ groovyOptions.forkOptions.jvmArgs += ["-Dgroovy.antlr4=true"]
 === JVM system properties to control parsing
 
 * `groovy.antlr4.cache.threshold`: how frequently to clear DFA cache(default: 64). **Notice:**
The more frequently the DFA cache is cleared, the poorer parsing performance will be(you can
not set the value that is less than the default value). But the DFA cache has to be cleared
to avoid OutOfMemoryError's occurring.
+* `groovy.clear.lexer.dfa.cache`: whether to clear the dfa cache of lexer(default: false)
 * `groovy.attach.groovydoc`: whether to attach groovydoc to node as metadata while parsing
groovy source code(default: false)
 * `groovy.attach.runtime.groovydoc`: whether to attach `@Groovydoc` annotation to all members
which have groovydoc(i.e. `/** ... */`)
 * `groovy.extract.doc.comment`: whether to collect groovydoc while parsing groovy source
code(default: false). **DEPRECATED, USE `groovy.attach.groovydoc` INSTEAD**

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
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 98526a1..e3dd188 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
@@ -30,7 +30,7 @@ import org.antlr.v4.runtime.atn.PredictionMode;
 import org.antlr.v4.runtime.misc.ParseCancellationException;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.antlr.v4.runtime.tree.TerminalNode;
-import org.apache.groovy.parser.antlr4.internal.AtnManager;
+import org.apache.groovy.parser.antlr4.internal.atnmanager.AtnManager;
 import org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy;
 import org.apache.groovy.parser.antlr4.util.StringUtils;
 import org.apache.groovy.util.Maps;
@@ -391,7 +391,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
         try {
             // parsing have to wait util clearing is complete.
-            AtnManager.RRWL.readLock().lock();
+            AtnManager.READ_LOCK.lock();
             try {
                 result = buildCST(PredictionMode.SLL);
             } catch (Throwable t) {
@@ -402,7 +402,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
                 result = buildCST(PredictionMode.LL);
             } finally {
-                AtnManager.RRWL.readLock().unlock();
+                AtnManager.READ_LOCK.unlock();
             }
         } catch (Throwable t) {
             throw convertException(t);

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangLexer.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangLexer.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangLexer.java
index d3b7538..51a8583 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangLexer.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangLexer.java
@@ -1,21 +1,3 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
 package org.apache.groovy.parser.antlr4;
 
 import org.antlr.v4.runtime.CharStream;
@@ -23,7 +5,8 @@ import org.antlr.v4.runtime.Lexer;
 import org.antlr.v4.runtime.LexerNoViableAltException;
 import org.antlr.v4.runtime.atn.ATN;
 import org.antlr.v4.runtime.atn.LexerATNSimulator;
-import org.apache.groovy.parser.antlr4.internal.AtnManager;
+import org.apache.groovy.parser.antlr4.internal.atnmanager.AtnManager;
+import org.apache.groovy.parser.antlr4.internal.atnmanager.LexerAtnManager;
 
 /**
  * The lexer for Groovy programming language, which is based on the lexer generated by Antlr4
@@ -35,7 +18,7 @@ public class GroovyLangLexer extends GroovyLexer {
     public GroovyLangLexer(CharStream input) {
         super(input);
 
-        this.setInterpreter(new PositionAdjustingLexerATNSimulator(this, new AtnManager(this).getATN()));
+        this.setInterpreter(new PositionAdjustingLexerATNSimulator(this, LexerAtnManager.INSTANCE.getATN()));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangParser.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangParser.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangParser.java
index fa33c6e..dd91276 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangParser.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovyLangParser.java
@@ -21,7 +21,7 @@ package org.apache.groovy.parser.antlr4;
 
 import org.antlr.v4.runtime.TokenStream;
 import org.antlr.v4.runtime.atn.ParserATNSimulator;
-import org.apache.groovy.parser.antlr4.internal.AtnManager;
+import org.apache.groovy.parser.antlr4.internal.atnmanager.ParserAtnManager;
 
 /**
  * The parser for Groovy programming language, which is based on the parser generated by
Antlr4
@@ -33,6 +33,6 @@ public class GroovyLangParser extends GroovyParser {
     public GroovyLangParser(TokenStream input) {
         super(input);
 
-        this.setInterpreter(new ParserATNSimulator(this, new AtnManager(this).getATN()));
+        this.setInterpreter(new ParserATNSimulator(this, ParserAtnManager.INSTANCE.getATN()));
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/AtnManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/AtnManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/AtnManager.java
deleted file mode 100644
index 686ec14..0000000
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/AtnManager.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-package org.apache.groovy.parser.antlr4.internal;
-
-import org.antlr.v4.runtime.atn.ATN;
-import org.apache.groovy.parser.antlr4.GroovyLangLexer;
-import org.apache.groovy.parser.antlr4.GroovyLangParser;
-import org.apache.groovy.util.Maps;
-import org.apache.groovy.util.SystemUtil;
-
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-/**
- * Manage ATN for lexer and parser to avoid memory leak
- *
- * @author <a href="mailto:realbluesun@hotmail.com">Daniel.Sun</a>
- * Created on 2016/08/14
- */
-public class AtnManager {
-    public static final ReentrantReadWriteLock RRWL = new ReentrantReadWriteLock(true);
-    private static final String DFA_CACHE_THRESHOLD_OPT = "groovy.antlr4.cache.threshold";
-    private static final String GROOVY_CLEAR_LEXER_DFA_CACHE = "groovy.clear.lexer.dfa.cache";
-    private static final int DEFAULT_DFA_CACHE_THRESHOLD = 64;
-    private static final int MIN_DFA_CACHE_THRESHOLD = 32;
-    private static final int DFA_CACHE_THRESHOLD;
-    private final Class ownerClass;
-    private final ATN atn;
-    private static final boolean TO_CLEAR_LEXER_DFA_CACHE;
-    private static final Map<Class, AtnWrapper> ATN_MAP = Maps.of(
-            GroovyLangLexer.class, new AtnWrapper(GroovyLangLexer._ATN),
-            GroovyLangParser.class, new AtnWrapper(GroovyLangParser._ATN)
-    );
-
-    static {
-        int t = DEFAULT_DFA_CACHE_THRESHOLD;
-
-        try {
-            t = Integer.parseInt(System.getProperty(DFA_CACHE_THRESHOLD_OPT));
-
-            // cache threshold should be at least MIN_DFA_CACHE_THRESHOLD for better performance
-            t = t < MIN_DFA_CACHE_THRESHOLD ? MIN_DFA_CACHE_THRESHOLD : t;
-        } catch (Exception e) {
-            // ignored
-        }
-
-        DFA_CACHE_THRESHOLD = t;
-
-        TO_CLEAR_LEXER_DFA_CACHE = SystemUtil.getBooleanSafe(GROOVY_CLEAR_LEXER_DFA_CACHE);
-    }
-
-    public AtnManager(GroovyLangLexer lexer) {
-        this.ownerClass = lexer.getClass();
-        this.atn = TO_CLEAR_LEXER_DFA_CACHE ? getAtnWrapper(this.ownerClass).checkAndClear()
: GroovyLangLexer._ATN;
-    }
-
-    public AtnManager(GroovyLangParser parser) {
-        this.ownerClass = parser.getClass();
-        this.atn = getAtnWrapper(this.ownerClass).checkAndClear();
-    }
-
-    public ATN getATN() {
-        return this.atn;
-    }
-
-    private AtnWrapper getAtnWrapper(Class ownerClass) {
-        return ATN_MAP.get(ownerClass);
-    }
-
-    private static class AtnWrapper {
-        private final ATN atn;
-        private final AtomicLong counter = new AtomicLong(0);
-
-        public AtnWrapper(ATN atn) {
-            this.atn = atn;
-        }
-
-        public ATN checkAndClear() {
-            if (0 != counter.incrementAndGet() % DFA_CACHE_THRESHOLD) {
-                return atn;
-            }
-
-            RRWL.writeLock().lock();
-            try {
-                atn.clearDFA();
-            } finally {
-                RRWL.writeLock().unlock();
-            }
-
-            return atn;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/AtnManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/AtnManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/AtnManager.java
new file mode 100644
index 0000000..dff64e7
--- /dev/null
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/AtnManager.java
@@ -0,0 +1,88 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.parser.antlr4.internal.atnmanager;
+
+import org.antlr.v4.runtime.atn.ATN;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Manage ATN to avoid memory leak
+ *
+ * @author <a href="mailto:realbluesun@hotmail.com">Daniel.Sun</a>
+ * Created on 2016/08/14
+ */
+public abstract class AtnManager {
+    private static final ReentrantReadWriteLock RRWL = new ReentrantReadWriteLock(true);
+    private static final ReentrantReadWriteLock.WriteLock WRITE_LOCK = RRWL.writeLock();
+    public static final ReentrantReadWriteLock.ReadLock READ_LOCK = RRWL.readLock();
+    private static final String DFA_CACHE_THRESHOLD_OPT = "groovy.antlr4.cache.threshold";
+    private static final int DEFAULT_DFA_CACHE_THRESHOLD = 64;
+    private static final int MIN_DFA_CACHE_THRESHOLD = 2;
+    private static final int DFA_CACHE_THRESHOLD;
+
+    static {
+        int t = DEFAULT_DFA_CACHE_THRESHOLD;
+
+        try {
+            t = Integer.parseInt(System.getProperty(DFA_CACHE_THRESHOLD_OPT));
+
+            // cache threshold should be at least MIN_DFA_CACHE_THRESHOLD for better performance
+            t = t < MIN_DFA_CACHE_THRESHOLD ? MIN_DFA_CACHE_THRESHOLD : t;
+        } catch (Exception e) {
+            // ignored
+        }
+
+        DFA_CACHE_THRESHOLD = t;
+    }
+
+
+    public abstract ATN getATN();
+
+    protected abstract boolean shouldClearDfaCache();
+
+    protected class AtnWrapper {
+        private final ATN atn;
+        private final AtomicLong counter = new AtomicLong(0);
+
+        public AtnWrapper(ATN atn) {
+            this.atn = atn;
+        }
+
+        public ATN checkAndClear() {
+            if (!shouldClearDfaCache()) {
+                return atn;
+            }
+
+            if (0 != counter.incrementAndGet() % DFA_CACHE_THRESHOLD) {
+                return atn;
+            }
+
+            WRITE_LOCK.lock();
+            try {
+                atn.clearDFA();
+            } finally {
+                WRITE_LOCK.unlock();
+            }
+
+            return atn;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/LexerAtnManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/LexerAtnManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/LexerAtnManager.java
new file mode 100644
index 0000000..aa6ed1f
--- /dev/null
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/LexerAtnManager.java
@@ -0,0 +1,31 @@
+package org.apache.groovy.parser.antlr4.internal.atnmanager;
+
+import org.antlr.v4.runtime.atn.ATN;
+import org.apache.groovy.parser.antlr4.GroovyLangLexer;
+import org.apache.groovy.util.SystemUtil;
+
+/**
+ * Manage ATN for lexer to avoid memory leak
+ */
+public class LexerAtnManager extends AtnManager {
+    private static final String GROOVY_CLEAR_LEXER_DFA_CACHE = "groovy.clear.lexer.dfa.cache";
+    private static final boolean TO_CLEAR_LEXER_DFA_CACHE;
+    private final AtnWrapper lexerAtnWrapper = new AtnManager.AtnWrapper(GroovyLangLexer._ATN);
+    public static final LexerAtnManager INSTANCE = new LexerAtnManager();
+
+    static {
+        TO_CLEAR_LEXER_DFA_CACHE = SystemUtil.getBooleanSafe(GROOVY_CLEAR_LEXER_DFA_CACHE);
+    }
+
+    @Override
+    public ATN getATN() {
+        return lexerAtnWrapper.checkAndClear();
+    }
+
+    @Override
+    protected boolean shouldClearDfaCache() {
+        return TO_CLEAR_LEXER_DFA_CACHE;
+    }
+
+    private LexerAtnManager() {}
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/6cc11028/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/ParserAtnManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/ParserAtnManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/ParserAtnManager.java
new file mode 100644
index 0000000..f30338c
--- /dev/null
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/atnmanager/ParserAtnManager.java
@@ -0,0 +1,24 @@
+package org.apache.groovy.parser.antlr4.internal.atnmanager;
+
+import org.antlr.v4.runtime.atn.ATN;
+import org.apache.groovy.parser.antlr4.GroovyLangParser;
+
+/**
+ * Manage ATN for parser to avoid memory leak
+ */
+public class ParserAtnManager extends AtnManager {
+    private final AtnWrapper parserAtnWrapper = new AtnManager.AtnWrapper(GroovyLangParser._ATN);
+    public static final ParserAtnManager INSTANCE = new ParserAtnManager();
+
+    @Override
+    public ATN getATN() {
+        return parserAtnWrapper.checkAndClear();
+    }
+
+    @Override
+    protected boolean shouldClearDfaCache() {
+        return true;
+    }
+
+    private ParserAtnManager() {}
+}


Mime
View raw message