camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/6] camel git commit: Optimise OrderedComparator to use shared instance to reduce memory footprint
Date Sat, 27 May 2017 12:47:22 GMT
Optimise OrderedComparator to use shared instance to reduce memory footprint


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/55af2c3f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/55af2c3f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/55af2c3f

Branch: refs/heads/master
Commit: 55af2c3f4ec3b8b710d80676ee04f0780032e916
Parents: faf98c8
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat May 27 10:56:12 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat May 27 10:56:12 2017 +0200

----------------------------------------------------------------------
 .../apache/camel/impl/DefaultCamelContext.java  |  4 +--
 .../camel/processor/CamelInternalProcessor.java |  2 +-
 .../processor/interceptor/DefaultChannel.java   |  2 +-
 .../apache/camel/util/OrderedComparator.java    | 17 ++++++++++
 .../org/apache/camel/util/UnitOfWorkHelper.java |  6 ++--
 .../camel/util/OrderedComparatorTest.java       | 34 ++++++++++++++++++++
 6 files changed, 58 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 5869183..7ed1d10 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -3677,7 +3677,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         doWarmUpRoutes(inputs, startConsumer);
 
         // sort the startup listeners so they are started in the right order
-        startupListeners.sort(new OrderedComparator());
+        startupListeners.sort(OrderedComparator.get());
         // now call the startup listeners where the routes has been warmed up
         // (only the actual route consumer has not yet been started)
         for (StartupListener startup : startupListeners) {
@@ -3701,7 +3701,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         }
 
         // sort the startup listeners so they are started in the right order
-        startupListeners.sort(new OrderedComparator());
+        startupListeners.sort(OrderedComparator.get());
         // now the consumers that was just started may also add new StartupListeners (such
as timer)
         // so we need to ensure they get started as well
         for (StartupListener startup : startupListeners) {

http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
b/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
index c98b1b0..33bda1b 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java
@@ -100,7 +100,7 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor {
     public void addAdvice(CamelInternalProcessorAdvice advice) {
         advices.add(advice);
         // ensure advices are sorted so they are in the order we want
-        advices.sort(new OrderedComparator());
+        advices.sort(OrderedComparator.get());
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
index e4d317d..e789ab1 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
@@ -260,7 +260,7 @@ public class DefaultChannel extends CamelInternalProcessor implements
ModelChann
         }
 
         // sort interceptors according to ordered
-        interceptors.sort(new OrderedComparator());
+        interceptors.sort(OrderedComparator.get());
         // then reverse list so the first will be wrapped last, as it would then be first
being invoked
         Collections.reverse(interceptors);
         // wrap the output with the configured interceptors

http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java b/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
index caa918a..5462a99 100644
--- a/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
+++ b/camel-core/src/main/java/org/apache/camel/util/OrderedComparator.java
@@ -27,12 +27,29 @@ import org.apache.camel.Ordered;
  */
 public final class OrderedComparator implements Comparator<Object> {
 
+    private static final OrderedComparator INSTANCE = new OrderedComparator();
+    private static final OrderedComparator INSTANCE_REVERSE = new OrderedComparator(true);
+
+    public static OrderedComparator get() {
+        return INSTANCE;
+    }
+
+    public static OrderedComparator getReverse() {
+        return INSTANCE_REVERSE;
+    }
+
     private final boolean reverse;
 
+    /**
+     * Favor using the static instance {@link #get()}
+     */
     public OrderedComparator() {
         this(false);
     }
 
+    /**
+     * Favor using the static instance {@link #getReverse()}
+     */
     public OrderedComparator(boolean reverse) {
         this.reverse = reverse;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/main/java/org/apache/camel/util/UnitOfWorkHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/UnitOfWorkHelper.java b/camel-core/src/main/java/org/apache/camel/util/UnitOfWorkHelper.java
index 350d02e..6446a40 100644
--- a/camel-core/src/main/java/org/apache/camel/util/UnitOfWorkHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/UnitOfWorkHelper.java
@@ -91,7 +91,7 @@ public final class UnitOfWorkHelper {
             // reverse so we invoke it FILO style instead of FIFO
             Collections.reverse(copy);
             // and honor if any was ordered by sorting it accordingly
-            copy.sort(new OrderedComparator());
+            copy.sort(OrderedComparator.get());
 
             // invoke synchronization callbacks
             for (Synchronization synchronization : copy) {
@@ -119,7 +119,7 @@ public final class UnitOfWorkHelper {
             // reverse so we invoke it FILO style instead of FIFO
             Collections.reverse(copy);
             // and honor if any was ordered by sorting it accordingly
-            copy.sort(new OrderedComparator());
+            copy.sort(OrderedComparator.get());
 
             // invoke synchronization callbacks
             for (Synchronization synchronization : copy) {
@@ -144,7 +144,7 @@ public final class UnitOfWorkHelper {
             // reverse so we invoke it FILO style instead of FIFO
             Collections.reverse(copy);
             // and honor if any was ordered by sorting it accordingly
-            copy.sort(new OrderedComparator());
+            copy.sort(OrderedComparator.get());
 
             // invoke synchronization callbacks
             for (Synchronization synchronization : copy) {

http://git-wip-us.apache.org/repos/asf/camel/blob/55af2c3f/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java b/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java
index 2c89e77..aa2abb9 100644
--- a/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/OrderedComparatorTest.java
@@ -28,6 +28,23 @@ import org.apache.camel.Ordered;
  */
 public class OrderedComparatorTest extends TestCase {
 
+    public void testOrderedComparatorGet() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(1));
+        answer.add(new MyOrder(5));
+        answer.add(new MyOrder(4));
+
+        answer.sort(OrderedComparator.get());
+
+        assertEquals(0, answer.get(0).getOrder());
+        assertEquals(1, answer.get(1).getOrder());
+        assertEquals(2, answer.get(2).getOrder());
+        assertEquals(4, answer.get(3).getOrder());
+        assertEquals(5, answer.get(4).getOrder());
+    }
+
     public void testOrderedComparator() throws Exception {
         List<Ordered> answer = new ArrayList<Ordered>();
         answer.add(new MyOrder(0));
@@ -45,6 +62,23 @@ public class OrderedComparatorTest extends TestCase {
         assertEquals(5, answer.get(4).getOrder());
     }
 
+    public void testOrderedComparatorGetReverse() throws Exception {
+        List<Ordered> answer = new ArrayList<Ordered>();
+        answer.add(new MyOrder(0));
+        answer.add(new MyOrder(2));
+        answer.add(new MyOrder(1));
+        answer.add(new MyOrder(5));
+        answer.add(new MyOrder(4));
+
+        answer.sort(OrderedComparator.getReverse());
+
+        assertEquals(5, answer.get(0).getOrder());
+        assertEquals(4, answer.get(1).getOrder());
+        assertEquals(2, answer.get(2).getOrder());
+        assertEquals(1, answer.get(3).getOrder());
+        assertEquals(0, answer.get(4).getOrder());
+    }
+
     public void testOrderedComparatorReverse() throws Exception {
         List<Ordered> answer = new ArrayList<Ordered>();
         answer.add(new MyOrder(0));


Mime
View raw message