Skip to content

Commit

Permalink
Run annotation scanner in parallel across different mod jars (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Nov 2, 2023
1 parent 5836277 commit 1a7537c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
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

0 comments on commit 1a7537c

Please sign in to comment.