Skip to content

Commit ef646f1

Browse files
committed
Cache EventPriority#values in ListenerListInst
1 parent 71c40a5 commit ef646f1

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/net/minecraftforge/eventbus/ListenerList.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static synchronized void unregisterAll(int id, IEventListener listener) {
9898
}
9999

100100
private static class ListenerListInst {
101+
// Enum#values() performs a defensive copy for each call.
102+
// As we never modify the returned values array in this class, we can safely reuse the returned values array.
103+
private static final EventPriority[] EVENT_PRIORITY_VALUES = EventPriority.values();
104+
101105
private boolean rebuild = true;
102106
private AtomicReference<IEventListener[]> listeners = new AtomicReference<>();
103107
private final @Nullable ArrayList<IEventListener>[] priorities;
@@ -108,7 +112,7 @@ private static class ListenerListInst {
108112
@SuppressWarnings("unchecked")
109113
private ListenerListInst() {
110114
// Make a lazy-loaded array of lists containing listeners for each priority level.
111-
priorities = (ArrayList<IEventListener>[]) new ArrayList[EventPriority.values().length];
115+
priorities = (ArrayList<IEventListener>[]) new ArrayList[EVENT_PRIORITY_VALUES.length];
112116
}
113117

114118
public void dispose() {
@@ -192,7 +196,7 @@ private void buildCache() {
192196
parent.buildCache();
193197

194198
ArrayList<IEventListener> ret = new ArrayList<>();
195-
Arrays.stream(EventPriority.values()).forEach(value -> {
199+
Arrays.stream(EVENT_PRIORITY_VALUES).forEach(value -> {
196200
List<IEventListener> listeners = getListeners(value);
197201
if (!listeners.isEmpty()) {
198202
ret.add(value); //Add the priority to notify the event of it's current phase.

0 commit comments

Comments
 (0)