Fixes thread starvation issue with parallelStream in findFuels() being called from within another parallelStream on the same ForkJoinPool

Introduces a `findFuelsOnceParallel` that is called before the parallel recipe/usage handlers are run (which now call the non parallel version of findFuels())
This commit is contained in:
Jason Mitchell authored and Jason Mitchell committed 2021-01-30 12:38:45 -08:00
1 parent 03ede1773c
commit 3bb7b6722a
5 files changed
+42 -17

No files matched your search

+1 -1
View File
@@ -2,4 +2,4 @@ mc_version=1.7.10
forge_version=10.13.4.1614-1.7.10
ccl_version=1.1.3.138
ccc_version=1.0.7.+
mod_version=2.0.7-GTNH
mod_version=2.0.8-GTNH
@@ -70,6 +70,8 @@ public class FurnaceRecipeHandler extends TemplateRecipeHandler
@Override
public TemplateRecipeHandler newInstance() {
// Use the non parallel version since we might already be using the forkJoinPool and might have no threads available
// This will ideally have been pre-cached elsewhere and will be a NOOP
findFuelsOnce();
return super.newInstance();
}
@@ -21,7 +21,7 @@ public class GuiCraftingRecipe extends GuiRecipe
profiler.start("recipe.concurrent.crafting");
// Pre-find the fuels so we're not fighting over it
FuelRecipeHandler.findFuelsOnce();
FuelRecipeHandler.findFuelsOnceParallel();
try {
handlers = ItemList.forkJoinPool.submit(() -> craftinghandlers.parallelStream()
@@ -12,13 +12,16 @@ import java.util.stream.Collectors;
public class GuiUsageRecipe extends GuiRecipe
{
public static boolean openRecipeGui(String inputId, Object... ingredients) {
public static boolean openRecipeGui(String inputId, Object... ingredients) {
Minecraft mc = NEIClientUtils.mc();
GuiContainer prevscreen = mc.currentScreen instanceof GuiContainer ? (GuiContainer) mc.currentScreen : null;
ArrayList<IUsageHandler> handlers;
TaskProfiler profiler = ProfilerRecipeHandler.getProfiler();
// Pre-find the fuels so we're not fighting over it
FuelRecipeHandler.findFuelsOnceParallel();
profiler.start("recipe.concurrent.usage");
try {
handlers = ItemList.forkJoinPool.submit(() -> usagehandlers.parallelStream()
@@ -69,15 +69,27 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH
lock.lock();
try {
if (afuels == null || afuels.isEmpty())
findFuels();
findFuels(false);
} finally {
lock.unlock();
}
}
public static void findFuelsOnceParallel() {
// Ensure we only find fuels once, even if threaded
lock.lock();
try {
if (afuels == null || afuels.isEmpty())
findFuels(true);
} finally {
lock.unlock();
}
}
private static Set<Item> efuels;
@SuppressWarnings("UnstableApiUsage")
private static void findFuels() {
Set<Item> efuels = new HashSet<>();
private static void findFuels(boolean parallel) {
efuels = new HashSet<>();
efuels.add(Item.getItemFromBlock(Blocks.brown_mushroom));
efuels.add(Item.getItemFromBlock(Blocks.red_mushroom));
efuels.add(Item.getItemFromBlock(Blocks.standing_sign));
@@ -86,20 +98,26 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH
efuels.add(Item.getItemFromBlock(Blocks.trapped_chest));
Stopwatch stopwatch = Stopwatch.createStarted();
try {
afuels = ItemList.forkJoinPool.submit(() -> ItemList.items.parallelStream().map(itemStack -> {
if (efuels.contains(itemStack.getItem())) return null;
final int burnTime = TileEntityFurnace.getItemBurnTime(itemStack);
if (burnTime <= 0) return null;
return new TemplateFuelPair(itemStack.copy(), burnTime);
}).filter(Objects::nonNull).collect(Collectors.toList())).get();
} catch (InterruptedException | ExecutionException e) {
afuels = new ArrayList<>();
e.printStackTrace();
if (parallel) {
try {
afuels = ItemList.forkJoinPool.submit(() -> ItemList.items.parallelStream().map(TemplateRecipeHandler::identifyFuel).filter(Objects::nonNull).collect(Collectors.toList())).get();
} catch (InterruptedException | ExecutionException e) {
afuels = new ArrayList<>();
e.printStackTrace();
}
} else {
afuels = ItemList.items.stream().map(TemplateRecipeHandler::identifyFuel).filter(Objects::nonNull).collect(Collectors.toList());
}
NEIClientConfig.logger.info("FindFuels took " + stopwatch.stop() );
}
private static TemplateFuelPair identifyFuel(final ItemStack itemStack) {
if (efuels.contains(itemStack.getItem())) return null;
final int burnTime = TileEntityFurnace.getItemBurnTime(itemStack);
if (burnTime <= 0) return null;
return new TemplateFuelPair(itemStack.copy(), burnTime);
}
/**
* This Recipe Handler runs on this internal class
@@ -523,7 +541,9 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH
public TemplateRecipeHandler newInstance() {
try {
findFuelsOnce();
// Use the non parallel version since we might already be using the forkJoinPool and might have no threads available
// This will ideally have been pre-cached elsewhere and will be a NOOP
findFuelsOnce();
return getClass().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);