TINKERPOP-1950 Cached global strategy lookups during traversal construction

This change leads to a 1.5x to 2x speed improvement in traversal construction. 
It is especially effective when processing traversals that have many child 
traversals within them as this method is called for not only the parent 
traversal but all the children as well.


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

Branch: refs/heads/TINKERPOP-1836
Commit: cd20298d3f4ee5f66fe4f037e8aa64daf32254b4
Parents: 44c4073
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Apr 20 16:18:13 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Apr 25 07:41:38 2018 -0400

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  1 +
 .../process/traversal/TraversalStrategies.java  | 36 +++++++++++++++-----
 2 files changed, 29 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cd20298d/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index f71602a..f6ca111 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -27,6 +27,7 @@ 
image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 * Bumped to httpclient 4.5.5.
 * Bumped to Groovy 2.4.15 - fixes bug with `Lambda` construction.
 * Improved performance of GraphSON deserialization of `Bytecode`.
+* Improved performance of traversal construction.
 
 [[release-3-2-8]]
 === TinkerPop 3.2.8 (Release Date: April 2, 2018)

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cd20298d/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java
index c7ee5bf..e84737c 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TraversalStrategies.java
@@ -52,6 +52,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -198,6 +199,12 @@ public interface TraversalStrategies extends Serializable, 
Cloneable {
 
     public static final class GlobalCache {
 
+        /**
+         * Keeps track of {@link GraphComputer} and/or {@link Graph} classes 
that have been initialized to the
+         * classloader so that they do not have to be reflected again.
+         */
+        private static Set<Class> LOADED = ConcurrentHashMap.newKeySet();
+
         private static final Map<Class<? extends Graph>, TraversalStrategies> 
GRAPH_CACHE = new HashMap<>();
         private static final Map<Class<? extends GraphComputer>, 
TraversalStrategies> GRAPH_COMPUTER_CACHE = new HashMap<>();
 
@@ -244,20 +251,33 @@ public interface TraversalStrategies extends 
Serializable, Cloneable {
         public static TraversalStrategies getStrategies(final Class 
graphOrGraphComputerClass) {
             try {
                 // be sure to load the class so that its static{} traversal 
strategy registration component is loaded.
-                // this is more important for GraphComputer classes as they 
are typically not instantiated prior to strategy usage like Graph classes.
-                final String graphComputerClassName = null != 
graphOrGraphComputerClass.getDeclaringClass() ?
+                // this is more important for GraphComputer classes as they 
are typically not instantiated prior to
+                // strategy usage like Graph classes.
+                if (!LOADED.contains(graphOrGraphComputerClass)) {
+                    final String graphComputerClassName = null != 
graphOrGraphComputerClass.getDeclaringClass() ?
                         
graphOrGraphComputerClass.getCanonicalName().replace("." + 
graphOrGraphComputerClass.getSimpleName(), "$" + 
graphOrGraphComputerClass.getSimpleName()) :
                         graphOrGraphComputerClass.getCanonicalName();
-                Class.forName(graphComputerClassName);
+                    Class.forName(graphComputerClassName);
+
+                    // keep track of stuff we already loaded once - stuff in 
this if/statement isn't cheap and this
+                    // method gets called a lot, basically every time a new 
traversal gets spun up (that includes
+                    // child traversals. perhaps it is possible to just check 
the cache keys for this information, but
+                    // it's not clear if this method will be called with 
something not in the cache and if it is and
+                    // it results in error, then we'd probably not want to 
deal with this block again anyway
+                    LOADED.add(graphOrGraphComputerClass);
+                }
             } catch (final ClassNotFoundException e) {
                 throw new IllegalStateException(e.getMessage(), e);
             }
-            if (Graph.class.isAssignableFrom(graphOrGraphComputerClass)) {
-                final TraversalStrategies traversalStrategies = 
GRAPH_CACHE.get(graphOrGraphComputerClass);
-                return null == traversalStrategies ? 
GRAPH_CACHE.get(Graph.class) : traversalStrategies;
+            
+            if (GRAPH_CACHE.containsKey(graphOrGraphComputerClass)) {
+                return GRAPH_CACHE.get(graphOrGraphComputerClass);
+            } else if 
(Graph.class.isAssignableFrom(graphOrGraphComputerClass)) {
+                return GRAPH_CACHE.get(Graph.class);
+            } else if 
(GRAPH_COMPUTER_CACHE.containsKey(graphOrGraphComputerClass)) {
+                return GRAPH_COMPUTER_CACHE.get(graphOrGraphComputerClass);
             } else if 
(GraphComputer.class.isAssignableFrom(graphOrGraphComputerClass)) {
-                final TraversalStrategies traversalStrategies = 
GRAPH_COMPUTER_CACHE.get(graphOrGraphComputerClass);
-                return null == traversalStrategies ? 
GRAPH_COMPUTER_CACHE.get(GraphComputer.class) : traversalStrategies;
+                return GRAPH_COMPUTER_CACHE.get(GraphComputer.class);
             } else {
                 throw new IllegalArgumentException("The 
TraversalStrategies.GlobalCache only supports Graph and GraphComputer strategy 
caching: " + graphOrGraphComputerClass.getCanonicalName());
             }

Reply via email to