From f9477d5adc3223f53bf220649a67142fdf5f83af Mon Sep 17 00:00:00 2001 From: Tim Vossen Date: Thu, 18 Feb 2021 18:19:18 +0100 Subject: [PATCH] Reuse the same strategy instance. This avoids unecessary allocations when using the default constructor of ArrayLists or ArrayDeques. --- .../hppc/BoundedProportionalArraySizingStrategy.java | 5 +++++ .../templates/com/carrotsearch/hppc/KTypeArrayDeque.java | 9 ++++++++- .../templates/com/carrotsearch/hppc/KTypeArrayList.java | 8 +++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/hppc/src/main/java/com/carrotsearch/hppc/BoundedProportionalArraySizingStrategy.java b/hppc/src/main/java/com/carrotsearch/hppc/BoundedProportionalArraySizingStrategy.java index a0e02084..d524a9b1 100644 --- a/hppc/src/main/java/com/carrotsearch/hppc/BoundedProportionalArraySizingStrategy.java +++ b/hppc/src/main/java/com/carrotsearch/hppc/BoundedProportionalArraySizingStrategy.java @@ -22,6 +22,11 @@ * */ public final class BoundedProportionalArraySizingStrategy implements ArraySizingStrategy { + + /** Instance of {@link BoundedProportionalArraySizingStrategy} with default values. */ + public static final BoundedProportionalArraySizingStrategy DEFAULT_INSTANCE = + new BoundedProportionalArraySizingStrategy(); + /** * Maximum allocable array length (approximately the largest positive integer decreased by the * array's object header). diff --git a/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayDeque.java b/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayDeque.java index eac87343..001b8eba 100644 --- a/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayDeque.java +++ b/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayDeque.java @@ -19,6 +19,13 @@ public class KTypeArrayDeque Preallocable, Cloneable, Accountable { + + /** + * Reuse the same strategy instance. + */ + private static final BoundedProportionalArraySizingStrategy DEFAULT_SIZING_STRATEGY = + BoundedProportionalArraySizingStrategy.DEFAULT_INSTANCE; + /** * Internal array for storing elements of the deque. */ @@ -62,7 +69,7 @@ public KTypeArrayDeque() { * expansion (inclusive). */ public KTypeArrayDeque(int expectedElements) { - this(expectedElements, new BoundedProportionalArraySizingStrategy()); + this(expectedElements, DEFAULT_SIZING_STRATEGY); } /** diff --git a/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayList.java b/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayList.java index 72d33d70..a15801d0 100644 --- a/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayList.java +++ b/hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayList.java @@ -36,6 +36,12 @@ public class KTypeArrayList new Object [0]; /*! #end !*/; + /** + * Reuse the same strategy instance. + */ + private static final BoundedProportionalArraySizingStrategy DEFAULT_SIZING_STRATEGY = + BoundedProportionalArraySizingStrategy.DEFAULT_INSTANCE; + /** * Internal array for storing the list. The array may be larger than the current size * ({@link #size()}). @@ -73,7 +79,7 @@ public KTypeArrayList() { * expansion (inclusive). */ public KTypeArrayList(int expectedElements) { - this(expectedElements, new BoundedProportionalArraySizingStrategy()); + this(expectedElements, DEFAULT_SIZING_STRATEGY); }