You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by xi...@apache.org on 2023/04/03 22:45:28 UTC

[tinkerpop] 01/02: perf: improved performance of comparison between not compatible types and nulls

This is an automated email from the ASF dual-hosted git repository.

xiazcy pushed a commit to branch 3.6-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 44c888dec68d66e56fd2acc8dbf09d7d1eed2aa5
Author: lvca <lv...@users.noreply.github.com>
AuthorDate: Sat Mar 18 10:51:10 2023 -0400

    perf: improved performance of comparison between not compatible types and nulls
    
    This fix dramatically improves performance of traversing when different types are encountered or simply null values. This piece of code should be designed to be fast, because invoked millions of times with queries. It is a bad practice to throw Java exceptions instead of simply return false.
---
 .../tinkerpop/gremlin/util/GremlinValueComparator.java   | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/GremlinValueComparator.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/GremlinValueComparator.java
index 345ecd8391..ea2566feee 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/GremlinValueComparator.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/GremlinValueComparator.java
@@ -129,8 +129,18 @@ public abstract class GremlinValueComparator implements Comparator<Object> {
             if (containersOfDifferentSize(f, s))
                 return false;
 
+            // For Compare, NaN always produces ERROR
+            if (eitherAreNaN(f, s))
+                return false;
+
+            // For Compare we do not cross type boundaries, including null
+            if (!comparable(f, s))
+                return false;
+
             try {
-                return this.compare(f, s) == 0;
+              // comparable(f, s) assures that type(f) == type(s)
+              final Type type = Type.type(f);
+              return comparator(type).compare(f, s) == 0;
             } catch (GremlinTypeErrorException ex) {
                 /**
                  * By routing through the compare(f, s) path we expose ourselves to type errors, which should be
@@ -141,7 +151,7 @@ public abstract class GremlinValueComparator implements Comparator<Object> {
                  *
                  * Can also happen for elements nested inside of collections.
                  */
-                return false;
+               return false;
             }
         }
 
@@ -345,7 +355,7 @@ public abstract class GremlinValueComparator implements Comparator<Object> {
         put(Type.MapEntry,       entryComparator);
         put(Type.Unknown,        unknownTypeComparator);
     }};
-    
+
     private GremlinValueComparator() {}
 
     protected Comparator comparator(final Type type) {