9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-19 14:59:29 +00:00
Files
Gale/patches/server/0089-Avoid-Class-isAssignableFrom-call-in-ClassInstanceMu.patch
2023-12-05 22:29:00 -05:00

60 lines
2.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Wed, 30 Nov 2022 21:15:33 +0100
Subject: [PATCH] Avoid Class#isAssignableFrom call in ClassInstanceMultiMap
License: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following mixin:
"me/jellysquid/mods/lithium/mixin/collections/entity_filtering/TypeFilterableListMixin.java"
By: Angeline <jellysquid3@users.noreply.github.com>
As part of: Lithium (https://github.com/CaffeineMC/lithium-fabric)
Licensed under: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
diff --git a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
index f9a7617f4c6c19c798d7fe40491690c8f5de14a9..d5c8fc37ae53835290c8fa731fef974734182ebe 100644
--- a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
+++ b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
@@ -57,14 +57,33 @@ public class ClassInstanceMultiMap<T> extends AbstractCollection<T> {
}
public <S> Collection<S> find(Class<S> type) {
- if (!this.baseClass.isAssignableFrom(type)) {
- throw new IllegalArgumentException("Don't know how to search for " + type);
- } else {
- List list = this.byClass.computeIfAbsent(type, (typeClass) -> { // Gale - dev import deobfuscation fixes
- return this.allInstances.stream().filter(typeClass::isInstance).collect(Collectors.toList());
- });
- return Collections.unmodifiableCollection(list);
+ // Gale start - Lithium - avoid Class#isAssignableFrom call in ClassInstanceMultiMap
+ /*
+ Only perform the slow Class#isAssignableFrom(Class) if a list doesn't exist for the type, otherwise
+ we can assume it's already valid. The slow-path code is moved to a separate method to help the JVM inline this.
+ */
+ Collection<T> collection = this.byClass.get(type);
+
+ if (collection == null) {
+ collection = this.createAllOfType(type);
}
+
+ return (Collection<S>) Collections.unmodifiableCollection(collection);
+ }
+
+ private <S> Collection<T> createAllOfType(Class<S> type) {
+ List<T> list = new java.util.ArrayList<>(1);
+
+ for (T allElement : this.allInstances) {
+ if (type.isInstance(allElement)) {
+ list.add(allElement);
+ }
+ }
+
+ this.byClass.put(type, list);
+
+ return list;
+ // Gale end - Lithium - avoid Class#isAssignableFrom call in ClassInstanceMultiMap
}
@Override