[camel] branch master updated: CAMEL-14354: camel-core - Optimize

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-14354: camel-core - Optimize

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 863ff5a  CAMEL-14354: camel-core - Optimize
863ff5a is described below

commit 863ff5ad48061b580e9e6e753e2432179d2cc981
Author: Claus Ibsen <[hidden email]>
AuthorDate: Tue Jan 14 07:03:57 2020 +0100

    CAMEL-14354: camel-core - Optimize
---
 .../camel/spi/CamelInternalProcessorAdvice.java    |  7 +++
 .../camel/processor/CamelInternalProcessor.java    | 55 +++++++++++++++++++---
 .../org/apache/camel/processor/ContractAdvice.java |  7 ++-
 3 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/CamelInternalProcessorAdvice.java b/core/camel-api/src/main/java/org/apache/camel/spi/CamelInternalProcessorAdvice.java
index 85728ee..3572a90 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/CamelInternalProcessorAdvice.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/CamelInternalProcessorAdvice.java
@@ -47,4 +47,11 @@ public interface CamelInternalProcessorAdvice<T> {
      */
     void after(Exchange exchange, T data) throws Exception;
 
+    /**
+     * Whether this advice has state or not.
+     */
+    default boolean hasState() {
+        return true;
+    }
+
 }
diff --git a/core/camel-base/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java b/core/camel-base/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
index f005d68..9be15bb 100644
--- a/core/camel-base/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
+++ b/core/camel-base/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
@@ -90,7 +90,10 @@ import org.slf4j.LoggerFactory;
  */
 public class CamelInternalProcessor extends DelegateAsyncProcessor {
 
+    private static final Object[] EMPTY_STATES = new Object[0];
+
     private final List<CamelInternalProcessorAdvice<?>> advices = new ArrayList<>();
+    private byte statefulAdvices;
 
     public CamelInternalProcessor() {
     }
@@ -108,6 +111,10 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
         advices.add(advice);
         // ensure advices are sorted so they are in the order we want
         advices.sort(OrderedComparator.get());
+
+        if (advice.hasState()) {
+            statefulAdvices++;
+        }
     }
 
     /**
@@ -148,14 +155,16 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
             return true;
         }
 
-        // optimise to use object array for states
-        final Object[] states = new Object[advices.size()];
+        // optimise to use object array for states, and only for the number of advices that keep state
+        final Object[] states = statefulAdvices > 0 ? new Object[statefulAdvices] : EMPTY_STATES;
         // optimise for loop using index access to avoid creating iterator object
-        for (int i = 0; i < advices.size(); i++) {
+        for (int i = 0, j = 0; i < advices.size(); i++) {
             CamelInternalProcessorAdvice task = advices.get(i);
             try {
                 Object state = task.before(exchange);
-                states[i] = state;
+                if (task.hasState()) {
+                    states[j++] = state;
+                }
             } catch (Throwable e) {
                 exchange.setException(e);
                 originalCallback.done(true);
@@ -166,9 +175,12 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
         // create internal callback which will execute the advices in reverse order when done
         AsyncCallback callback = doneSync -> {
             try {
-                for (int i = advices.size() - 1; i >= 0; i--) {
+                for (int i = advices.size() - 1, j = states.length - 1; i >= 0; i--) {
                     CamelInternalProcessorAdvice task = advices.get(i);
-                    Object state = states[i];
+                    Object state = null;
+                    if (task.hasState()) {
+                        state = states[j--];
+                    }
                     try {
                         task.after(exchange, state);
                     } catch (Throwable e) {
@@ -306,6 +318,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
                 uow.afterRoute(exchange, route);
             }
         }
+
+        @Override
+        public boolean hasState() {
+            return false;
+        }
     }
 
     /**
@@ -331,6 +348,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
         public void after(Exchange exchange, Object state) throws Exception {
             inflightRepository.remove(exchange, id);
         }
+
+        @Override
+        public boolean hasState() {
+            return false;
+        }
     }
 
     /**
@@ -406,6 +428,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
             }
             return false;
         }
+
+        @Override
+        public boolean hasState() {
+            return false;
+        }
     }
 
     /**
@@ -455,11 +482,15 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
         }
 
         @Override
+        public boolean hasState() {
+            return false;
+        }
+
+        @Override
         public int getOrder() {
             // we want tracer just before calling the processor
             return Ordered.LOWEST - 1;
         }
-
     }
 
     /**
@@ -757,6 +788,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
         public void after(Exchange exchange, Object data) throws Exception {
             // noop
         }
+
+        @Override
+        public boolean hasState() {
+            return false;
+        }
     }
 
     /**
@@ -798,6 +834,11 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
             tracer.traceAfterNode(processorDefinition, exchange);
         }
 
+        @Override
+        public boolean hasState() {
+            return false;
+        }
+
         private static final class TracingAfterRoute extends SynchronizationAdapter {
 
             private final Tracer tracer;
diff --git a/core/camel-base/src/main/java/org/apache/camel/processor/ContractAdvice.java b/core/camel-base/src/main/java/org/apache/camel/processor/ContractAdvice.java
index e314b77..79293e3 100644
--- a/core/camel-base/src/main/java/org/apache/camel/processor/ContractAdvice.java
+++ b/core/camel-base/src/main/java/org/apache/camel/processor/ContractAdvice.java
@@ -110,7 +110,12 @@ public class ContractAdvice implements CamelInternalProcessorAdvice {
             exchange.setException(e);
         }
     }
-    
+
+    @Override
+    public boolean hasState() {
+        return false;
+    }
+
     private void doTransform(Message message, DataType from, DataType to) throws Exception {
         if (from == null) {
             // If 'from' is null, only Java-Java convertion is performed.