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
commit 557efba1617d15082a7f6fb6b81c48102e7d23f7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Mar 14 17:36:13 2021 +0100 camel-core - Small optimization on Choice EIP --- .../apache/camel/processor/ChoiceProcessor.java | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ChoiceProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ChoiceProcessor.java index bf11759..6fce791 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ChoiceProcessor.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ChoiceProcessor.java @@ -17,6 +17,7 @@ package org.apache.camel.processor; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.camel.AsyncCallback; @@ -46,12 +47,15 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P private String id; private String routeId; - private final List<FilterProcessor> filters; + // optimize to use an array + private final FilterProcessor[] filters; + private final int len; private final AsyncProcessor otherwise; private transient long notFiltered; public ChoiceProcessor(List<FilterProcessor> filters, Processor otherwise) { - this.filters = filters; + this.filters = filters.toArray(new FilterProcessor[0]); + this.len = filters.size(); this.otherwise = otherwise != null ? AsyncProcessorConverterHelper.convert(otherwise) : null; } @@ -72,7 +76,8 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P }; // find the first matching filter and process the exchange using it - for (FilterProcessor filter : filters) { + for (int i = 0; i < len; i++) { + FilterProcessor filter = filters[i]; // evaluate the predicate on filter predicate early to be faster // and avoid issues when having nested choices // as we should only pick one processor @@ -121,7 +126,7 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P } public List<FilterProcessor> getFilters() { - return filters; + return Arrays.asList(filters); } public Processor getOtherwise() { @@ -139,8 +144,8 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P * Reset counters. */ public void reset() { - for (FilterProcessor filter : filters) { - filter.reset(); + for (int i = 0; i < len; i++) { + filters[i].reset(); } notFiltered = 0; } @@ -151,8 +156,8 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P return null; } List<Processor> answer = new ArrayList<>(); - if (!filters.isEmpty()) { - answer.addAll(filters); + if (len > 0) { + answer.addAll(Arrays.asList(filters)); } if (otherwise != null) { answer.add(otherwise); @@ -162,7 +167,7 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P @Override public boolean hasNext() { - return otherwise != null || !filters.isEmpty(); + return otherwise != null || len > 0; } @Override @@ -187,21 +192,22 @@ public class ChoiceProcessor extends AsyncProcessorSupport implements Navigate<P @Override protected void doInit() throws Exception { - ServiceHelper.initService(filters, otherwise); + ServiceHelper.initService(Arrays.asList(filters), otherwise); } @Override protected void doStart() throws Exception { - ServiceHelper.startService(filters, otherwise); + ServiceHelper.startService(Arrays.asList(filters), otherwise); } @Override protected void doStop() throws Exception { - ServiceHelper.stopService(otherwise, filters); + ServiceHelper.stopService(otherwise, Arrays.asList(filters)); } @Override protected void doShutdown() throws Exception { - ServiceHelper.stopAndShutdownServices(otherwise, filters); + ServiceHelper.stopAndShutdownServices(otherwise, Arrays.asList(filters)); } + }