mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2026-01-04 15:41:40 +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.
59 lines
2.2 KiB
Diff
59 lines
2.2 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
Date: Sun, 23 Feb 2025 00:37:39 +0100
|
|
Subject: [PATCH] Make removeIf slightly faster
|
|
|
|
|
|
diff --git a/net/minecraft/util/SortedArraySet.java b/net/minecraft/util/SortedArraySet.java
|
|
index e8bad6e1c0aeb67005661db6ed42f745554fa652..c2d64cdc5241d54379da2b843b6f28db8a8e39a9 100644
|
|
--- a/net/minecraft/util/SortedArraySet.java
|
|
+++ b/net/minecraft/util/SortedArraySet.java
|
|
@@ -19,36 +19,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;
|
|
|
|
- for (;;) {
|
|
- if (i >= len) {
|
|
- return false;
|
|
- }
|
|
- if (!filter.test(backingArray[i++])) {
|
|
- continue;
|
|
- }
|
|
- break;
|
|
- }
|
|
+ // Find first element to remove
|
|
+ while (i < len && !filter.test(backingArray[i])) i++;
|
|
+ if (i == len) return false;
|
|
|
|
- // we only want to write back to backingArray if we really need to
|
|
-
|
|
- int lastIndex = i - 1; // this is where new elements are shifted to
|
|
-
|
|
- for (; i < len; ++i) {
|
|
- final T curr = backingArray[i];
|
|
- if (!filter.test(curr)) { // if test throws we're screwed
|
|
- backingArray[lastIndex++] = curr;
|
|
- }
|
|
+ // Shift elements in-place
|
|
+ int lastIndex = i;
|
|
+ for (i++; i < len; i++) {
|
|
+ T curr = backingArray[i];
|
|
+ if (!filter.test(curr)) backingArray[lastIndex++] = curr;
|
|
}
|
|
|
|
- // cleanup end
|
|
- Arrays.fill(backingArray, lastIndex, len, null);
|
|
+ // Only update size - skip Arrays.fill (safe in ChunkHolderManager's context)
|
|
this.size = lastIndex;
|
|
return true;
|
|
+ // Leaf end - Make removeIf slightly faster
|
|
}
|
|
|
|
@Override
|