You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ap...@apache.org on 2024/06/19 09:54:47 UTC

(camel) branch main updated: CAMEL-20884 - improve Velocity usage in Camel package Maven plugin

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 49ec7bc1bf4 CAMEL-20884 - improve Velocity usage in Camel package Maven plugin
49ec7bc1bf4 is described below

commit 49ec7bc1bf4a2fe3dfb84e420b74489506f551fc
Author: Aurélien Pupier <ap...@redhat.com>
AuthorDate: Wed Jun 19 09:49:20 2024 +0200

    CAMEL-20884 - improve Velocity usage in Camel package Maven plugin
    
    - use a single RuntimeInstance of Velocity given that it is initialized
    always with the same parameters It was retrieved at least 2 times per
    component.
    - use a cache to retrieve Velocity Template. It was retrieved at least 2
    times per component. So now there is only 15 calls.
    - Initialize the VelocityContext with the map of parameter in the
    constructor instead of iterating on each element of the map and adding
    them one by one
    
    it was using around 10% of the time spent in org.apache.camel.** classes
    during a `mvn clean install -D quickly`. Locally went from around 10'05"
    to around 9'20" to execute previously mentioned command
    
    Signed-off-by: Aurélien Pupier <ap...@redhat.com>
---
 .../maven/packaging/AbstractGeneratorMojo.java      | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java
index 75bccad91b2..04b7b51a5d2 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java
@@ -62,6 +62,8 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo {
     public static final String NL = "\n";
 
     private static final Map<String, Class<?>> KNOWN_CLASSES_CACHE = new ConcurrentHashMap<>();
+    private static final RuntimeInstance VELOCITY = createVelocityRuntime();
+    private static final Map<String, Template> VELOCITY_TEMPLATES = new ConcurrentHashMap<String, Template>();
 
     /**
      * The maven project.
@@ -97,7 +99,6 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo {
         KNOWN_CLASSES_CACHE.put("int", int.class);
         KNOWN_CLASSES_CACHE.put("long", long.class);
         KNOWN_CLASSES_CACHE.put("boolean", boolean.class);
-
     }
 
     public void execute(
@@ -120,19 +121,21 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo {
     }
 
     protected String velocity(String templatePath, Map<String, Object> ctx) {
+        VelocityContext context = new VelocityContext(ctx);
+        Template template = VELOCITY_TEMPLATES.computeIfAbsent(templatePath, VELOCITY::getTemplate);
+
+        StringWriter writer = new StringWriter();
+        template.merge(context, writer);
+        return writer.toString();
+    }
+
+    private static RuntimeInstance createVelocityRuntime() {
         Properties props = new Properties();
         props.setProperty("resource.loaders", "class");
         props.setProperty("resource.loader.class.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
         RuntimeInstance velocity = new RuntimeInstance();
         velocity.init(props);
-
-        VelocityContext context = new VelocityContext();
-        ctx.forEach(context::put);
-
-        Template template = velocity.getTemplate(templatePath);
-        StringWriter writer = new StringWriter();
-        template.merge(context, writer);
-        return writer.toString();
+        return velocity;
     }
 
     protected boolean updateResource(Path dir, String fileName, String data) {