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

apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 7a4cae56a52 CAMEL-20884 - Cache Index from jandex.idx defined in jar
7a4cae56a52 is described below

commit 7a4cae56a52d995cba8921317bc4c9f472432dc8
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Wed Jun 19 13:39:51 2024 +0200

    CAMEL-20884 - Cache Index from jandex.idx defined in jar
    
    Recreating in-memory the Java Object Index from the jandex.idx was done
    approximately 9 times per components. This action is relatively long. In
    fact, only a few different jars require this operation.
    
    It was representing 25% of the time spent in camel classes when calling
    `mvn clean install -D quickly`
    
    To give an order of magnitude, locally build time with timestamped went
    from 9'45" to 8'23" globally. The time spent in camel-package Maven
    plugin reported decreased from 54" to 39"
    
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
---
 .../camel/maven/packaging/SpiGeneratorMojo.java    | 29 ++++++++++++++--------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
index 5263e48619f..6b4f4ad906a 100644
--- 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
@@ -29,6 +29,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.regex.Matcher;
@@ -48,6 +49,7 @@ import org.jboss.jandex.AnnotationTarget.Kind;
 import org.jboss.jandex.ClassInfo.NestingType;
 import org.jboss.jandex.CompositeIndex;
 import org.jboss.jandex.DotName;
+import org.jboss.jandex.Index;
 import org.jboss.jandex.IndexReader;
 import org.jboss.jandex.IndexView;
 import org.jboss.jandex.Indexer;
@@ -58,6 +60,7 @@ public class SpiGeneratorMojo extends AbstractGeneratorMojo {
 
     public static final DotName SERVICE_FACTORY = 
DotName.createSimple(ServiceFactory.class.getName());
     public static final DotName CONSTANT_PROVIDER = 
DotName.createSimple(ConstantProvider.class.getName());
+    private static final Map<String, Index> JANDEX_CACHE = new 
ConcurrentHashMap<>();
 
     @Parameter(defaultValue = "${project.build.outputDirectory}")
     protected File classesDirectory;
@@ -193,17 +196,22 @@ public class SpiGeneratorMojo extends 
AbstractGeneratorMojo {
     }
 
     private void addIndex(List<IndexView> indices, String cpe) throws 
IOException {
-        try (JarFile jf = new JarFile(cpe)) {
-            JarEntry indexEntry = jf.getJarEntry("META-INF/jandex.idx");
-            if (indexEntry != null) {
-                readIndexFromJandex(indices, jf, indexEntry);
-            } else {
-                createIndexFromClass(indices, jf);
+        Index index = JANDEX_CACHE.get(cpe);
+        if (index == null) {
+            try (JarFile jf = new JarFile(cpe)) {
+                JarEntry indexEntry = jf.getJarEntry("META-INF/jandex.idx");
+                if (indexEntry != null) {
+                    index = readIndexFromJandex(jf, indexEntry);
+                } else {
+                    index = createIndexFromClass(jf);
+                }
+                JANDEX_CACHE.put(cpe, index);
             }
         }
+        indices.add(index);
     }
 
-    private void createIndexFromClass(List<IndexView> indices, JarFile jf) 
throws IOException {
+    private Index createIndexFromClass(JarFile jf) throws IOException {
         final Indexer indexer = new Indexer();
 
         List<JarEntry> classes = jf.stream()
@@ -215,13 +223,12 @@ public class SpiGeneratorMojo extends 
AbstractGeneratorMojo {
                 indexer.index(is);
             }
         }
-
-        indices.add(indexer.complete());
+        return indexer.complete();
     }
 
-    private void readIndexFromJandex(List<IndexView> indices, JarFile jf, 
JarEntry indexEntry) throws IOException {
+    private Index readIndexFromJandex(JarFile jf, JarEntry indexEntry) throws 
IOException {
         try (InputStream is = jf.getInputStream(indexEntry)) {
-            indices.add(new IndexReader(is).read());
+            return new IndexReader(is).read();
         }
     }
 

Reply via email to