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.
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 345ecd8..ea2566f 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 @@
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 @@
*
* Can also happen for elements nested inside of collections.
*/
- return false;
+ return false;
}
}
@@ -345,7 +355,7 @@
put(Type.MapEntry, entryComparator);
put(Type.Unknown, unknownTypeComparator);
}};
-
+
private GremlinValueComparator() {}
protected Comparator comparator(final Type type) {