From 80a4585efd5bdfeeb09c8b2c066f5f0c9bbef6a3 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Thu, 9 Feb 2023 22:01:32 -0500 Subject: [PATCH] Do not deserialize model JSONs in parallel Some Forge model loaders attempt to read more models while deserializing, which causes issues --- .../ModelBakeryMixin.java | 49 +------------------ 1 file changed, 2 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/ModelBakeryMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/ModelBakeryMixin.java index f00197f0..38c149a1 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/ModelBakeryMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/ModelBakeryMixin.java @@ -59,55 +59,12 @@ public abstract class ModelBakeryMixin { @Shadow @Final private Map topLevelModels; @Shadow @Final protected IResourceManager resourceManager; - private Map deserializedModelCache = null; + private Map>> deserializedBlockstateCache = null; - private boolean useModelCache = false; - - @Inject(method = "loadBlockModel", at = @At("HEAD"), cancellable = true) - private void useCachedModel(ResourceLocation location, CallbackInfoReturnable cir) { - if(useModelCache && deserializedModelCache != null) { - BlockModel model = deserializedModelCache.get(location); - if(model != null) - cir.setReturnValue(model); - } - } - - private BlockModel loadModelSafely(ResourceLocation location) { - try { - return this.loadBlockModel(location); - } catch(Throwable e) { - ModernFix.LOGGER.warn("Model " + location + " will not be preloaded"); - return null; - } - } @Inject(method = "processLoading", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/IProfiler;popPush(Ljava/lang/String;)V", ordinal = 0)) private void preloadJsonModels(IProfiler profilerIn, int maxMipmapLevel, CallbackInfo ci) { - profilerIn.popPush("loadjsons"); - AsyncStopwatch.measureAndLogSerialRunningTime("Parallel JSON loading", () -> { - useModelCache = false; - this.deserializedModelCache = new ConcurrentHashMap<>(); - Collection modelLocations = Minecraft.getInstance().getResourceManager().listResources("models", p -> { - if(!p.endsWith(".json")) - return false; - for(int i = 0; i < p.length(); i++) { - if(!ResourceLocation.validPathChar(p.charAt(i))) - return false; - } - return true; - }); - ArrayList> futures = new ArrayList<>(); - for(ResourceLocation location : modelLocations) { - futures.add(CompletableFuture.runAsync(() -> { - ResourceLocation modelPath = new ResourceLocation(location.getNamespace(), location.getPath().substring(7, location.getPath().length() - 5)); - BlockModel model = this.loadModelSafely(modelPath); - if(model != null) - this.deserializedModelCache.put(modelPath, model); - }, Util.backgroundExecutor())); - } - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); - useModelCache = true; - }); + profilerIn.popPush("loadblockstates"); AsyncStopwatch.measureAndLogSerialRunningTime("Parallel blockstate loading", () -> { ThreadLocal containerHolder = ThreadLocal.withInitial(BlockModelDefinition.ContainerHolder::new); this.deserializedBlockstateCache = new ConcurrentHashMap<>(); @@ -145,9 +102,7 @@ public abstract class ModelBakeryMixin { @Inject(method = "processLoading", at = @At("RETURN"), remap = false) private void clearModelCache(IProfiler profilerIn, int maxMipmapLevel, CallbackInfo ci) { - deserializedModelCache.clear(); deserializedBlockstateCache.clear(); - useModelCache = false; } private List replacementList = null;