You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/09/29 07:36:06 UTC

[2/4] karaf git commit: Cache unmarshalled Features

Cache unmarshalled Features

If we are processing a deep feature tree with multiple dependencies
referencing same features (like in the case of OpenDaylight), we end
up unmarshalling the same features over and over again.

Rather than doing that, instantiate a cache, which will hold a weak
reference to features already encountered.

Before:
real    1m46.523s
user    1m59.258s
sys     0m17.048s

After:
real    0m42.642s
user    1m0.892s
sys     0m10.148s

Signed-off-by: Robert Varga <ni...@hq.sk>
(cherry picked from commit 9714d76be6442faca13e6f45f02a27eac52b8ebb)


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/8d55bc78
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/8d55bc78
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/8d55bc78

Branch: refs/heads/karaf-4.0.x
Commit: 8d55bc782eb1a135b47b9eb3e989dd8d9244d88a
Parents: cc857df
Author: Robert Varga <ni...@hq.sk>
Authored: Sun Aug 13 19:38:26 2017 +0200
Committer: Robert Varga <ni...@hq.sk>
Committed: Wed Sep 27 13:33:00 2017 +0200

----------------------------------------------------------------------
 .../features/GenerateDescriptorMojo.java        | 30 ++++++++++++++++----
 1 file changed, 24 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/8d55bc78/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index 813e412..2d367dc 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -39,6 +39,7 @@ import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.WeakHashMap;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 
@@ -439,7 +440,8 @@ public class GenerateDescriptorMojo extends MojoSupport {
         // the feature's Maven artifact to allow for multi-feature repositories)
         // TODO Initialise the repositories from the existing feature file if any
         Map<Dependency, Feature> otherFeatures = new HashMap<>();
-        Map<Feature, String> featureRepositories = new HashMap<Feature, String>();
+        Map<Feature, String> featureRepositories = new HashMap<>();
+        FeaturesCache cache = new FeaturesCache();
         for (final LocalDependency entry : localDependencies) {
             Object artifact = entry.getArtifact();
 
@@ -447,9 +449,11 @@ public class GenerateDescriptorMojo extends MojoSupport {
                 continue;
             }
 
-            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, artifact, entry.getParent(),
-                    true);
+            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache, artifact,
+                    entry.getParent(), true);
         }
+        // Do not retain cache beyond this point
+        cache = null;
 
         // Second pass to look for bundles
         if (addBundlesToPrimaryFeature) {
@@ -541,7 +545,7 @@ public class GenerateDescriptorMojo extends MojoSupport {
     }
 
     private void processFeatureArtifact(Features features, Feature feature, Map<Dependency, Feature> otherFeatures,
-                                        Map<Feature, String> featureRepositories,
+                                        Map<Feature, String> featureRepositories, FeaturesCache cache,
                                         Object artifact, Object parent, boolean add)
             throws MojoExecutionException, XMLStreamException, JAXBException, IOException {
         if (this.dependencyHelper.isArtifactAFeature(artifact) && FEATURE_CLASSIFIER.equals(
@@ -551,9 +555,9 @@ public class GenerateDescriptorMojo extends MojoSupport {
                 throw new MojoExecutionException(
                         "Cannot locate file for feature: " + artifact + " at " + featuresFile);
             }
-            Features includedFeatures = readFeaturesFile(featuresFile);
+            Features includedFeatures = cache.get(featuresFile);
             for (String repository : includedFeatures.getRepository()) {
-                processFeatureArtifact(features, feature, otherFeatures, featureRepositories,
+                processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache,
                         new DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false);
             }
             for (Feature includedFeature : includedFeatures.getFeature()) {
@@ -920,4 +924,18 @@ public class GenerateDescriptorMojo extends MojoSupport {
         return "\tTree listing is saved here: " + treeListFile.getAbsolutePath() + "\n";
     }
 
+    private static final class FeaturesCache {
+        private final Map<File, Features> map = new WeakHashMap<>();
+
+        Features get(final File featuresFile) throws XMLStreamException, JAXBException, IOException {
+            final Features existing = map.get(featuresFile);
+            if (existing != null) {
+                return existing;
+            }
+
+            final Features computed = readFeaturesFile(featuresFile);
+            map.put(featuresFile, computed);
+            return computed;
+        }
+    }
 }