From d87013132d6d19d70679984813e8ee78e197c25a Mon Sep 17 00:00:00 2001 From: Su5eD Date: Sun, 17 Mar 2024 19:44:34 +0100 Subject: [PATCH] Initialize language adapters lazily --- .../loader/impl/FabricLoaderImpl.java | 19 ++++++++++++------- .../impl/entrypoint/EntrypointStorage.java | 17 ++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java b/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java index 05a2a145..e589e35f 100644 --- a/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java +++ b/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java @@ -16,6 +16,7 @@ package net.fabricmc.loader.impl; +import com.google.common.base.Suppliers; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import cpw.mods.modlauncher.ArgumentHandler; @@ -41,6 +42,7 @@ import java.nio.file.Path; import java.util.*; import java.util.function.Consumer; +import java.util.function.Supplier; @SuppressWarnings("deprecation") public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader { @@ -50,7 +52,7 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader { private final List mods = new ArrayList<>(); private final Multimap modAliases = HashMultimap.create(); - private final Map adapterMap = new HashMap<>(); + private final Map> adapterMap = new HashMap<>(); private final EntrypointStorage entrypointStorage = new EntrypointStorage(); private final ObjectShare objectShare = new ObjectShareImpl(); @@ -229,7 +231,7 @@ public void setup() { } private void setupLanguageAdapters() { - adapterMap.put("default", DefaultLanguageAdapter.INSTANCE); + adapterMap.put("default", () -> DefaultLanguageAdapter.INSTANCE); for (ModContainerImpl mod : mods) { // add language adapters @@ -238,11 +240,14 @@ private void setupLanguageAdapters() { throw new RuntimeException("Duplicate language adapter key: " + laEntry.getKey() + "! (" + laEntry.getValue() + ", " + adapterMap.get(laEntry.getKey()).getClass().getName() + ")"); } - try { - adapterMap.put(laEntry.getKey(), (LanguageAdapter) Class.forName(laEntry.getValue(), true, Thread.currentThread().getContextClassLoader()).getDeclaredConstructor().newInstance()); - } catch (Exception e) { - throw new RuntimeException("Failed to instantiate language adapter: " + laEntry.getKey(), e); - } + Supplier supplier = Suppliers.memoize(() -> { + try { + return (LanguageAdapter) Class.forName(laEntry.getValue(), true, Thread.currentThread().getContextClassLoader()).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new RuntimeException("Failed to instantiate language adapter: " + laEntry.getKey(), e); + } + }); + adapterMap.put(laEntry.getKey(), supplier); } } } diff --git a/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java b/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java index 29f2db7c..4030b90b 100644 --- a/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java +++ b/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java @@ -25,13 +25,8 @@ import net.fabricmc.loader.impl.util.log.Log; import net.fabricmc.loader.impl.util.log.LogCategory; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Supplier; public final class EntrypointStorage { interface Entry { @@ -98,11 +93,11 @@ public Collection getInstances() { private static final class NewEntry implements Entry { private final ModContainerImpl mod; - private final LanguageAdapter adapter; + private final Supplier adapter; private final String value; private final Map, Object> instanceMap; - NewEntry(ModContainerImpl mod, LanguageAdapter adapter, String value) { + NewEntry(ModContainerImpl mod, Supplier adapter, String value) { this.mod = mod; this.adapter = adapter; this.value = value; @@ -121,7 +116,7 @@ public synchronized T getOrCreate(Class type) throws Exception { T ret = (T) instanceMap.get(type); if (ret == null) { - ret = adapter.create(mod, value, type); + ret = adapter.get().create(mod, value, type); assert ret != null; T prev = (T) instanceMap.putIfAbsent(type, ret); if (prev != null) ret = prev; @@ -173,7 +168,7 @@ public void addDeprecated(ModContainerImpl modContainer, String adapter, String getOrCreateEntries("server").add(oe); } - public void add(ModContainerImpl modContainer, String key, EntrypointMetadata metadata, Map adapterMap) throws Exception { + public void add(ModContainerImpl modContainer, String key, EntrypointMetadata metadata, Map> adapterMap) throws Exception { if (!adapterMap.containsKey(metadata.getAdapter())) { throw new Exception("Could not find adapter '" + metadata.getAdapter() + "' (mod " + modContainer.getMetadata().getId() + "!)"); }