JavaTranslator now makes use of a static method cache. This speeds up 
Bytecode->JavaTranslator translation. From 0.098ms to 0.029ms. If a 
GremlinServer Bytecode cache is not desired, then, at minimum we should use 
this JavaTranslator update in its place.


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/2a2b59b1
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/2a2b59b1
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/2a2b59b1

Branch: refs/heads/TINKERPOP-1442-master
Commit: 2a2b59b15833d80587bb8127d6edfd38a17ab788
Parents: 13782aa
Author: Marko A. Rodriguez <okramma...@gmail.com>
Authored: Tue Sep 13 13:23:32 2016 -0600
Committer: Marko A. Rodriguez <okramma...@gmail.com>
Committed: Tue Sep 13 13:23:32 2016 -0600

----------------------------------------------------------------------
 .../tinkerpop/gremlin/jsr223/JavaTranslator.java | 19 +++++++++++++++----
 .../structure/TinkerGraphPlayTest.java           |  2 +-
 2 files changed, 16 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2a2b59b1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
index dac6583..d00ad1c 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
@@ -33,6 +33,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -41,12 +42,11 @@ public final class JavaTranslator<S extends 
TraversalSource, T extends Traversal
 
     private final S traversalSource;
     private final Class anonymousTraversal;
-    private final Map<String, List<Method>> traversalSourceMethodCache = new 
HashMap<>();
-    private final Map<String, List<Method>> traversalMethodCache = new 
HashMap<>();
+    private static final Map<Class<? extends TraversalSource>, Map<String, 
List<Method>>> TRAVERSAL_SOURCE_METHOD_CACHE = new ConcurrentHashMap<>();
+    private static final Map<Class<? extends Traversal>, Map<String, 
List<Method>>> TRAVERSAL_METHOD_CACHE = new ConcurrentHashMap<>();
 
     private JavaTranslator(final S traversalSource) {
         this.traversalSource = traversalSource;
-        // todo: could produce an NPE later on. need a good model for when a 
traversal species doesn't support nesting.
         this.anonymousTraversal = 
traversalSource.getAnonymousTraversalClass().orElse(null);
     }
 
@@ -102,8 +102,12 @@ public final class JavaTranslator<S extends 
TraversalSource, T extends Traversal
     }
 
     private Object invokeMethod(final Object delegate, final Class returnType, 
final String methodName, final Object... arguments) {
+        //////////////////////////
+        //////////////////////////
         // populate method cache for fast access to methods in subsequent calls
-        final Map<String, List<Method>> methodCache = delegate instanceof 
TraversalSource ? this.traversalSourceMethodCache : this.traversalMethodCache;
+        final Map<String, List<Method>> methodCache = delegate instanceof 
TraversalSource ?
+                
this.TRAVERSAL_SOURCE_METHOD_CACHE.getOrDefault(delegate.getClass(), new 
HashMap<>()) :
+                this.TRAVERSAL_METHOD_CACHE.getOrDefault(delegate.getClass(), 
new HashMap<>());
         if (methodCache.isEmpty()) {
             for (final Method method : delegate.getClass().getMethods()) {
                 if (!(method.getName().equals("addV") && 
method.getParameterCount() == 1 && 
method.getParameters()[0].getType().equals(Object[].class))) { // hack cause 
its hard to tell Object[] vs. String :|
@@ -115,7 +119,14 @@ public final class JavaTranslator<S extends 
TraversalSource, T extends Traversal
                     list.add(method);
                 }
             }
+            if (delegate instanceof TraversalSource)
+                TRAVERSAL_SOURCE_METHOD_CACHE.put((Class<TraversalSource>) 
delegate.getClass(), methodCache);
+            else
+                TRAVERSAL_METHOD_CACHE.put((Class<Traversal>) 
delegate.getClass(), methodCache);
         }
+        //////////////////////////
+        //////////////////////////
+
         // create a copy of the argument array so as not to mutate the 
original bytecode
         final Object[] argumentsCopy = new Object[arguments.length];
         for (int i = 0; i < arguments.length; i++) {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2a2b59b1/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
----------------------------------------------------------------------
diff --git 
a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
 
b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
index 94fd250..37187fe 100644
--- 
a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
+++ 
b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
@@ -77,7 +77,7 @@ public class TinkerGraphPlayTest {
 
         final Traversal<?, ?> traversal = 
g.V().repeat(out()).times(2).groupCount().by("name").select(Column.keys).order().by(Order.decr);
         final Bytecode bytecode = traversal.asAdmin().getBytecode();
-
+        //final JavaTranslator translator = JavaTranslator.of(g);
         final Map<Bytecode, Traversal.Admin<?, ?>> cache = new HashMap<>();
         cache.put(bytecode, traversal.asAdmin());
         final HashSet<?> result = new LinkedHashSet<>(Arrays.asList("ripple", 
"lop"));

Reply via email to