From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Kevin Raneri Date: Wed, 10 Nov 2021 00:37:03 -0500 Subject: [PATCH] Pufferfish: Optimize mob spawning Original license: GPL v3 Original project: https://github.com/pufferfish-gg/Pufferfish Co-authored-by: booky10 Co-authored-by: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> This patch aims to reduce the main-thread impact of mob spawning by offloading as much work as possible to other threads. It is possible for inconsistencies to come up, but when they happen they never interfere with the server's operation (they don't produce errors), and side effects are limited to more or less mobs being spawned in any particular tick. It is possible to disable this optimization if it is not required or if it interferes with any plugins. On servers with thousands of entities, this can result in performance gains of up to 15%, which is significant and, in my opinion, worth the low risk of minor mob-spawning-related inconsistencies. diff --git a/src/main/java/ca/spottedleaf/moonrise/common/list/IteratorSafeOrderedReferenceSet.java b/src/main/java/ca/spottedleaf/moonrise/common/list/IteratorSafeOrderedReferenceSet.java index c21e00812f1aaa1279834a0562d360d6b89e146c..4a6bfc2a09401b4c96d6f368ead7b060dae2a08b 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/list/IteratorSafeOrderedReferenceSet.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/list/IteratorSafeOrderedReferenceSet.java @@ -10,7 +10,7 @@ public final class IteratorSafeOrderedReferenceSet { public static final int ITERATOR_FLAG_SEE_ADDITIONS = 1 << 0; private final Reference2IntLinkedOpenHashMap indexMap; - private int firstInvalidIndex = -1; + private final java.util.concurrent.atomic.AtomicInteger firstInvalidIndex = new java.util.concurrent.atomic.AtomicInteger(-1); // Leaf - Pufferfish - Async mob spawning - atomic /* list impl */ private E[] listElements; @@ -18,7 +18,7 @@ public final class IteratorSafeOrderedReferenceSet { private final double maxFragFactor; - private int iteratorCount; + private final java.util.concurrent.atomic.AtomicInteger iteratorCount = new java.util.concurrent.atomic.AtomicInteger(); // Pufferfish - async mob spawning public IteratorSafeOrderedReferenceSet() { this(16, 0.75f, 16, 0.2); @@ -79,11 +79,11 @@ public final class IteratorSafeOrderedReferenceSet { } public int createRawIterator() { - ++this.iteratorCount; + this.iteratorCount.incrementAndGet(); // Pufferfish - async mob spawning if (this.indexMap.isEmpty()) { return -1; } else { - return this.firstInvalidIndex == 0 ? this.indexMap.getInt(this.indexMap.firstKey()) : 0; + return this.firstInvalidIndex.get() == 0 ? this.indexMap.getInt(this.indexMap.firstKey()) : 0; // Leaf - Pufferfish - Async mob spawning } } @@ -100,7 +100,7 @@ public final class IteratorSafeOrderedReferenceSet { } public void finishRawIterator() { - if (--this.iteratorCount == 0) { + if (this.iteratorCount.decrementAndGet() == 0) { // Pufferfish - async mob spawning if (this.getFragFactor() >= this.maxFragFactor) { this.defrag(); } @@ -110,14 +110,17 @@ public final class IteratorSafeOrderedReferenceSet { public boolean remove(final E element) { final int index = this.indexMap.removeInt(element); if (index >= 0) { - if (this.firstInvalidIndex < 0 || index < this.firstInvalidIndex) { - this.firstInvalidIndex = index; + // Leaf start - Pufferfish - Async mob spawning + int firstInvalidIndex = this.firstInvalidIndex.get(); + if (firstInvalidIndex < 0 || index < firstInvalidIndex) { + this.firstInvalidIndex.set(index); } + // Leaf end - Pufferfish - Async mob spawning if (this.listElements[index] != element) { throw new IllegalStateException(); } this.listElements[index] = null; - if (this.iteratorCount == 0 && this.getFragFactor() >= this.maxFragFactor) { + if (this.iteratorCount.get() == 0 && this.getFragFactor() >= this.maxFragFactor) { // Pufferfish - async mob spawning this.defrag(); } //this.check(); @@ -149,14 +152,17 @@ public final class IteratorSafeOrderedReferenceSet { } private void defrag() { - if (this.firstInvalidIndex < 0) { + // Leaf start - Pufferfish - Async mob spawning + int firstInvalidIndex = this.firstInvalidIndex.get(); + if (firstInvalidIndex < 0) { return; // nothing to do } + // Leaf end - Pufferfish - Async mob spawning if (this.indexMap.isEmpty()) { Arrays.fill(this.listElements, 0, this.listSize, null); this.listSize = 0; - this.firstInvalidIndex = -1; + this.firstInvalidIndex.set(-1); // Leaf - Pufferfish - Async mob spawning //this.check(); return; } @@ -166,11 +172,11 @@ public final class IteratorSafeOrderedReferenceSet { int lastValidIndex; java.util.Iterator> iterator; - if (this.firstInvalidIndex == 0) { + if (firstInvalidIndex == 0) { // Leaf - Pufferfish - Async mob spawning iterator = this.indexMap.reference2IntEntrySet().fastIterator(); lastValidIndex = 0; } else { - lastValidIndex = this.firstInvalidIndex; + lastValidIndex = firstInvalidIndex; // Leaf - Pufferfish - Async mob spawning final E key = backingArray[lastValidIndex - 1]; iterator = this.indexMap.reference2IntEntrySet().fastIterator(new Reference2IntMap.Entry() { @Override @@ -201,7 +207,7 @@ public final class IteratorSafeOrderedReferenceSet { // cleanup end Arrays.fill(backingArray, lastValidIndex, this.listSize, null); this.listSize = lastValidIndex; - this.firstInvalidIndex = -1; + this.firstInvalidIndex.set(-1); // Leaf - Pufferfish - Async mob spawning //this.check(); } @@ -219,7 +225,7 @@ public final class IteratorSafeOrderedReferenceSet { } public IteratorSafeOrderedReferenceSet.Iterator iterator(final int flags) { - ++this.iteratorCount; + this.iteratorCount.incrementAndGet(); // Pufferfish - async mob spawning return new BaseIterator<>(this, true, (flags & ITERATOR_FLAG_SEE_ADDITIONS) != 0 ? Integer.MAX_VALUE : this.listSize); } @@ -306,7 +312,7 @@ public final class IteratorSafeOrderedReferenceSet { } this.lastReturned = null; this.finished = true; - this.set.finishRawIterator(); + this.set.finishRawIterator(); // Pufferfish - async mob spawning - diff on change } } }