Improve performance of IIngredientMatrix.isIncomplete inside baseline implementation

This commit is contained in:
DBotThePony 2023-08-17 22:59:30 +07:00
parent 2e4984162b
commit eff453ed1c
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 27 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import kotlin.KotlinVersion;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.EventPriority;
@ -13,6 +14,7 @@ import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.IdMappingEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.dbotthepony.mc.otm.android.AndroidResearchManager;
@ -60,6 +62,7 @@ import top.theillusivec4.curios.api.CuriosApi;
import static net.minecraftforge.common.MinecraftForge.EVENT_BUS;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.atomic.AtomicInteger;
// The value here should match an entry in the META-INF/mods.toml file
@Mod(OverdriveThatMatters.MOD_ID)
@ -69,6 +72,17 @@ public final class OverdriveThatMatters {
// Directly reference a log4j logger.
public static final String MOD_ID = "overdrive_that_matters";
private static final Logger LOGGER = LogManager.getLogger();
public static final AtomicInteger INGREDIENT_CACHE_INVALIDATION_COUNTER;
static {
try {
var f = Ingredient.class.getDeclaredField("INVALIDATION_COUNTER");
f.setAccessible(true);
INGREDIENT_CACHE_INVALIDATION_COUNTER = (AtomicInteger) f.get(null);
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
public static OverdriveThatMatters INSTANCE;
public static ResourceLocation loc(String path) {

View File

@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.recipe
import net.minecraft.core.NonNullList
import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.crafting.Ingredient
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.container.get
import ru.dbotthepony.mc.otm.core.collect.allEqual
import ru.dbotthepony.mc.otm.core.collect.any
@ -10,6 +11,7 @@ import ru.dbotthepony.mc.otm.core.collect.flatMap
import ru.dbotthepony.mc.otm.core.collect.map
import ru.dbotthepony.mc.otm.core.isActuallyEmpty
import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.core.util.countingLazy
import java.util.function.Predicate
interface IIngredientMatrix : Predicate<CraftingContainer>, Iterable<Ingredient> {
@ -119,6 +121,16 @@ interface IIngredientMatrix : Predicate<CraftingContainer>, Iterable<Ingredient>
class IngredientMatrix(override val width: Int, override val height: Int) : IIngredientMatrix {
private val data = Array(width * height) { Ingredient.EMPTY }
private val lazy = countingLazy(OverdriveThatMatters.INGREDIENT_CACHE_INVALIDATION_COUNTER) {
super.isIncomplete
}
override val isIncomplete by lazy
override fun iterator(): Iterator<Ingredient> {
return data.iterator()
}
override fun get(column: Int, row: Int): Ingredient {
require(column in 0 until width) { "Column out of bounds: $column (matrix width: $width)" }
require(row in 0 until height) { "Row out of bounds: $row > (matrix height: $height)" }
@ -129,6 +141,7 @@ class IngredientMatrix(override val width: Int, override val height: Int) : IIng
require(column in 0 until width) { "Column out of bounds: $column (matrix width: $width)" }
require(row in 0 until height) { "Row out of bounds: $row > (matrix height: $height)" }
data[column + row * width] = value
lazy.invalidate()
}
companion object {