Optimizations to extension loading

This commit is contained in:
RednedEpic 2022-04-24 14:53:47 -05:00
parent 7c8bf330a9
commit 7f0e5b409f
3 changed files with 69 additions and 63 deletions

View file

@ -26,6 +26,7 @@ dependencies {
implementation("com.nukkitx.fastutil", "fastutil-int-boolean-maps", Versions.fastutilVersion)
implementation("com.nukkitx.fastutil", "fastutil-object-int-maps", Versions.fastutilVersion)
implementation("com.nukkitx.fastutil", "fastutil-object-object-maps", Versions.fastutilVersion)
implementation("com.nukkitx.fastutil", "fastutil-object-reference-maps", Versions.fastutilVersion)
// Network libraries
implementation("org.java-websocket", "Java-WebSocket", Versions.websocketVersion)

View file

@ -25,6 +25,8 @@
package org.geysermc.geyser.extension;
import it.unimi.dsi.fastutil.objects.Object2ReferenceMap;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import org.geysermc.geyser.api.extension.Extension;
import org.geysermc.geyser.api.extension.ExtensionDescription;
import org.geysermc.geyser.api.extension.exception.InvalidExtensionException;
@ -38,7 +40,7 @@ import java.util.Map;
public class GeyserExtensionClassLoader extends URLClassLoader {
private final GeyserExtensionLoader loader;
private final Map<String, Class<?>> classes = new HashMap<>();
private final Object2ReferenceMap<String, Class<?>> classes = new Object2ReferenceOpenHashMap<>();
public GeyserExtensionClassLoader(GeyserExtensionLoader loader, ClassLoader parent, Path path) throws MalformedURLException {
super(new URL[] { path.toUri().toURL() }, parent);

View file

@ -25,6 +25,8 @@
package org.geysermc.geyser.extension;
import it.unimi.dsi.fastutil.objects.Object2ReferenceMap;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserImpl;
@ -51,13 +53,15 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Stream;
@RequiredArgsConstructor
public class GeyserExtensionLoader extends ExtensionLoader {
private static final Path EXTENSION_DIRECTORY = Paths.get("extensions");
private static final Pattern API_VERSION_PATTERN = Pattern.compile("^[0-9]+\\.[0-9]+\\.[0-9]+$");
private static final Pattern API_VERSION_PATTERN = Pattern.compile("^\\d+\\.\\d+\\.\\d+$");
private static final Pattern[] EXTENSION_FILTERS = new Pattern[] { Pattern.compile("^.+\\.jar$") };
private final Map<String, Class<?>> classes = new HashMap<>();
private final Object2ReferenceMap<String, Class<?>> classes = new Object2ReferenceOpenHashMap<>();
private final Map<String, GeyserExtensionClassLoader> classLoaders = new HashMap<>();
private final Map<Extension, GeyserExtensionContainer> extensionContainers = new HashMap<>();
@ -105,29 +109,27 @@ public class GeyserExtensionLoader extends ExtensionLoader {
}
public Pattern[] extensionFilters() {
return new Pattern[] { Pattern.compile("^.+\\.jar$") };
return EXTENSION_FILTERS;
}
public Class<?> classByName(final String name) throws ClassNotFoundException{
Class<?> clazz = this.classes.get(name);
try {
for (GeyserExtensionClassLoader loader : this.classLoaders.values()) {
if (clazz != null) {
continue;
return clazz;
}
for (GeyserExtensionClassLoader loader : this.classLoaders.values()) {
clazz = loader.findClass(name, false);
if (clazz != null) {
break;
}
}
return clazz;
} catch (NullPointerException s) {
return null;
}
}
void setClass(String name, final Class<?> clazz) {
if (!this.classes.containsKey(name)) {
this.classes.put(name,clazz);
}
this.classes.putIfAbsent(name, clazz);
}
@Override
@ -149,8 +151,8 @@ public class GeyserExtensionLoader extends ExtensionLoader {
Map<String, GeyserExtensionContainer> loadedExtensions = new LinkedHashMap<>();
Pattern[] extensionFilters = this.extensionFilters();
Files.walk(EXTENSION_DIRECTORY).forEach(path -> {
try (Stream<Path> entries = Files.walk(EXTENSION_DIRECTORY)) {
entries.forEach(path -> {
if (Files.isDirectory(path)) {
return;
}
@ -203,6 +205,7 @@ public class GeyserExtensionLoader extends ExtensionLoader {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_with_name", path.getFileName(), path.toAbsolutePath()), e);
}
});
}
for (GeyserExtensionContainer container : loadedExtensions.values()) {
this.extensionContainers.put(container.extension(), container);