You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/09/03 05:04:33 UTC

[isis] branch master updated: ISIS-2774: some heap optimizations with Can

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a4f5f90  ISIS-2774: some heap optimizations with Can<T>
a4f5f90 is described below

commit a4f5f90af9bc94b0dfc999fac72cd1d4bb92e6c0
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Sep 3 07:04:23 2021 +0200

    ISIS-2774: some heap optimizations with Can<T>
---
 .../org/apache/isis/commons/collections/Can.java   | 82 +++++++---------------
 .../isis/commons/collections/Can_Multiple.java     |  9 +--
 .../isis/commons/collections/Can_Singleton.java    |  4 +-
 .../isis/commons/collections/_CanFactory.java      | 69 ++++++++++++++++++
 4 files changed, 100 insertions(+), 64 deletions(-)

diff --git a/commons/src/main/java/org/apache/isis/commons/collections/Can.java b/commons/src/main/java/org/apache/isis/commons/collections/Can.java
index fbf6eeb..a6f4566 100644
--- a/commons/src/main/java/org/apache/isis/commons/collections/Can.java
+++ b/commons/src/main/java/org/apache/isis/commons/collections/Can.java
@@ -228,28 +228,11 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
             return empty();
         }
 
-        // this is just an optimization, to pre-allocate a reasonable list size,
-        // specifically targeted at small list sizes
-        val maxSize = Math.min(array.length, 1024);
-
         val nonNullElements = Stream.of(array)
                 .filter(_NullSafe::isPresent)
-                .collect(Collectors.toCollection(()->new ArrayList<>(maxSize)));
-
-        nonNullElements.trimToSize(); // in case we have a 'sparse' collection as input to this method
-
-        val size = nonNullElements.size();
-
-        if(size==0) {
-            return empty();
-        }
-
-        if(size==1) {
-            return ofSingleton(((List<T>)nonNullElements).get(0));
-        }
-
-        return Can_Multiple.of(nonNullElements);
+                .collect(_CanFactory.toListWithSizeUpperBound(array.length));
 
+        return _CanFactory.ofNonNullElements(nonNullElements);
     }
 
     /**
@@ -262,31 +245,17 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
      */
     public static <T> Can<T> ofCollection(final @Nullable Collection<T> collection) {
 
-        if(_NullSafe.size(collection)==0) {
+        val inputSize = _NullSafe.size(collection);
+
+        if(inputSize==0) {
             return empty();
         }
 
-        // this is just an optimization, to pre-allocate a reasonable list size,
-        // specifically targeted at small list sizes
-        val maxSize = Math.min(collection.size(), 1024);
-
         val nonNullElements = collection.stream()
                 .filter(_NullSafe::isPresent)
-                .collect(Collectors.toCollection(()->new ArrayList<>(maxSize)));
+                .collect(_CanFactory.toListWithSizeUpperBound(inputSize));
 
-        nonNullElements.trimToSize(); // in case we have a 'sparse' collection as input to this method
-
-        val size = nonNullElements.size();
-
-        if(size==0) {
-            return empty();
-        }
-
-        if(size==1) {
-            return ofSingleton(((List<T>)nonNullElements).get(0));
-        }
-
-        return Can_Multiple.of(nonNullElements);
+        return _CanFactory.ofNonNullElements(nonNullElements);
     }
 
     /**
@@ -303,10 +272,14 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
             return empty();
         }
 
-        val elements = new ArrayList<T>();
-        iterable.forEach(elements::add);
+        val nonNullElements = new ArrayList<T>();
+        iterable.forEach(element->{
+            if(element!=null) {
+                nonNullElements.add(element);
+            }
+        });
 
-        return ofCollection(elements);
+        return _CanFactory.ofNonNullElements(nonNullElements);
     }
 
     /**
@@ -325,11 +298,14 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
             return empty();
         }
 
-        val elements = new ArrayList<T>();
+        val nonNullElements = new ArrayList<T>();
         while(enumeration.hasMoreElements()) {
-            elements.add(enumeration.nextElement());
+            val element = enumeration.nextElement();
+            if(element!=null) {
+                nonNullElements.add(element);
+            }
         }
-        return ofCollection(elements);
+        return _CanFactory.ofNonNullElements(nonNullElements);
     }
 
     /**
@@ -352,17 +328,7 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
                 .filter(_NullSafe::isPresent)
                 .collect(Collectors.toCollection(()->new ArrayList<>()));
 
-        val size = nonNullElements.size();
-
-        if(size==0) {
-            return empty();
-        }
-
-        if(size==1) {
-            return ofSingleton(((List<T>)nonNullElements).get(0));
-        }
-
-        return Can_Multiple.of(nonNullElements);
+        return _CanFactory.ofNonNullElements(nonNullElements);
     }
 
     /**
@@ -435,13 +401,13 @@ extends Iterable<T>, Comparable<Can<T>>, Serializable {
             return empty();
         }
 
-        val mappedElements =
+        val nonNullMappedElements =
                 stream()
                 .map(mapper)
                 .filter(_NullSafe::isPresent)
-                .collect(Collectors.toCollection(ArrayList::new));
+                .collect(_CanFactory.toListWithSizeUpperBound(size()));
 
-        return ofCollection(mappedElements);
+        return _CanFactory.ofNonNullElements(nonNullMappedElements);
     }
 
     // -- CONCATENATION
diff --git a/commons/src/main/java/org/apache/isis/commons/collections/Can_Multiple.java b/commons/src/main/java/org/apache/isis/commons/collections/Can_Multiple.java
index 1c7c7d5..a2d7bba 100644
--- a/commons/src/main/java/org/apache/isis/commons/collections/Can_Multiple.java
+++ b/commons/src/main/java/org/apache/isis/commons/collections/Can_Multiple.java
@@ -123,14 +123,15 @@ final class Can_Multiple<T> implements Can<T> {
 
     @Override
     public Can<T> unique(final @NonNull BiPredicate<T, T> equality) {
-        val list = _Lists.<T>newArrayList();
+        final int initialSize = Math.min(1024, elements.size());
+        val uniqueElements = _Lists.<T>newArrayList(initialSize);
         elements
         .forEach(element->{
-            if(!list.stream().anyMatch(x->equality.test(x, element))) {
-                list.add(element);
+            if(!uniqueElements.stream().anyMatch(x->equality.test(x, element))) {
+                uniqueElements.add(element);
             }
         });
-        return Can.ofCollection(list);
+        return _CanFactory.ofNonNullElements(uniqueElements);
     }
 
     @Override
diff --git a/commons/src/main/java/org/apache/isis/commons/collections/Can_Singleton.java b/commons/src/main/java/org/apache/isis/commons/collections/Can_Singleton.java
index 667ffb5..72b26c9 100644
--- a/commons/src/main/java/org/apache/isis/commons/collections/Can_Singleton.java
+++ b/commons/src/main/java/org/apache/isis/commons/collections/Can_Singleton.java
@@ -166,7 +166,7 @@ final class Can_Singleton<T> implements Can<T> {
         val newElements = new ArrayList<T>(other.size()+1);
         newElements.add(element);
         other.forEach(newElements::add);
-        return Can_Multiple.of(newElements);
+        return _CanFactory.ofNonNullElements(newElements);
     }
 
     @Override
@@ -234,7 +234,7 @@ final class Can_Singleton<T> implements Can<T> {
         for(int i=0; i<pickCount; i++) {
             newElements.add(element);
         }
-        return Can.ofCollection(newElements);
+        return _CanFactory.ofNonNullElements(newElements);
     }
 
     @Override
diff --git a/commons/src/main/java/org/apache/isis/commons/collections/_CanFactory.java b/commons/src/main/java/org/apache/isis/commons/collections/_CanFactory.java
new file mode 100644
index 0000000..1237b95
--- /dev/null
+++ b/commons/src/main/java/org/apache/isis/commons/collections/_CanFactory.java
@@ -0,0 +1,69 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.commons.collections;
+
+import java.util.ArrayList;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+
+import lombok.experimental.UtilityClass;
+
+/**
+ * As the {@link Can} interface can only provide public methods,
+ * we encapsulate optimized unsafe Can constructors here with
+ * package private access only.
+ */
+@UtilityClass
+class _CanFactory {
+
+    /**
+     * @param <T> element type
+     * @param nonNullElements - not containing <code>null</code>
+     * @implNote no non-null checks for optimization (package internal use only)
+     */
+    <T> Can<T> ofNonNullElements(final ArrayList<T> nonNullElements) {
+
+        if(nonNullElements.isEmpty()) {
+            return Can.empty();
+        }
+
+        if(nonNullElements.size()==1) {
+            return Can.ofSingleton(nonNullElements.get(0));
+        }
+
+        // let's be heap friendly, ignore below a certain threshold
+        if(nonNullElements.size()>32) {
+            nonNullElements.trimToSize();
+        }
+
+        return Can_Multiple.of(nonNullElements);
+    }
+
+    <T> ArrayList<T> arrayListWithSizeUpperBound(final int maxSize) {
+        // this is just an optimization, to pre-allocate a reasonable list size,
+        // specifically targeted at small list sizes
+        return new ArrayList<T>(Math.min(maxSize, 1024));
+    }
+
+    <T> Collector<T, ?, ArrayList<T>> toListWithSizeUpperBound(final int maxSize) {
+        return Collectors.toCollection(()->
+                _CanFactory.arrayListWithSizeUpperBound(maxSize));
+    }
+
+}