mirror of
https://github.com/Dreeam-qwq/Gale.git
synced 2025-12-21 15:59:28 +00:00
68 lines
3.1 KiB
Diff
68 lines
3.1 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: MIT (https://opensource.org/licenses/MIT)
|
|
Gale - https://galemc.org
|
|
|
|
This patch is based on the following mixin:
|
|
"com/ishland/vmp/mixins/general/collections/MixinTypeFilterableList.java"
|
|
By: ishland <ishlandmc@yeah.net>
|
|
As part of: VMP (https://github.com/RelativityMC/VMP-fabric)
|
|
Licensed under: MIT (https://opensource.org/licenses/MIT)
|
|
|
|
diff --git a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
|
|
index 4264b3a69b5cbe2e56058927ceb5409389cecf4b..ff545f6d36c265d70c79fc1acc7a279f10fbfcfd 100644
|
|
--- a/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
|
|
+++ b/src/main/java/net/minecraft/util/ClassInstanceMultiMap.java
|
|
@@ -56,18 +56,41 @@ 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<? extends T> list = this.byClass
|
|
- .computeIfAbsent(type, typeClass -> this.allInstances.stream().filter(typeClass::isInstance).collect(Util.toMutableList()));
|
|
- return (Collection<S>)Collections.unmodifiableCollection(list);
|
|
+ // Gale start - VMP - 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>) collection;
|
|
+ }
|
|
+
|
|
+ private <S> Collection<T> createAllOfType(Class<S> type) {
|
|
+ return this.byClass.computeIfAbsent(
|
|
+ type,
|
|
+ typeClass -> {
|
|
+ it.unimi.dsi.fastutil.objects.ObjectArrayList<T> ts = new it.unimi.dsi.fastutil.objects.ObjectArrayList<>(this.allInstances.size());
|
|
+
|
|
+ for (T _allElement : ((it.unimi.dsi.fastutil.objects.ObjectArrayList<T>) this.allInstances).elements()) {
|
|
+ if (typeClass.isInstance(_allElement)) {
|
|
+ ts.add(_allElement);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return ts;
|
|
+ }
|
|
+ );
|
|
+ // Gale end - VMP - avoid Class#isAssignableFrom call in ClassInstanceMultiMap
|
|
}
|
|
|
|
@Override
|
|
public Iterator<T> iterator() {
|
|
- return (Iterator<T>)(this.allInstances.isEmpty() ? Collections.emptyIterator() : Iterators.unmodifiableIterator(this.allInstances.iterator()));
|
|
+ return this.allInstances.isEmpty() ? Collections.emptyIterator() : Iterators.unmodifiableIterator(this.allInstances.iterator());
|
|
}
|
|
|
|
public List<T> getAllInstances() {
|