9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-31 12:56:29 +00:00

[ci skip] cleanup (6/14)

This commit is contained in:
Dreeam
2025-03-07 15:49:01 -05:00
parent efc9314dd9
commit e38a717f8a
5 changed files with 74 additions and 56 deletions

View File

@@ -5,10 +5,10 @@ Subject: [PATCH] Optimize AABB
diff --git a/net/minecraft/world/phys/AABB.java b/net/minecraft/world/phys/AABB.java
index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..3a41291aa9c0d728b54cf962360303750725bc82 100644
index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..00daaff66bd26e9ca15a7eb4052ff38f9e662f7b 100644
--- a/net/minecraft/world/phys/AABB.java
+++ b/net/minecraft/world/phys/AABB.java
@@ -220,13 +220,14 @@ public class AABB {
@@ -220,13 +220,16 @@ public class AABB {
}
public AABB intersect(AABB other) {
@@ -19,6 +19,7 @@ index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..3a41291aa9c0d728b54cf96236030375
- double min1 = Math.min(this.maxY, other.maxY);
- double min2 = Math.min(this.maxZ, other.maxZ);
- return new AABB(max, max1, max2, min, min1, min2);
+ // Leaf start - Optimize AABB
+ return new AABB(
+ this.minX > other.minX ? this.minX : other.minX,
+ this.minY > other.minY ? this.minY : other.minY,
@@ -27,14 +28,16 @@ index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..3a41291aa9c0d728b54cf96236030375
+ this.maxY < other.maxY ? this.maxY : other.maxY,
+ this.maxZ < other.maxZ ? this.maxZ : other.maxZ
+ );
+ // Leaf end - Optimize AABB
}
public AABB minmax(AABB other) {
@@ -258,16 +259,33 @@ public class AABB {
@@ -258,16 +261,39 @@ public class AABB {
}
public boolean intersects(AABB other) {
- return this.intersects(other.minX, other.minY, other.minZ, other.maxX, other.maxY, other.maxZ);
+ // Leaf start - Optimize AABB
+ // Removed redundant method call overhead
+ return this.minX < other.maxX &&
+ this.maxX > other.minX &&
@@ -42,10 +45,12 @@ index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..3a41291aa9c0d728b54cf96236030375
+ this.maxY > other.minY &&
+ this.minZ < other.maxZ &&
+ this.maxZ > other.minZ;
+ // Leaf end - Optimize AABB
}
public boolean intersects(double x1, double y1, double z1, double x2, double y2, double z2) {
- return this.minX < x2 && this.maxX > x1 && this.minY < y2 && this.maxY > y1 && this.minZ < z2 && this.maxZ > z1;
+ // Leaf start - Optimize AABB
+ // No temporary variables needed, direct comparison
+ return this.minX < x2 &&
+ this.maxX > x1 &&
@@ -53,17 +58,20 @@ index f64c04b32dd2d0fe143fc8bf9f498e52beb66a58..3a41291aa9c0d728b54cf96236030375
+ this.maxY > y1 &&
+ this.minZ < z2 &&
+ this.maxZ > z1;
+ // Leaf end - Optimize AABB
}
public boolean intersects(Vec3 min, Vec3 max) {
return this.intersects(
- Math.min(min.x, max.x), Math.min(min.y, max.y), Math.min(min.z, max.z), Math.max(min.x, max.x), Math.max(min.y, max.y), Math.max(min.z, max.z)
+ // Leaf start - Optimize AABB
+ min.x < max.x ? min.x : max.x,
+ min.y < max.y ? min.y : max.y,
+ min.z < max.z ? min.z : max.z,
+ min.x > max.x ? min.x : max.x,
+ min.y > max.y ? min.y : max.y,
+ min.z > max.z ? min.z : max.z
+ // Leaf end - Optimize AABB
);
}

View File

@@ -5,43 +5,39 @@ Subject: [PATCH] Improve sorting in SortedArraySet
diff --git a/net/minecraft/util/SortedArraySet.java b/net/minecraft/util/SortedArraySet.java
index 339b19e88567be382e550ed54477fabd58d51faa..bde5b4cb4cda003acd7343b16f09f915b71fe3f2 100644
index 339b19e88567be382e550ed54477fabd58d51faa..3de282a68bd125ad17fee47bebef3835a860c170 100644
--- a/net/minecraft/util/SortedArraySet.java
+++ b/net/minecraft/util/SortedArraySet.java
@@ -11,6 +11,7 @@ import javax.annotation.Nullable;
public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.moonrise.patches.chunk_system.util.ChunkSystemSortedArraySet<T> { // Paper - rewrite chunk system
private static final int DEFAULT_INITIAL_CAPACITY = 10;
private final Comparator<T> comparator;
+ private final boolean isNaturalOrder;
+ private final boolean isNaturalOrder; // Leaf - Improve sorting in SortedArraySet
T[] contents;
int size;
@@ -93,10 +94,11 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
@@ -93,6 +94,7 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
private SortedArraySet(int initialCapacity, Comparator<T> comparator) {
this.comparator = comparator;
+ this.isNaturalOrder = comparator == Comparator.naturalOrder();
+ this.isNaturalOrder = comparator == Comparator.naturalOrder(); // Leaf - Improve sorting in SortedArraySet
if (initialCapacity < 0) {
throw new IllegalArgumentException("Initial capacity (" + initialCapacity + ") is negative");
} else {
- this.contents = (T[])castRawArray(new Object[initialCapacity]);
+ this.contents = (T[]) castRawArray(new Object[initialCapacity]);
}
}
@@ -121,7 +123,51 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
@@ -121,9 +123,57 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
}
private int findIndex(T object) {
- return Arrays.binarySearch(this.contents, 0, this.size, object, this.comparator);
+ // Leaf start - Improve sorting in SortedArraySet
+ return isNaturalOrder ? naturalBinarySearch(object) : customBinarySearch(object);
+ }
+
}
+ private int naturalBinarySearch(T object) {
+ int low = 0;
+ int high = this.size - 1;
+ Comparable<? super T> key = (Comparable<? super T>) object;
+ T[] a = this.contents;
+ final Comparable<? super T> key = (Comparable<? super T>) object;
+ final T[] a = this.contents;
+
+ while (low <= high) {
+ int mid = (low + high) >>> 1;
@@ -56,14 +52,15 @@ index 339b19e88567be382e550ed54477fabd58d51faa..bde5b4cb4cda003acd7343b16f09f915
+ return mid;
+ }
+ }
+
+ return -(low + 1);
+ }
+
+ private int customBinarySearch(T object) {
+ int low = 0;
+ int high = this.size - 1;
+ T[] a = this.contents;
+ Comparator<T> c = this.comparator;
+ final T[] a = this.contents;
+ final Comparator<T> c = this.comparator;
+
+ while (low <= high) {
+ int mid = (low + high) >>> 1;
@@ -78,7 +75,11 @@ index 339b19e88567be382e550ed54477fabd58d51faa..bde5b4cb4cda003acd7343b16f09f915
+ return mid;
+ }
+ }
+
+ return -(low + 1);
}
+ }
+ // Leaf end - Improve sorting in SortedArraySet
+
private static int getInsertionPosition(int index) {
return -index - 1;
}

View File

@@ -5,14 +5,15 @@ Subject: [PATCH] Make removeIf slightly faster
diff --git a/net/minecraft/util/SortedArraySet.java b/net/minecraft/util/SortedArraySet.java
index bde5b4cb4cda003acd7343b16f09f915b71fe3f2..7af029d8c2677ef00186acb5a3794b0ab3267ebd 100644
index 3de282a68bd125ad17fee47bebef3835a860c170..7a838be229f65d676ed0ed4f6aa074630c90b99f 100644
--- a/net/minecraft/util/SortedArraySet.java
+++ b/net/minecraft/util/SortedArraySet.java
@@ -18,35 +18,22 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
@@ -18,37 +18,26 @@ public class SortedArraySet<T> extends AbstractSet<T> implements ca.spottedleaf.
// Paper start - rewrite chunk system
@Override
public final boolean removeIf(final java.util.function.Predicate<? super T> filter) {
- // prev. impl used an iterator, which could be n^2 and creates garbage
+ // Leaf start - Make removeIf slightly faster
int i = 0;
final int len = this.size;
final T[] backingArray = this.contents;
@@ -52,4 +53,7 @@ index bde5b4cb4cda003acd7343b16f09f915b71fe3f2..7af029d8c2677ef00186acb5a3794b0a
+ // Only update size - skip Arrays.fill (safe in ChunkHolderManager's context)
this.size = lastIndex;
return true;
+ // Leaf end - Make removeIf slightly faster
}
@Override

View File

@@ -5,95 +5,95 @@ Subject: [PATCH] Optimize LinearPalette
diff --git a/net/minecraft/world/level/chunk/LinearPalette.java b/net/minecraft/world/level/chunk/LinearPalette.java
index 2073f6ff41aa570102621d183ee890b076267d54..459b6adca18868354374d00f3da906395fb474ab 100644
index 2073f6ff41aa570102621d183ee890b076267d54..2595ab14e2edd9c0f113f8997b0d3290a2c2fcad 100644
--- a/net/minecraft/world/level/chunk/LinearPalette.java
+++ b/net/minecraft/world/level/chunk/LinearPalette.java
@@ -1,5 +1,6 @@
package net.minecraft.world.level.chunk;
+import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.core.IdMap;
@@ -10,6 +11,8 @@ import org.apache.commons.lang3.Validate;
@@ -10,6 +10,10 @@ import org.apache.commons.lang3.Validate;
public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.patches.fast_palette.FastPalette<T> { // Paper - optimise palette reads
private final IdMap<T> registry;
private final T[] values;
+ // Leaf start - Optimize LinearPalette
+ private final int[] byteSizes;
+ private final int[] idCache; // Cached registry IDs for values
+ // Leaf end - Optimize LinearPalette
private final PaletteResize<T> resizeHandler;
private final int bits;
private int size;
@@ -23,24 +26,34 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
@@ -24,23 +28,40 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
private LinearPalette(IdMap<T> registry, int bits, PaletteResize<T> resizeHandler, List<T> values) {
this.registry = registry;
- this.values = (T[])(new Object[1 << bits]);
+ this.values = (T[]) (new Object[1 << bits]);
this.values = (T[])(new Object[1 << bits]);
+ // Leaf start - Optimize LinearPalette
+ this.idCache = new int[1 << bits];
+ this.byteSizes = new int[1 << bits]; // Initialize byteSizes
+ // Leaf end - Optimize LinearPalette
this.bits = bits;
this.resizeHandler = resizeHandler;
Validate.isTrue(values.size() <= this.values.length, "Can't initialize LinearPalette of size %d with %d entries", this.values.length, values.size());
for (int i = 0; i < values.size(); i++) {
- this.values[i] = values.get(i);
+ // Leaf start - Optimize LinearPalette
+ T value = values.get(i);
+ this.values[i] = value;
+ int id = registry.getId(value);
+ this.idCache[i] = id;
+ this.byteSizes[i] = VarInt.getByteSize(id); // Precompute byte size
+ // Leaf end - Optimize LinearPalette
}
-
this.size = values.size();
}
- private LinearPalette(IdMap<T> registry, T[] values, PaletteResize<T> resizeHandler, int bits, int size) {
+ private LinearPalette(IdMap<T> registry, T[] values, int[] idCache, PaletteResize<T> resizeHandler, int bits, int size) {
+ private LinearPalette(IdMap<T> registry, T[] values, int[] idCache, PaletteResize<T> resizeHandler, int bits, int size) { // Leaf - Optimize LinearPalette
this.registry = registry;
this.values = values;
+ this.idCache = idCache;
+ this.idCache = idCache; // Leaf - Optimize LinearPalette
this.resizeHandler = resizeHandler;
this.bits = bits;
this.size = size;
+ // Leaf start - Optimize LinearPalette
+ this.byteSizes = new int[idCache.length];
+ for (int i = 0; i < idCache.length; i++) {
+ this.byteSizes[i] = VarInt.getByteSize(idCache[i]);
+ }
+ // Leaf end - Optimize LinearPalette
}
public static <A> Palette<A> create(int bits, IdMap<A> registry, PaletteResize<A> resizeHandler, List<A> values) {
@@ -58,6 +71,9 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
@@ -58,6 +79,11 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
int ix = this.size;
if (ix < this.values.length) {
this.values[ix] = state;
+ // Leaf start - Optimize LinearPalette
+ int id = registry.getId(state);
+ this.idCache[ix] = id;
+ this.byteSizes[ix] = VarInt.getByteSize(id); // Cache byte size
+ // Leaf end - Optimize LinearPalette
this.size++;
return ix;
} else {
@@ -88,29 +104,28 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
@Override
public void read(FriendlyByteBuf buffer) {
@@ -90,7 +116,12 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
this.size = buffer.readVarInt();
-
for (int i = 0; i < this.size; i++) {
- this.values[i] = this.registry.byIdOrThrow(buffer.readVarInt());
+ // Leaf start - Optimize LinearPalette
+ int id = buffer.readVarInt();
+ this.values[i] = this.registry.byIdOrThrow(id);
+ this.idCache[i] = id;
+ this.byteSizes[i] = VarInt.getByteSize(id); // Precompute during read
+ // Leaf end - Optimize LinearPalette
}
}
@Override
public void write(FriendlyByteBuf buffer) {
@@ -99,17 +130,18 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
buffer.writeVarInt(this.size);
-
for (int i = 0; i < this.size; i++) {
- buffer.writeVarInt(this.registry.getId(this.values[i]));
+ buffer.writeVarInt(this.idCache[i]); // Use cached ID
+ buffer.writeVarInt(this.idCache[i]); // Leaf - Optimize LinearPalette - Use cached ID
}
}
@@ -103,19 +103,21 @@ index 2073f6ff41aa570102621d183ee890b076267d54..459b6adca18868354374d00f3da90639
-
- for (int i = 0; i < this.getSize(); i++) {
- byteSize += VarInt.getByteSize(this.registry.getId(this.values[i]));
+ // Leaf start - Optimize LinearPalette
+ int byteSize = VarInt.getByteSize(this.size);
+ for (int i = 0; i < this.size; i++) {
+ byteSize += this.byteSizes[i]; // Use cached byte sizes
}
-
+ // Leaf end - Optimize LinearPalette
return byteSize;
}
@@ -121,6 +136,54 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
@@ -121,6 +153,56 @@ public class LinearPalette<T> implements Palette<T>, ca.spottedleaf.moonrise.pat
@Override
public Palette<T> copy(PaletteResize<T> resizeHandler) {
- return new LinearPalette<>(this.registry, (T[])((Object[])this.values.clone()), resizeHandler, this.bits, this.size);
+ // Leaf start - Optimize LinearPalette
+ // Special case for empty palette - fastest possible return
+ if (this.size == 0) {
+ return new LinearPalette<>(this.registry, (T[]) new Object[1], new int[1], resizeHandler, this.bits, 0);
@@ -165,5 +167,6 @@ index 2073f6ff41aa570102621d183ee890b076267d54..459b6adca18868354374d00f3da90639
+ this.bits,
+ this.size
+ );
+ // Leaf end - Optimize LinearPalette
}
}

View File

@@ -1,11 +1,11 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Mon, 24 Feb 2025 16:33:46 +0100
Subject: [PATCH] Optimize IdMapper
Subject: [PATCH] Replace IdMapper id map with optimized collection
diff --git a/net/minecraft/core/IdMapper.java b/net/minecraft/core/IdMapper.java
index 439dc29b8ee8a1dc2ec63c00a9727a37bb697bad..b063cdb0667de69c5ab5ce3895a0adfb19d6a28f 100644
index 439dc29b8ee8a1dc2ec63c00a9727a37bb697bad..be80e0bd9b44f7a04075827f0a8f22a126031c07 100644
--- a/net/minecraft/core/IdMapper.java
+++ b/net/minecraft/core/IdMapper.java
@@ -11,7 +11,7 @@ import javax.annotation.Nullable;
@@ -13,18 +13,19 @@ index 439dc29b8ee8a1dc2ec63c00a9727a37bb697bad..b063cdb0667de69c5ab5ce3895a0adfb
public class IdMapper<T> implements IdMap<T> {
private int nextId;
- private final Reference2IntMap<T> tToId;
+ private final it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap<T> tToId;
+ private final it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap<T> tToId; // Leaf - Optimize IdMapper
private final List<T> idToT;
public IdMapper() {
@@ -20,7 +20,20 @@ public class IdMapper<T> implements IdMap<T> {
@@ -20,7 +20,22 @@ public class IdMapper<T> implements IdMap<T> {
public IdMapper(int expectedSize) {
this.idToT = Lists.newArrayListWithExpectedSize(expectedSize);
- this.tToId = new Reference2IntOpenHashMap<>(expectedSize);
+ // Leaf start - Replace IdMapper id map with optimized collection
+ this.tToId = new it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap<>(
+ expectedSize,
+ new it.unimi.dsi.fastutil.Hash.Strategy<T>() {
+ new it.unimi.dsi.fastutil.Hash.Strategy<>() {
+ @Override
+ public int hashCode(T o) {
+ return System.identityHashCode(o);
@@ -36,6 +37,7 @@ index 439dc29b8ee8a1dc2ec63c00a9727a37bb697bad..b063cdb0667de69c5ab5ce3895a0adfb
+ }
+ }
+ );
+ // Leaf end - Replace IdMapper id map with optimized collection
this.tToId.defaultReturnValue(-1);
}