-        return distances.computeIfAbsent(o1, this::slowDistance);
+        return distances.getOrDefault(o1, slowDistance(o1));

doesn't it defeat the goal of having a cache?

Romain Manni-Bucau
@rmannibucau <https://x.com/rmannibucau> | .NET Blog
<https://dotnetbirdie.github.io/> | Blog <https://rmannibucau.github.io/> | Old
Blog <http://rmannibucau.wordpress.com> | Github
<https://github.com/rmannibucau> | LinkedIn
<https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/en-us/product/java-ee-8-high-performance-9781788473064>
Javaccino <https://javaccino.dev/> founder (Java/.NET service - contact via
linkedin)


---------- Forwarded message ---------
De : <[email protected]>
Date: lun. 24 août 2026 à 11:17
Subject: (johnzon) branch master updated: optimized caching
To: [email protected] <[email protected]>


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

jungm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new bbf13566 optimized caching
bbf13566 is described below

commit bbf13566bbb664abe85e36aa9dabea0bb07f7cce
Author: Markus Jung <[email protected]>
AuthorDate: Mon Aug 24 11:17:02 2026 +0200

    optimized caching
---
 .../PerHierarchyAndLexicographicalOrderFieldComparator.java | 13
+++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git
a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/order/PerHierarchyAndLexicographicalOrderFieldComparator.java
b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/order/PerHierarchyAndLexicographicalOrderFieldComparator.java
index 48948f63..9c4b2327 100644
---
a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/order/PerHierarchyAndLexicographicalOrderFieldComparator.java
+++
b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/order/PerHierarchyAndLexicographicalOrderFieldComparator.java
@@ -45,7 +45,12 @@ public class
PerHierarchyAndLexicographicalOrderFieldComparator implements Compa
     }

     private int distance(final String o1) {
-        return distances.computeIfAbsent(o1, this::slowDistance);
+        return distances.getOrDefault(o1, slowDistance(o1));
+    }
+
+    private int cache(final String o1, final int distance) {
+        distances.putIfAbsent(o1, distance);
+        return distance;
     }

     private int slowDistance(String o1) {
@@ -54,20 +59,20 @@ public class
PerHierarchyAndLexicographicalOrderFieldComparator implements Compa
         while (current != null && current != Object.class) {
             try {
                 current.getDeclaredField(o1);
-                return i;
+                return cache(o1, i);
             } catch (final NoSuchFieldException e) {
                 // no-op
             }
             final String methodSuffix =
Character.toUpperCase(o1.charAt(0)) + (o1.length() > 1 ? o1.substring(1) :
"");
             try {
                 current.getDeclaredMethod("get" + methodSuffix);
-                return i;
+                return cache(o1, i);
             } catch (final Exception e) {
                 // no-op
             }
             try {
                 current.getDeclaredMethod("is" + methodSuffix);
-                return i;
+                return cache(o1, i);
             } catch (final Exception e) {
                 // no-op
             }

Reply via email to