From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 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 extends AbstractSet implements ca.spottedleaf. // Paper start - rewrite chunk system @Override public final boolean removeIf(final java.util.function.Predicate 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