2021-06-11 14:02:28 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
Date: Wed, 15 Apr 2020 18:23:28 -0700
|
|
|
|
Subject: [PATCH] Optimise ArraySetSorted#removeIf
|
|
|
|
|
|
|
|
Remove iterator allocation and ensure the call is always O(n)
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/util/SortedArraySet.java b/src/main/java/net/minecraft/util/SortedArraySet.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/net/minecraft/util/SortedArraySet.java
|
|
|
|
+++ b/src/main/java/net/minecraft/util/SortedArraySet.java
|
|
|
|
@@ -0,0 +0,0 @@ public class SortedArraySet<T> extends AbstractSet<T> {
|
2021-06-14 03:06:38 +02:00
|
|
|
this.contents = (T[])castRawArray(new Object[initialCapacity]);
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
+ // Paper start - optimise removeIf
|
|
|
|
+ @Override
|
|
|
|
+ public boolean removeIf(java.util.function.Predicate<? super T> filter) {
|
|
|
|
+ // prev. impl used an iterator, which could be n^2 and creates garbage
|
2021-06-14 03:06:38 +02:00
|
|
|
+ int i = 0, len = this.size;
|
|
|
|
+ T[] backingArray = this.contents;
|
2021-06-11 14:02:28 +02:00
|
|
|
+
|
|
|
|
+ for (;;) {
|
|
|
|
+ if (i >= len) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if (!filter.test(backingArray[i])) {
|
|
|
|
+ ++i;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // we only want to write back to backingArray if we really need to
|
|
|
|
+
|
|
|
|
+ int lastIndex = i; // this is where new elements are shifted to
|
|
|
|
+
|
|
|
|
+ for (; i < len; ++i) {
|
|
|
|
+ T curr = backingArray[i];
|
|
|
|
+ if (!filter.test(curr)) { // if test throws we're screwed
|
|
|
|
+ backingArray[lastIndex++] = curr;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // cleanup end
|
|
|
|
+ Arrays.fill(backingArray, lastIndex, len, null);
|
2021-06-14 03:06:38 +02:00
|
|
|
+ this.size = lastIndex;
|
2021-06-11 14:02:28 +02:00
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ // Paper end - optimise removeIf
|
2021-06-14 03:06:38 +02:00
|
|
|
|
|
|
|
public static <T extends Comparable<T>> SortedArraySet<T> create() {
|
|
|
|
return create(10);
|