You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/09/03 07:11:55 UTC

(maven) 01/01: Optimize computeLocations() and ModelObjectPool location comparison

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

gnodet pushed a commit to branch optimize-locations-and-pool
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 2e42ba1b30b30dff53629f80eac55e946e2a090e
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Sep 3 07:11:38 2026 +0000

    Optimize computeLocations() and ModelObjectPool location comparison
    
    Two targeted hot path optimizations identified by JFR profiling:
    
    1. computeLocations(): replace Stream.concat().collect(toUnmodifiableMap)
       with HashMap.putAll() + Map.copyOf(). Also return oldlocs directly
       when newlocs is empty (avoids unnecessary Map.copyOf since base
       locations are already immutable).
    
    2. ModelObjectPool.PoolKey: precompute locationsHashCode at build time
       in the generated model classes. Use it for fast inequality checks
       before full map comparison in locationsEqual(). Replace per-key
       location iteration with direct Map.equals() via getLocations().
       Add fast path for both-empty maps (common for imported deps).
---
 .../maven/impl/model/DefaultModelObjectPool.java   | 39 ++++++++--------------
 src/mdo/model.vm                                   | 32 +++++++++++++++---
 2 files changed, 41 insertions(+), 30 deletions(-)

diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelObjectPool.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelObjectPool.java
index 6295ae92f3..b54602e4cd 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelObjectPool.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelObjectPool.java
@@ -253,29 +253,29 @@ private static boolean dependenciesEqual(
                     && Objects.equals(dep1.getSystemPath(), dep2.getSystemPath())
                     && Objects.equals(dep1.getExclusions(), dep2.getExclusions())
                     && Objects.equals(dep1.getOptional(), dep2.getOptional())
-                    && Objects.equals(dep1.getLocationKeys(), dep2.getLocationKeys())
                     && locationsEqual(dep1, dep2)
                     && Objects.equals(dep1.getImportedFrom(), dep2.getImportedFrom());
         }
 
         /**
          * Compare locations maps for two dependencies.
+         * Uses the direct getLocations() map instead of iterating through
+         * individual keys to avoid KeyValueHolder allocation overhead.
+         * Short-circuits on empty maps (common for imported dependencies).
          */
         private static boolean locationsEqual(
                 org.apache.maven.api.model.Dependency dep1, org.apache.maven.api.model.Dependency dep2) {
-            var keys1 = dep1.getLocationKeys();
-            var keys2 = dep2.getLocationKeys();
-
-            if (!Objects.equals(keys1, keys2)) {
-                return false;
+            var locs1 = dep1.getLocations();
+            var locs2 = dep2.getLocations();
+            // Fast path: both empty (common for imported deps with location tracking off)
+            if (locs1.isEmpty() && locs2.isEmpty()) {
+                return true;
             }
-
-            for (Object key : keys1) {
-                if (!Objects.equals(dep1.getLocation(key), dep2.getLocation(key))) {
-                    return false;
-                }
+            // Use precomputed hash code for fast inequality check
+            if (dep1.getLocationsHashCode() != dep2.getLocationsHashCode()) {
+                return false;
             }
-            return true;
+            return locs1.equals(locs2);
         }
 
         /**
@@ -303,23 +303,10 @@ private static int dependencyHashCode(org.apache.maven.api.model.Dependency dep)
             h = 31 * h + Objects.hashCode(dep.getSystemPath());
             h = 31 * h + Objects.hashCode(dep.getExclusions());
             h = 31 * h + Objects.hashCode(dep.getOptional());
-            h = 31 * h + Objects.hashCode(dep.getLocationKeys());
-            h = 31 * h + locationsHashCode(dep);
+            h = 31 * h + dep.getLocationsHashCode();
             h = 31 * h + Objects.hashCode(dep.getImportedFrom());
             return h;
         }
-
-        /**
-         * Compute hash code for locations map.
-         */
-        private static int locationsHashCode(org.apache.maven.api.model.Dependency dep) {
-            int hash = 1;
-            for (Object key : dep.getLocationKeys()) {
-                hash = 31 * hash + Objects.hashCode(key);
-                hash = 31 * hash + Objects.hashCode(dep.getLocation(key));
-            }
-            return hash;
-        }
     }
 
     /**
diff --git a/src/mdo/model.vm b/src/mdo/model.vm
index 481ee0b489..e147607a58 100644
--- a/src/mdo/model.vm
+++ b/src/mdo/model.vm
@@ -151,6 +151,8 @@ public class ${class.name}
     #if ( $locationTracking && ! $class.superClass )
     /** Locations */
     final Map<Object, InputLocation> locations;
+    /** Cached hash code for the locations map, precomputed at build time */
+    final int locationsHashCode;
     /** Location tracking */
     final InputLocation importedFrom;
     #end
@@ -180,6 +182,7 @@ public class ${class.name}
     #end
     #if ( $locationTracking && ! $class.superClass )
         this.locations = builder.computeLocations();
+        this.locationsHashCode = this.locations.hashCode();
         this.importedFrom = builder.importedFrom;
     #end
     }
@@ -264,6 +267,25 @@ public class ${class.name}
         return locations.keySet().stream();
     }
 
+    /**
+     * Gets the locations map directly for bulk comparison and hashing.
+     *
+     * @return an unmodifiable map of locations, never {@code null}
+     */
+    public Map<Object, InputLocation> getLocations() {
+        return locations;
+    }
+
+    /**
+     * Gets the precomputed hash code for the locations map.
+     * Avoids re-iterating the map entries during pooling/interning operations.
+     *
+     * @return the cached hash code of the locations map
+     */
+    public int getLocationsHashCode() {
+        return locationsHashCode;
+    }
+
     /**
      * Gets the input location that caused this model to be read.
      */
@@ -518,14 +540,16 @@ public class ${class.name}
             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
             if (newlocs.isEmpty()) {
-                return Map.copyOf(oldlocs);
+                return oldlocs;
             }
             if (oldlocs.isEmpty()) {
                 return Map.copyOf(newlocs);
             }
-            return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
-                    // Keep value from newlocs in case of duplicates
-                    .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
+            // Use HashMap.putAll instead of Stream.concat().collect() to avoid
+            // Stream allocation and intermediate Map.Entry iteration overhead
+            HashMap<Object, InputLocation> merged = new HashMap<>(oldlocs);
+            merged.putAll(newlocs); // newlocs entries override oldlocs (same semantics as before)
+            return Map.copyOf(merged);
         }
     #end
     }