mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-26 02:19:19 +00:00
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2 since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList. Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2 lazy handles filter condition on iteration, so much better.
65 lines
3.0 KiB
Diff
65 lines
3.0 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
Date: Wed, 23 Jul 2025 15:51:59 +0200
|
|
Subject: [PATCH] Custom NonNullList
|
|
|
|
|
|
diff --git a/net/minecraft/core/NonNullList.java b/net/minecraft/core/NonNullList.java
|
|
index 7e31c5c8659d24948fd45a2d6ee7bdeca6027d27..9d39369f298d53031b118c71f52471b0239a804b 100644
|
|
--- a/net/minecraft/core/NonNullList.java
|
|
+++ b/net/minecraft/core/NonNullList.java
|
|
@@ -14,23 +14,28 @@ public class NonNullList<E> extends AbstractList<E> {
|
|
private final E defaultValue;
|
|
|
|
public static <E> NonNullList<E> create() {
|
|
- return new NonNullList<>(Lists.newArrayList(), null);
|
|
+ return new NonNullList<>(new org.dreeam.leaf.util.list.OptimizedNonNullListArrayList<>(null), null);
|
|
}
|
|
|
|
public static <E> NonNullList<E> createWithCapacity(int initialCapacity) {
|
|
- return new NonNullList<>(Lists.newArrayListWithCapacity(initialCapacity), null);
|
|
+ return new NonNullList<>(new org.dreeam.leaf.util.list.OptimizedNonNullListArrayList<>(initialCapacity, null), null);
|
|
}
|
|
|
|
public static <E> NonNullList<E> withSize(int size, E defaultValue) {
|
|
Validate.notNull(defaultValue);
|
|
- Object[] objects = new Object[size];
|
|
+
|
|
+ @SuppressWarnings("unchecked")
|
|
+ E[] objects = (E[])new Object[size];
|
|
Arrays.fill(objects, defaultValue);
|
|
- return new NonNullList<>(Arrays.asList((E[])objects), defaultValue);
|
|
+ return new NonNullList<>(new org.dreeam.leaf.util.list.OptimizedNonNullListArrayList<>(objects, defaultValue), defaultValue);
|
|
}
|
|
|
|
@SafeVarargs
|
|
public static <E> NonNullList<E> of(E defaultValue, E... elements) {
|
|
- return new NonNullList<>(Arrays.asList(elements), defaultValue);
|
|
+ Validate.notNull(elements);
|
|
+ // Directly create the custom list from the array.
|
|
+ // This avoids the problematic, fixed-size `Arrays.asList()` and is more efficient.
|
|
+ return new NonNullList<>(new org.dreeam.leaf.util.list.OptimizedNonNullListArrayList<>(elements, defaultValue), defaultValue);
|
|
}
|
|
|
|
protected NonNullList(List<E> list, @Nullable E defaultValue) {
|
|
@@ -69,10 +74,16 @@ public class NonNullList<E> extends AbstractList<E> {
|
|
@Override
|
|
public void clear() {
|
|
if (this.defaultValue == null) {
|
|
- super.clear();
|
|
+ this.list.clear();
|
|
} else {
|
|
- for (int i = 0; i < this.size(); i++) {
|
|
- this.set(i, this.defaultValue);
|
|
+ // If the backing list is our custom type, use its fast fill method.
|
|
+ if (this.list instanceof org.dreeam.leaf.util.list.OptimizedNonNullListArrayList) {
|
|
+ ((org.dreeam.leaf.util.list.OptimizedNonNullListArrayList<E>) this.list).fillWithDefault();
|
|
+ } else {
|
|
+ // Fallback for any other List impl
|
|
+ for (int i = 0; i < this.size(); i++) {
|
|
+ this.list.set(i, this.defaultValue);
|
|
+ }
|
|
}
|
|
}
|
|
}
|