From b28a418bd9f8885044f48afc5b6c253ad70256e2 Mon Sep 17 00:00:00 2001 From: cpw Date: Sun, 10 Feb 2019 11:44:22 -0500 Subject: [PATCH] Use a configuration value for the thread count, make it default to availableProcessors when set to <=0 (default value is -1) Signed-off-by: cpw --- .../minecraftforge/fml/loading/FMLConfig.java | 14 +++++++----- .../resources/META-INF/defaultfmlconfig.toml | 2 ++ .../java/net/minecraftforge/fml/ModList.java | 22 +++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLConfig.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLConfig.java index a2b4648f0..5ec8f957d 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLConfig.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/FMLConfig.java @@ -39,9 +39,7 @@ public class FMLConfig private static ConfigSpec configSpec = new ConfigSpec(); static { configSpec.define("splashscreen", Boolean.TRUE); - configSpec.defineInList("side", Dist.CLIENT.name(), Arrays.stream(Dist.values()).map(Enum::name).collect(Collectors.toList())); - configSpec.defineInRange("maxframerate", 60, 10, 120); - configSpec.defineInRange("minframerate", 60, 10, 120); + configSpec.define("maxThreads", -1); } private CommentedFileConfig configData; @@ -70,7 +68,13 @@ public class FMLConfig LOGGER.debug(CORE, "Splash screen is {}", INSTANCE.splashScreenEnabled()); } - public boolean splashScreenEnabled() { - return configData.getOptional("splashscreen").orElse(Boolean.FALSE); + public static boolean splashScreenEnabled() { + return INSTANCE.configData.getOptional("splashscreen").orElse(Boolean.FALSE); + } + + public static int loadingThreadCount() { + int val = INSTANCE.configData.get("maxThreads"); + if (val <= 0) return Runtime.getRuntime().availableProcessors(); + return val; } } diff --git a/src/fmllauncher/resources/META-INF/defaultfmlconfig.toml b/src/fmllauncher/resources/META-INF/defaultfmlconfig.toml index bddded498..dd5a6903d 100644 --- a/src/fmllauncher/resources/META-INF/defaultfmlconfig.toml +++ b/src/fmllauncher/resources/META-INF/defaultfmlconfig.toml @@ -1,2 +1,4 @@ # does the splashscreen run splashscreen = true +# max threads for parallel loading : -1 uses Runtime#availableProcessors +maxThreads = -1 diff --git a/src/main/java/net/minecraftforge/fml/ModList.java b/src/main/java/net/minecraftforge/fml/ModList.java index 396e44190..576228928 100644 --- a/src/main/java/net/minecraftforge/fml/ModList.java +++ b/src/main/java/net/minecraftforge/fml/ModList.java @@ -19,6 +19,7 @@ package net.minecraftforge.fml; +import net.minecraftforge.fml.loading.FMLConfig; import net.minecraftforge.forgespi.language.ModFileScanData; import net.minecraftforge.fml.loading.FMLLoader; import net.minecraftforge.fml.loading.moddiscovery.ModFile; @@ -35,7 +36,6 @@ import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinWorkerThread; -import java.util.concurrent.FutureTask; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -56,14 +56,7 @@ public class ModList private final Map fileById; private List mods; private Map indexedMods; - private ForkJoinPool modLoadingThreadPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), pool -> - { - ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); - thread.setName("modloading-worker-" + thread.getPoolIndex()); - // The default sets it to the SystemClassloader, so copy the current one. - thread.setContextClassLoader(Thread.currentThread().getContextClassLoader()); - return thread; - }, null, false); + private ForkJoinPool modLoadingThreadPool; private List modFileScanData; private ModList(final List modFiles, final List sortedList) @@ -75,6 +68,9 @@ public class ModList this.fileById = this.modFiles.stream().map(ModFileInfo::getMods).flatMap(Collection::stream). map(ModInfo.class::cast). collect(Collectors.toMap(ModInfo::getModId, ModInfo::getOwningFile)); + final int loadingThreadCount = FMLConfig.loadingThreadCount(); + LOGGER.debug(LOADING, "Using {} threads for parallel mod-loading", loadingThreadCount); + modLoadingThreadPool = new ForkJoinPool(loadingThreadCount, ModList::newForkJoinWorkerThread, null, false); } public static ModList of(List modFiles, List sortedList) @@ -91,6 +87,14 @@ public class ModList return INSTANCE; } + private static ForkJoinWorkerThread newForkJoinWorkerThread(ForkJoinPool pool) { + ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); + thread.setName("modloading-worker-" + thread.getPoolIndex()); + // The default sets it to the SystemClassloader, so copy the current one. + thread.setContextClassLoader(Thread.currentThread().getContextClassLoader()); + return thread; + } + public List getModFiles() { return modFiles;