Skip to content

Commit 0c9ada4

Browse files
authored
Refine performance (alt) (#1508)
Reusing #1492 but simplifying it: copy only the collection that is about to be modified, leave other unchanged (less copy less mem, less cycles).
1 parent b6eefc1 commit 0c9ada4

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/manager/AbstractDependencyManager.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
* or applied in same POM) are always applied. This implementation makes sure, that version and scope are not applied
5252
* onto same node that actually provided the rules, to no override work that ModelBuilder did. It achieves this goal
5353
* by tracking "depth" for each collected rule and ignoring rules coming from same depth as processed dependency node is.
54+
* <p>
55+
* Note for future: the field {@code managedLocalPaths} is <em>intentionally left out of hash/equals</em>, with
56+
* reason explained above.
5457
*
5558
* @since 2.0.0
5659
*/
@@ -113,15 +116,8 @@ protected AbstractDependencyManager(
113116
// nullable: if using scope manager, but there is no system scope defined
114117
this.systemDependencyScope = systemDependencyScope;
115118

116-
this.hashCode = Objects.hash(
117-
depth,
118-
deriveUntil,
119-
applyFrom,
120-
managedVersions,
121-
managedScopes,
122-
managedOptionals,
123-
managedLocalPaths,
124-
managedExclusions);
119+
// exclude managedLocalPaths
120+
this.hashCode = Objects.hash(depth, managedVersions, managedScopes, managedOptionals, managedExclusions);
125121
}
126122

127123
protected abstract DependencyManager newInstance(
@@ -185,7 +181,7 @@ public DependencyManager deriveChildManager(DependencyCollectionContext context)
185181
Collection<Exclusion> exclusions = managedDependency.getExclusions();
186182
if (!exclusions.isEmpty()) {
187183
if (managedExclusions == this.managedExclusions) {
188-
managedExclusions = MMap.copy(this.managedExclusions);
184+
managedExclusions = MMap.copyWithKey(key, this.managedExclusions);
189185
}
190186
Collection<Holder<Collection<Exclusion>>> managed = managedExclusions.get(key);
191187
if (managed == null) {
@@ -320,13 +316,11 @@ public boolean equals(Object obj) {
320316
}
321317

322318
AbstractDependencyManager that = (AbstractDependencyManager) obj;
319+
// exclude managedLocalPaths
323320
return depth == that.depth
324-
&& deriveUntil == that.deriveUntil
325-
&& applyFrom == that.applyFrom
326321
&& managedVersions.equals(that.managedVersions)
327322
&& managedScopes.equals(that.managedScopes)
328323
&& managedOptionals.equals(that.managedOptionals)
329-
&& managedLocalPaths.equals(that.managedLocalPaths)
330324
&& managedExclusions.equals(that.managedExclusions);
331325
}
332326

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/manager/MMap.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.eclipse.aether.util.graph.manager;
2020

21+
import java.util.ArrayList;
22+
import java.util.Collection;
2123
import java.util.HashMap;
2224

2325
/**
@@ -47,6 +49,12 @@ public static <K, V> MMap<K, V> copy(MMap<K, V> orig) {
4749
return new MMap<>(new HashMap<>(orig.delegate));
4850
}
4951

52+
public static <K, V> MMap<K, Collection<V>> copyWithKey(K key, MMap<K, Collection<V>> orig) {
53+
HashMap<K, Collection<V>> delegateLocal = new HashMap<>(orig.delegate);
54+
delegateLocal.computeIfPresent(key, (k, v) -> new ArrayList<>(v));
55+
return new MMap<>(delegateLocal);
56+
}
57+
5058
protected final HashMap<K, V> delegate;
5159

5260
private MMap(HashMap<K, V> delegate) {

0 commit comments

Comments
 (0)