From 70c91c161ee3e70bb18a409347a6353b93a691e8 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Thu, 31 May 2012 20:14:28 -0500 Subject: [PATCH] Resize UnsafeList to initial capacity when cleared and implement clone. --- .../bukkit/craftbukkit/util/UnsafeList.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java b/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java index 818ca5e000..c6deec142a 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java +++ b/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java @@ -5,21 +5,22 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.AbstractList; -import java.util.Arrays; import java.util.List; import java.util.RandomAccess; // implementation of an ArrayList that offers a getter without range checks public class UnsafeList extends AbstractList implements List, RandomAccess, Cloneable, Serializable { - private static final long serialVersionUID = 8683452581112892189L; + private static final long serialVersionUID = 8683452581112892190L; private transient Object[] data; private int size; + private int initialCapacity; public UnsafeList(int capacity) { super(); if (capacity < 0) capacity = 128; int rounded = Integer.highestOneBit(capacity - 1) << 1; data = new Object[rounded]; + initialCapacity = rounded; } public UnsafeList() { @@ -95,11 +96,9 @@ public class UnsafeList extends AbstractList implements List, RandomAcc } public void clear() { - for (int i = 0; i < size; i++) { - data[i] = null; - } - + // Create new array to reset memory usage to initial capacity size = 0; + data = new Object[initialCapacity]; } // actually rounds up to nearest power of two @@ -137,6 +136,7 @@ public class UnsafeList extends AbstractList implements List, RandomAcc os.defaultWriteObject(); os.writeInt(size); + os.writeInt(initialCapacity); for (int i = 0; i < size; i++) { os.writeObject(data[i]); } @@ -146,9 +146,23 @@ public class UnsafeList extends AbstractList implements List, RandomAcc is.defaultReadObject(); size = is.readInt(); + initialCapacity = is.readInt(); data = new Object[Integer.highestOneBit(size - 1) << 1]; for (int i = 0; i < size; i++) { data[i] = is.readObject(); } } + + public UnsafeList clone() { + try { + UnsafeList copy = (UnsafeList) super.clone(); + copy.data = Java15Compat.Arrays_copyOf(data, size); + copy.size = size; + copy.initialCapacity = initialCapacity; + return copy; + } catch (CloneNotSupportedException ex) { + // This should never happen + return null; + } + } }