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 2017/12/01 17:34:16 UTC
groovy git commit: Refine StubCache for better performance
Repository: groovy
Updated Branches:
refs/heads/master 48a491544 -> bd1b3bc2c
Refine StubCache for better performance
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/bd1b3bc2
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/bd1b3bc2
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/bd1b3bc2
Branch: refs/heads/master
Commit: bd1b3bc2c5ac5c9a7e26da74765da7fc01046974
Parents: 48a4915
Author: sunlan <su...@apache.org>
Authored: Sat Dec 2 01:31:33 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 2 01:34:05 2017 +0800
----------------------------------------------------------------------
.../groovy/ast/decompiled/AsmDecompiler.java | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/bd1b3bc2/src/main/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java b/src/main/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
index df7ca54..933f1b5 100644
--- a/src/main/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
+++ b/src/main/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
@@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.ast.decompiled;
+import groovy.lang.GroovyRuntimeException;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
@@ -30,12 +31,14 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* A utility class responsible for decompiling JVM class files and producing {@link ClassStub} objects reflecting their structure.
@@ -46,12 +49,12 @@ public abstract class AsmDecompiler {
private static class StubCache {
/**
- * Caches stubs per URL. This cache is useful when performing multiple compilations in the same JVM/class loader and in tests.
+ * Caches stubs per URI. This cache is useful when performing multiple compilations in the same JVM/class loader and in tests.
*
* It's synchronized "just in case". Occasional misses are expected if several threads attempt to load the same class,
* but this shouldn't result in serious memory issues.
*/
- static final Map<URL, SoftReference<ClassStub>> map = Collections.synchronizedMap(new HashMap<URL, SoftReference<ClassStub>>());
+ static final Map<URI, SoftReference<ClassStub>> map = new ConcurrentHashMap<URI, SoftReference<ClassStub>>(); // According to http://michaelscharf.blogspot.jp/2006/11/javaneturlequals-and-hashcode-make.html, use java.net.URI instead.
}
/**
@@ -63,7 +66,14 @@ public abstract class AsmDecompiler {
* @throws IOException if reading from this URL is impossible
*/
public static ClassStub parseClass(URL url) throws IOException {
- SoftReference<ClassStub> ref = StubCache.map.get(url);
+ URI uri;
+ try {
+ uri = url.toURI();
+ } catch (URISyntaxException e) {
+ throw new GroovyRuntimeException(e);
+ }
+
+ SoftReference<ClassStub> ref = StubCache.map.get(uri);
ClassStub stub = ref == null ? null : ref.get();
if (stub == null) {
DecompilingVisitor visitor = new DecompilingVisitor();
@@ -74,7 +84,7 @@ public abstract class AsmDecompiler {
stream.close();
}
stub = visitor.result;
- StubCache.map.put(url, new SoftReference<ClassStub>(stub));
+ StubCache.map.put(uri, new SoftReference<ClassStub>(stub));
}
return stub;
}