rmannibucau commented on code in PR #146:
URL: https://github.com/apache/johnzon/pull/146#discussion_r3857383604


##########
johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/order/PerHierarchyAndLexicographicalOrderFieldComparator.java:
##########
@@ -18,67 +18,87 @@
  */
 package org.apache.johnzon.jsonb.order;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.Comparator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public class PerHierarchyAndLexicographicalOrderFieldComparator implements 
Comparator<String> {
     private final Class<?> clazz;
     private final Map<String, Integer> distances = new ConcurrentHashMap<>();
+    private final AtomicBoolean populated = new AtomicBoolean();
 
     public PerHierarchyAndLexicographicalOrderFieldComparator(final Class<?> 
clazz) {
         this.clazz = clazz;
     }
 
     @Override
     public int compare(final String o1, final String o2) {
-        if (o1.equals(o2)) {
+        if (o1 != null ? o1.equals(o2) : o2 == null) {
             return 0;
         }
-        final int d1 = distance(o1);
-        final int d2 = distance(o2);
+        populateDistances();
+        final Integer d1 = o1 == null ? null : distances.get(o1);
+        final Integer d2 = o2 == null ? null : distances.get(o2);
+        if (d1 == null || d2 == null) {
+            return compareStrings(o1, o2);
+        }
         final int res = d2 - d1; // reversed!
         if (res == 0) {
-            return o1.compareTo(o2);
+            return compareStrings(o1, o2);
         }
         return res;
     }
 
-    private int distance(final String o1) {
-        return distances.getOrDefault(o1, slowDistance(o1));
+    private void populateDistances() {

Review Comment:
   can be worth using visibility check to reduce the cache size but this is 
already better overall



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to