Cache all DeferredRegister suppliers

This commit is contained in:
embeddedt 2023-01-07 12:49:07 -05:00
parent 0fb6c71734
commit dd91031382
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
4 changed files with 38 additions and 26 deletions

View File

@ -0,0 +1,35 @@
package org.embeddedt.modernfix.mixin.perf.parallel_potentially_unsafe.parallel_deferred_suppliers;
import com.sun.org.apache.xpath.internal.operations.Bool;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.IForgeRegistry;
import org.embeddedt.modernfix.registry.DeferredRegisterBaker;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.HashMap;
@Mixin(DeferredRegister.EventDispatcher.class)
public class EventDispatcherMixin {
private static HashMap<ResourceLocation, Boolean> hasRegistryBaked = new HashMap<>();
@Inject(method = "handleEvent", at = @At("HEAD"), remap = false)
private void bakeIfNeeded(RegistryEvent.Register<?> event, CallbackInfo ci) {
IForgeRegistry<?> registry = event.getRegistry();
if(registry == null)
return;
ResourceLocation location = registry.getRegistryName();
if(location == null)
return;
if(!hasRegistryBaked.getOrDefault(location, false)) {
DeferredRegisterBaker.bakeSuppliers(location);
hasRegistryBaked.put(location, true);
}
}
}

View File

@ -1,24 +0,0 @@
package org.embeddedt.modernfix.mixin.perf.parallel_potentially_unsafe.parallel_deferred_suppliers;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.ModLoadingStage;
import net.minecraftforge.registries.GameData;
import org.embeddedt.modernfix.registry.DeferredRegisterBaker;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
@Mixin(GameData.class)
public class GameDataMixin {
@Inject(method = "generateRegistryEvents", at = @At(value = "INVOKE", target = "Ljava/util/List;stream()Ljava/util/stream/Stream;"), locals = LocalCapture.CAPTURE_FAILHARD, remap = false)
private static void bakeDeferredBeforeSendingEvents(CallbackInfoReturnable<Stream<ModLoadingStage.EventGenerator<?>>> cir, List<ResourceLocation> keys) {
/* TODO also bake items, maybe? */
DeferredRegisterBaker.bakeSuppliers(new ResourceLocation("minecraft", "block"));
}
}

View File

@ -30,6 +30,7 @@ public class DeferredRegisterBaker {
HashMap<String, List<CachedSupplier<?>>> registrySupplierMap = supplierMap.get(registry);
if(registrySupplierMap == null)
return;
ModernFix.LOGGER.info("Caching suppliers for " + registry);
Stopwatch realtimeStopwatch = Stopwatch.createStarted();
AsyncStopwatch cpuStopwatch = new AsyncStopwatch();
OrderedParallelModDispatcher.dispatchBlocking(ModWorkManager.parallelExecutor(), modId -> {
@ -50,7 +51,7 @@ public class DeferredRegisterBaker {
});
realtimeStopwatch.stop();
if(modErrors.size() > 0)
ModernFix.LOGGER.warn("The following mods had errors while caching suppliers (this is likely safe): [" + String.join(", ", modErrors) + "]");
ModernFix.LOGGER.warn("The following mods had errors while caching " + registry + " suppliers (this is likely safe): [" + String.join(", ", modErrors) + "]");
ModernFix.LOGGER.info("CPU time spent constructing " + registry + " suppliers: " + cpuStopwatch.getCpuTime()/1000f + " seconds");
ModernFix.LOGGER.info("Real time spent constructing " + registry + " suppliers: " + realtimeStopwatch.elapsed(TimeUnit.MILLISECONDS)/1000f + " seconds");
}

View File

@ -20,7 +20,7 @@
"perf.thread_priorities.UtilMixin",
"perf.preload_block_classes.GameDataMixin",
"perf.parallel_potentially_unsafe.parallel_deferred_suppliers.DeferredRegisterMixin",
"perf.parallel_potentially_unsafe.parallel_deferred_suppliers.GameDataMixin",
"perf.parallel_potentially_unsafe.parallel_deferred_suppliers.EventDispatcherMixin",
"perf.parallel_potentially_unsafe.parallel_blockstate_cache_rebuild.BlocksMixin",
"perf.parallel_potentially_unsafe.parallel_blockstate_cache_rebuild.BlockCallbacksMixin",
"perf.parallel_potentially_unsafe.parallel_blockstate_cache_rebuild.ShapeCacheMixin"