-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce skipper to avoid unnecessary dependency resolution
- Loading branch information
1 parent
69aeda6
commit e0fa3bc
Showing
4 changed files
with
542 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
280 changes: 280 additions & 0 deletions
280
...l/src/main/java/org/eclipse/aether/internal/impl/collect/DependencyResolutionSkipper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
package org.eclipse.aether.internal.impl.collect; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.eclipse.aether.artifact.Artifact; | ||
import org.eclipse.aether.graph.DependencyNode; | ||
import org.eclipse.aether.util.artifact.ArtifactIdUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
final class DependencyResolutionSkipper | ||
{ | ||
private static final Logger LOGGER = LoggerFactory.getLogger( DependencyResolutionSkipper.class ); | ||
|
||
private Map<DependencyNode, DependencyResolutionResult> results = new LinkedHashMap<>( 256 ); | ||
private CacheManager cacheManager = new CacheManager(); | ||
private CoordinateManager coordinateManager = new CoordinateManager(); | ||
|
||
DependencyResolutionSkipper() | ||
{ | ||
// enables default constructor | ||
} | ||
|
||
void report() | ||
{ | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( "Skipped {} nodes as having deeper depth", | ||
results.entrySet().stream().filter( n -> n.getValue().skippedAsDeeperDepth ).count() ); | ||
LOGGER.trace( "Skipped {} nodes as having version conflict", | ||
results.entrySet().stream().filter( n -> n.getValue().skippedAsVersionConflict ).count() ); | ||
LOGGER.trace( "Resolved {} nodes", | ||
results.entrySet().stream().filter( n -> n.getValue().resolve ).count() ); | ||
LOGGER.trace( "Forced resolving {} nodes for scope selection", | ||
results.entrySet().stream().filter( n -> n.getValue().forceResolution ).count() ); | ||
} | ||
} | ||
|
||
public Map<DependencyNode, DependencyResolutionResult> getResults() | ||
{ | ||
return results; | ||
} | ||
|
||
boolean skipResolution( DependencyNode node, List<DependencyNode> parents ) | ||
{ | ||
DependencyResolutionResult result = new DependencyResolutionResult( node ); | ||
results.put( node, result ); | ||
|
||
int depth = parents.size() + 1; | ||
coordinateManager.createCoordinate( node, depth ); | ||
|
||
if ( cacheManager.isVersionConflict( node ) ) | ||
{ | ||
//skip resolving version conflict losers (omitted for conflict) | ||
result.skippedAsVersionConflict = true; | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( "Skipped resolving node: {} as version conflict", | ||
ArtifactIdUtils.toId( node.getArtifact() ) ); | ||
} | ||
} | ||
else if ( coordinateManager.isLeftmost( node, parents ) ) | ||
{ | ||
/* | ||
* Force resolving the node to retain conflict paths when its coordinate is more left than last resolved. | ||
* This is because Maven picks the widest scope present among conflicting dependencies. | ||
*/ | ||
result.forceResolution = true; | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( "Force resolving node: {} for scope selection", | ||
ArtifactIdUtils.toId( node.getArtifact() ) ); | ||
} | ||
} | ||
else if ( cacheManager.getWinnerDepth( node ) <= depth ) | ||
{ | ||
//skip resolving if depth deeper (omitted for duplicate) | ||
result.skippedAsDeeperDepth = true; | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( "Skipped resolving node: {} as the node's depth is deeper than winner", | ||
ArtifactIdUtils.toId( node.getArtifact() ) ); | ||
} | ||
} | ||
else | ||
{ | ||
result.resolve = true; | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( "Resolving node: {}", | ||
ArtifactIdUtils.toId( node.getArtifact() ) ); | ||
} | ||
} | ||
|
||
if ( result.toResolve() ) | ||
{ | ||
coordinateManager.updateLeftmost( node ); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void cacheWithDepth( DependencyNode node, List<DependencyNode> parents ) | ||
{ | ||
boolean parentForceResolution = parents.stream() | ||
.anyMatch( n -> results.containsKey( n ) && results.get( n ).forceResolution ); | ||
if ( parentForceResolution ) | ||
{ | ||
if ( LOGGER.isTraceEnabled() ) | ||
{ | ||
LOGGER.trace( | ||
"Won't cache as node: {} inherits from a force-resolved node and will be omitted for duplicate", | ||
ArtifactIdUtils.toId( node.getArtifact() ) ); | ||
} | ||
} | ||
else | ||
{ | ||
cacheManager.cacheWinnerDepth( node, parents.size() + 1 ); | ||
} | ||
} | ||
|
||
|
||
static final class DependencyResolutionResult | ||
{ | ||
DependencyNode current; | ||
boolean skippedAsVersionConflict; //omitted for conflict | ||
boolean skippedAsDeeperDepth; //omitted for duplicate | ||
boolean resolve; //node to resolve (winner node) | ||
boolean forceResolution; //force resolving (duplicate node) for scope selection | ||
|
||
DependencyResolutionResult( DependencyNode current ) | ||
{ | ||
this.current = current; | ||
} | ||
|
||
boolean toResolve() | ||
{ | ||
return resolve || forceResolution; | ||
} | ||
} | ||
|
||
static final class CacheManager | ||
{ | ||
|
||
/** | ||
* artifact -> depth, only cache winners. | ||
*/ | ||
private final HashMap<Artifact, Integer> winnerDepths = new HashMap<>( 256 ); | ||
|
||
|
||
/** | ||
* versionLessId -> Artifact, only cache winners | ||
*/ | ||
private final Map<String, Artifact> winnerGAs = new HashMap<>( 256 ); | ||
|
||
boolean isVersionConflict( DependencyNode node ) | ||
{ | ||
String ga = ArtifactIdUtils.toVersionlessId( node.getArtifact() ); | ||
if ( winnerGAs.containsKey( ga ) ) | ||
{ | ||
Artifact result = winnerGAs.get( ga ); | ||
return !node.getArtifact().getVersion().equals( result.getVersion() ); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void cacheWinnerDepth( DependencyNode node, int depth ) | ||
{ | ||
LOGGER.trace( "Artifact {} with depth {} cached", node.getArtifact(), depth ); | ||
winnerDepths.put( node.getArtifact(), depth ); | ||
winnerGAs.put( ArtifactIdUtils.toVersionlessId( node.getArtifact() ), node.getArtifact() ); | ||
} | ||
|
||
int getWinnerDepth( DependencyNode node ) | ||
{ | ||
return winnerDepths.getOrDefault( node.getArtifact(), Integer.MAX_VALUE ); | ||
} | ||
|
||
} | ||
|
||
|
||
static final class CoordinateManager | ||
{ | ||
private final Map<Integer, AtomicInteger> sequenceGen = new HashMap<>( 256 ); | ||
|
||
/** | ||
* Dependency node -> Coordinate | ||
*/ | ||
private final Map<DependencyNode, Coordinate> coordinateMap = new HashMap<>( 256 ); | ||
|
||
/** | ||
* Leftmost coordinate of given artifact | ||
*/ | ||
private final Map<Artifact, Coordinate> leftmostCoordinates = new HashMap<>( 256 ); | ||
|
||
|
||
Coordinate getCoordinate( DependencyNode node ) | ||
{ | ||
return coordinateMap.get( node ); | ||
} | ||
|
||
Coordinate createCoordinate( DependencyNode node, int depth ) | ||
{ | ||
int seq = sequenceGen.computeIfAbsent( depth, k -> new AtomicInteger() ).incrementAndGet(); | ||
Coordinate coordinate = new Coordinate( depth, seq ); | ||
coordinateMap.put( node, coordinate ); | ||
return coordinate; | ||
} | ||
|
||
void updateLeftmost( DependencyNode current ) | ||
{ | ||
leftmostCoordinates.put( current.getArtifact(), getCoordinate( current ) ); | ||
} | ||
|
||
boolean isLeftmost( DependencyNode node, List<DependencyNode> parents ) | ||
{ | ||
Coordinate leftmost = leftmostCoordinates.get( node.getArtifact() ); | ||
if ( leftmost != null && leftmost.depth <= parents.size() ) | ||
{ | ||
DependencyNode sameLevelNode = parents.get( leftmost.depth - 1 ); | ||
if ( getCoordinate( sameLevelNode ).sequence < leftmost.sequence ) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
static final class Coordinate | ||
{ | ||
int depth; | ||
int sequence; | ||
|
||
Coordinate( int depth, int sequence ) | ||
{ | ||
this.depth = depth; | ||
this.sequence = sequence; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "{" | ||
+ "depth=" | ||
+ depth | ||
+ ", sequence=" | ||
+ sequence | ||
+ '}'; | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.