From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sun, 1 Jun 2025 18:13:24 +0200 Subject: [PATCH] optimise ReferenceList diff --git a/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java b/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java index 8df9406b77eb3c225ebf88bf76a7adb666452f3b..14ac2a533e0b882f26ee4a11f8d6bccfe752c750 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java @@ -47,17 +47,21 @@ public final class ReferenceList implements Iterable { // move the object at the end to this index final int endIndex = --this.count; - final E end = (E)this.references[endIndex]; if (index != endIndex) { + // The removed element was not the last one. + // Move the element that was at 'endIndex' (the old tail) to 'index'. + final E end = (E)this.references[endIndex]; // not empty after this call this.referenceToIndex.put(end, index); // update index + this.references[index] = end; } - this.references[index] = end; + // Null out the slot at 'endIndex'. + // If 'index == endIndex', this was the slot of the removed element. + // If 'index != endIndex', this was the original slot of the moved element 'end'. this.references[endIndex] = null; return true; } - public boolean add(final E obj) { final int count = this.count; final int currIndex = this.referenceToIndex.putIfAbsent(obj, count);