You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/10/30 10:49:51 UTC

(camel) branch main updated: (chores) core: use faster type checks when possible

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

orpiske 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 69492a40949  (chores) core: use faster type checks when possible
69492a40949 is described below

commit 69492a409494a457717b7307abb75d8b91c200fb
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Oct 30 10:18:16 2023 +0100

     (chores) core: use faster type checks when possible
    
    This prioritizes faster type checks for most common and/or simple types whenever possible/safe to do so
---
 .../org/apache/camel/support/ObjectHelper.java     | 42 +++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
index 33526c2c926..c1876c76482 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
@@ -22,11 +22,18 @@ import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.TreeMap;
+import java.util.WeakHashMap;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
@@ -782,9 +789,23 @@ public final class ObjectHelper {
 
         if (value == null) {
             return Collections.emptyList();
-        } else if (value instanceof String) {
+        }
+        if (fastStringCheck(value)) {
             return createStringIterator((String) value, delimiter, allowEmptyValues, pattern);
-        } else if (value instanceof Iterator) {
+        }
+        if (fastIsMap(value)) {
+            Map<?, ?> map = (Map<?, ?>) value;
+            return map.entrySet();
+        }
+        if (value.getClass().isArray()) {
+            return createArrayIterator(value);
+        }
+
+        return trySlowIterables(value);
+    }
+
+    private static Iterable<?> trySlowIterables(Object value) {
+        if (value instanceof Iterator) {
             final Iterator<Object> iterator = (Iterator<Object>) value;
             return (Iterable<Object>) () -> iterator;
         } else if (value instanceof Iterable) {
@@ -792,8 +813,6 @@ public final class ObjectHelper {
         } else if (value instanceof Map) {
             Map<?, ?> map = (Map<?, ?>) value;
             return map.entrySet();
-        } else if (value.getClass().isArray()) {
-            return createArrayIterator(value);
         } else if (value instanceof NodeList) {
             // lets iterate through DOM results after performing XPaths
             final NodeList nodeList = (NodeList) value;
@@ -803,6 +822,21 @@ public final class ObjectHelper {
         }
     }
 
+    private static boolean fastStringCheck(Object obj) {
+        return obj.getClass() == String.class;
+    }
+
+    private static boolean fastIsMap(Object obj) {
+        return obj.getClass() == HashMap.class
+                || obj.getClass() == ConcurrentHashMap.class
+                || obj.getClass() == ConcurrentSkipListMap.class
+                || obj.getClass() == EnumMap.class
+                || obj.getClass() == LinkedHashMap.class
+                || obj.getClass() == TreeMap.class
+                || obj.getClass() == WeakHashMap.class;
+
+    }
+
     private static Iterable<Object> createArrayIterator(Object value) {
         if (org.apache.camel.util.ObjectHelper.isPrimitiveArrayType(value.getClass())) {
             final Object array = value;