You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by st...@apache.org on 2019/07/29 14:17:42 UTC

[johnzon] 02/02: JOHNZON-226 performance: avoid Array.getLength

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

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

commit f2d04b6155861e23eb779fa7be0f948276c5c7c3
Author: Mark Struberg <st...@apache.org>
AuthorDate: Mon Jul 29 15:22:01 2019 +0200

    JOHNZON-226 performance: avoid Array.getLength
    
    Array.getLength performs rather poor as it doesn't get inlined
    by the JIT due to heavy reflection.
---
 .../johnzon/mapper/MappingGeneratorImpl.java       | 35 +++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index f741aff..1b4d4e8 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -470,7 +470,7 @@ public class MappingGeneratorImpl implements MappingGenerator {
      * @param key either the attribute key or {@code null} if the array should be rendered without key
      */
     private void writeArray(Class<?> type, Adapter itemConverter, String key, Object arrayValue, Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
-        final int length = Array.getLength(arrayValue);
+        final int length = getArrayLength(arrayValue);
         if (length == 0 && config.isSkipEmptyArray()) {
             return;
         }
@@ -582,6 +582,39 @@ public class MappingGeneratorImpl implements MappingGenerator {
         generator.writeEnd();
     }
 
+    private int getArrayLength(Object array) {
+        // Note: all types of multidimensional arrays are instanceof Object[]
+        if (array instanceof Object[]) {
+            return ((Object[]) array).length;
+        }
+        if (array instanceof boolean[]) {
+            return ((boolean[])array).length;
+        }
+        if (array instanceof byte[]) {
+            return ((byte[])array).length;
+        }
+        if (array instanceof char[]) {
+            return ((char[]) array).length;
+        }
+        if (array instanceof short[]) {
+            return ((short[]) array).length;
+        }
+        if (array instanceof int[]) {
+            return ((int[]) array).length;
+        }
+        if (array instanceof long[]) {
+            return ((long[]) array).length;
+        }
+        if (array instanceof float[]) {
+            return ((float[]) array).length;
+        }
+        if (array instanceof double[]) {
+            return ((double[]) array).length;
+        }
+
+        throw new IllegalArgumentException("This is not an array! " + array);
+    }
+
     private void writeItem(final Object o, final Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
         if (o == null) {
             generator.writeNull();