From 24fea7737dce0652fa95ea9090ee442ef0553bb9 Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Fri, 9 Apr 2021 11:19:48 +0200 Subject: [PATCH 1/3] Add initial capacity to List and Compound tags --- .../java/net/querz/nbt/tag/CompoundTag.java | 11 +++- src/main/java/net/querz/nbt/tag/ListTag.java | 61 +++++++++++++------ 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/querz/nbt/tag/CompoundTag.java b/src/main/java/net/querz/nbt/tag/CompoundTag.java index daa629cd..9cdc3363 100644 --- a/src/main/java/net/querz/nbt/tag/CompoundTag.java +++ b/src/main/java/net/querz/nbt/tag/CompoundTag.java @@ -1,7 +1,5 @@ package net.querz.nbt.tag; -import net.querz.io.MaxDepthIO; - import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -10,7 +8,10 @@ import java.util.Set; import java.util.function.BiConsumer; -public class CompoundTag extends Tag>> implements Iterable>>, Comparable, MaxDepthIO { +import net.querz.io.MaxDepthIO; + +public class CompoundTag extends Tag>> + implements Iterable>>, Comparable, MaxDepthIO { public static final byte ID = 10; @@ -18,6 +19,10 @@ public CompoundTag() { super(createEmptyValue()); } + public CompoundTag(int initialCapacity) { + super(new HashMap<>(initialCapacity)); + } + @Override public byte getID() { return ID; diff --git a/src/main/java/net/querz/nbt/tag/ListTag.java b/src/main/java/net/querz/nbt/tag/ListTag.java index f26c4b07..955ad1a4 100644 --- a/src/main/java/net/querz/nbt/tag/ListTag.java +++ b/src/main/java/net/querz/nbt/tag/ListTag.java @@ -1,7 +1,5 @@ package net.querz.nbt.tag; -import net.querz.io.MaxDepthIO; - import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -10,38 +8,53 @@ import java.util.Objects; import java.util.function.Consumer; +import net.querz.io.MaxDepthIO; + /** * ListTag represents a typed List in the nbt structure. * An empty {@link ListTag} will be of type {@link EndTag} (unknown type). * The type of an empty untyped {@link ListTag} can be set by using any of the {@code add()} * methods or any of the {@code as...List()} methods. - * */ + */ public class ListTag> extends Tag> implements Iterable, Comparable>, MaxDepthIO { public static final byte ID = 9; private Class typeClass = null; - private ListTag() { - super(createEmptyValue(3)); + private ListTag(int initialCapacity) { + super(createEmptyValue(initialCapacity)); } @Override public byte getID() { return ID; } - + /** - *

Creates a non-type-safe ListTag. Its element type will be set after the first + *

Creates a non-type-safe ListTag. Its element type will be set after the first * element was added.

- * - *

This is an internal helper method for cases where the element type is not known + * + *

This is an internal helper method for cases where the element type is not known * at construction time. Use {@link #ListTag(Class)} when the type is known.

- * + * * @return A new non-type-safe ListTag */ public static ListTag createUnchecked(Class typeClass) { - ListTag list = new ListTag<>(); + return createUnchecked(typeClass, 3); + } + + /** + *

Creates a non-type-safe ListTag. Its element type will be set after the first + * element was added.

+ * + *

This is an internal helper method for cases where the element type is not known + * at construction time. Use {@link #ListTag(Class)} when the type is known.

+ * + * @return A new non-type-safe ListTag + */ + public static ListTag createUnchecked(Class typeClass, int initialCapacity) { + ListTag list = new ListTag<>(initialCapacity); list.typeClass = typeClass; return list; } @@ -49,10 +62,10 @@ public static ListTag createUnchecked(Class typeClass) { /** *

Creates an empty mutable list to be used as empty value of ListTags.

* - * @param Type of the list elements + * @param Type of the list elements * @param initialCapacity The initial capacity of the returned List * @return An instance of {@link java.util.List} with an initial capacity of 3 - * */ + */ private static List createEmptyValue(int initialCapacity) { return new ArrayList<>(initialCapacity); } @@ -60,10 +73,20 @@ private static List createEmptyValue(int initialCapacity) { /** * @param typeClass The exact class of the elements * @throws IllegalArgumentException When {@code typeClass} is {@link EndTag}{@code .class} - * @throws NullPointerException When {@code typeClass} is {@code null} + * @throws NullPointerException When {@code typeClass} is {@code null} */ public ListTag(Class typeClass) throws IllegalArgumentException, NullPointerException { - super(createEmptyValue(3)); + this(typeClass, 3); + } + + /** + * @param typeClass The exact class of the elements + * @param initialCapacity Initial capacity of list + * @throws IllegalArgumentException When {@code typeClass} is {@link EndTag}{@code .class} + * @throws NullPointerException When {@code typeClass} is {@code null} + */ + public ListTag(Class typeClass, int initialCapacity) throws IllegalArgumentException, NullPointerException { + super(createEmptyValue(initialCapacity)); if (typeClass == EndTag.class) { throw new IllegalArgumentException("cannot create ListTag with EndTag elements"); } @@ -114,8 +137,9 @@ public T set(int index, T t) { /** * Adds a Tag to this ListTag after the last index. + * * @param t The element to be added. - * */ + */ public void add(T t) { add(size(), t); } @@ -271,7 +295,8 @@ public boolean equals(Object other) { if (this == other) { return true; } - if (!super.equals(other) || size() != ((ListTag) other).size() || getTypeClass() != ((ListTag) other).getTypeClass()) { + if (!super.equals(other) || size() != ((ListTag) other).size() || getTypeClass() != ((ListTag) other) + .getTypeClass()) { return false; } for (int i = 0; i < size(); i++) { @@ -295,7 +320,7 @@ public int compareTo(ListTag o) { @SuppressWarnings("unchecked") @Override public ListTag clone() { - ListTag copy = new ListTag<>(); + ListTag copy = new ListTag<>(this.size()); // assure type safety for clone copy.typeClass = typeClass; for (T t : getValue()) { From 0fc12da00815b30f0f2122d46f1aad8d4e63658e Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Fri, 16 Apr 2021 14:44:12 +0200 Subject: [PATCH 2/3] Clone with correct size to avoid resizing --- src/main/java/net/querz/nbt/tag/CompoundTag.java | 2 +- src/main/java/net/querz/nbt/tag/ListTag.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/querz/nbt/tag/CompoundTag.java b/src/main/java/net/querz/nbt/tag/CompoundTag.java index 9cdc3363..6c754cd7 100644 --- a/src/main/java/net/querz/nbt/tag/CompoundTag.java +++ b/src/main/java/net/querz/nbt/tag/CompoundTag.java @@ -282,7 +282,7 @@ public int compareTo(CompoundTag o) { @Override public CompoundTag clone() { - CompoundTag copy = new CompoundTag(); + CompoundTag copy = new CompoundTag(((int) Math.ceil(getValue().size() * 1.75f)) + 1); for (Map.Entry> e : getValue().entrySet()) { copy.put(e.getKey(), e.getValue().clone()); } diff --git a/src/main/java/net/querz/nbt/tag/ListTag.java b/src/main/java/net/querz/nbt/tag/ListTag.java index 955ad1a4..6f381351 100644 --- a/src/main/java/net/querz/nbt/tag/ListTag.java +++ b/src/main/java/net/querz/nbt/tag/ListTag.java @@ -320,7 +320,7 @@ public int compareTo(ListTag o) { @SuppressWarnings("unchecked") @Override public ListTag clone() { - ListTag copy = new ListTag<>(this.size()); + ListTag copy = new ListTag<>(((int) Math.ceil(this.size() * 1.75)) + 1); // assure type safety for clone copy.typeClass = typeClass; for (T t : getValue()) { From 1e73e7db02d05c007f8e4a3524ec3c66946f85b7 Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Fri, 16 Apr 2021 16:05:48 +0200 Subject: [PATCH 3/3] Clone with correct size to avoid resizing --- src/main/java/net/querz/nbt/tag/CompoundTag.java | 2 +- src/main/java/net/querz/nbt/tag/ListTag.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/querz/nbt/tag/CompoundTag.java b/src/main/java/net/querz/nbt/tag/CompoundTag.java index 6c754cd7..28145891 100644 --- a/src/main/java/net/querz/nbt/tag/CompoundTag.java +++ b/src/main/java/net/querz/nbt/tag/CompoundTag.java @@ -282,7 +282,7 @@ public int compareTo(CompoundTag o) { @Override public CompoundTag clone() { - CompoundTag copy = new CompoundTag(((int) Math.ceil(getValue().size() * 1.75f)) + 1); + CompoundTag copy = new CompoundTag((int) Math.ceil(getValue().size() * 1.33f)); for (Map.Entry> e : getValue().entrySet()) { copy.put(e.getKey(), e.getValue().clone()); } diff --git a/src/main/java/net/querz/nbt/tag/ListTag.java b/src/main/java/net/querz/nbt/tag/ListTag.java index 6f381351..955ad1a4 100644 --- a/src/main/java/net/querz/nbt/tag/ListTag.java +++ b/src/main/java/net/querz/nbt/tag/ListTag.java @@ -320,7 +320,7 @@ public int compareTo(ListTag o) { @SuppressWarnings("unchecked") @Override public ListTag clone() { - ListTag copy = new ListTag<>(((int) Math.ceil(this.size() * 1.75)) + 1); + ListTag copy = new ListTag<>(this.size()); // assure type safety for clone copy.typeClass = typeClass; for (T t : getValue()) {