Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run annotation scanner in parallel across different mods #32

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package net.neoforged.fml.loading.moddiscovery;

import com.mojang.logging.LogUtils;
import net.neoforged.fml.loading.FMLConfig;
import net.neoforged.fml.loading.ImmediateWindowHandler;
import net.neoforged.fml.loading.LoadingModList;
import net.neoforged.fml.loading.LogMarkers;
Expand All @@ -17,6 +18,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class BackgroundScanHandler
{
Expand All @@ -38,10 +40,14 @@ private enum ScanStatus {
private LoadingModList loadingModList;

public BackgroundScanHandler() {
modContentScanner = Executors.newSingleThreadExecutor(r -> {
int maxThreads = FMLConfig.getIntConfigValue(FMLConfig.ConfigValue.MAX_THREADS);
// Leave 1 thread for Minecraft's own bootstrap
int poolSize = Math.max(1, maxThreads - 1);
AtomicInteger threadCount = new AtomicInteger();
modContentScanner = Executors.newFixedThreadPool(poolSize, r -> {
final Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
thread.setName("Background Scan Handler");
thread.setName("background-scan-handler-" + threadCount.getAndIncrement());
return thread;
});
scannedFiles = new ArrayList<>();
Expand All @@ -65,7 +71,7 @@ public void submitForScanning(final ModFile file) {
file.setFutureScanResult(future);
}

private void addCompletedFile(final ModFile file, final ModFileScanData modFileScanData, final Throwable throwable) {
private synchronized void addCompletedFile(final ModFile file, final ModFileScanData modFileScanData, final Throwable throwable) {
if (throwable != null) {
status = ScanStatus.ERRORED;
LOGGER.error(LogMarkers.SCAN,"An error occurred scanning file {}", file, throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ModFile implements IModFile {
private final IModProvider provider;
private IModFileInfo modFileInfo;
private ModFileScanData fileModFileScanData;
private CompletableFuture<ModFileScanData> futureScanResult;
private volatile CompletableFuture<ModFileScanData> futureScanResult;
private List<CoreModFile> coreMods;
private List<String> mixinConfigs;
private Path accessTransformer;
Expand Down Expand Up @@ -158,11 +158,11 @@ public ModFileScanData getScanResult() {
}

public void setScanResult(final ModFileScanData modFileScanData, final Throwable throwable) {
this.futureScanResult = null;
this.fileModFileScanData = modFileScanData;
if (throwable != null) {
this.scanError = throwable;
}
this.futureScanResult = null;
}

public void setFileProperties(Map<String, Object> fileProperties) {
Expand Down
Loading