Turn off Forge resource cache

This commit is contained in:
embeddedt 2023-04-26 14:15:24 -04:00
parent 9c172e2010
commit 821a15ecaa
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
3 changed files with 82 additions and 4 deletions

View File

@ -0,0 +1,15 @@
package org.embeddedt.modernfix.mixin.perf.modern_resourcepacks;
import net.minecraftforge.resource.ResourceCacheManager;
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;
@Mixin(value = ResourceCacheManager.class, remap = false)
public class ResourceCacheManagerMixin {
@Inject(method = "shouldUseCache", at = @At("HEAD"), cancellable = true)
private static void disableCache(CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(false);
}
}

View File

@ -1,22 +1,84 @@
package org.embeddedt.modernfix.mixin.perf.modern_resourcepacks;
import net.minecraft.resources.ResourceLocation;
import com.google.common.base.Joiner;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.VanillaPackResources;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import org.apache.commons.lang3.tuple.Pair;
import org.embeddedt.modernfix.FileWalker;
import org.embeddedt.modernfix.util.FileUtil;
import org.embeddedt.modernfix.util.PackTypeHelper;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
@Mixin(VanillaPackResources.class)
public class VanillaPackResourcesMixin {
@Shadow @Final private static Map<PackType, Path> ROOT_DIR_BY_TYPE;
private static LoadingCache<Pair<Path, Integer>, List<Path>> pathStreamLoadingCache = CacheBuilder.newBuilder()
.build(FileWalker.INSTANCE);
private static Set<String> containedPaths = null;
@Inject(method = "<init>", at = @At("TAIL"))
private void cacheContainedPaths(PackMetadataSection arg, String[] p_i47912_1_, CallbackInfo ci) {
if(containedPaths != null)
return;
containedPaths = new HashSet<>();
Joiner slashJoiner = Joiner.on('/');
for(PackType type : PackType.values()) {
if(!PackTypeHelper.isVanillaPackType(type))
continue;
Path root = ROOT_DIR_BY_TYPE.get(type);
if(root == null)
throw new IllegalStateException("No filesystem for vanilla " + type.name() + " assets");
try {
try(Stream<Path> stream = Files.walk(root)) {
stream
.map(path -> root.relativize(path.toAbsolutePath()))
.forEach(path -> containedPaths.add(slashJoiner.join(type.getDirectory(), path)));
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
@Redirect(method = "getResources(Ljava/util/Collection;Ljava/lang/String;Ljava/nio/file/Path;Ljava/lang/String;Ljava/util/function/Predicate;)V", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;walk(Ljava/nio/file/Path;[Ljava/nio/file/FileVisitOption;)Ljava/util/stream/Stream;"))
private static Stream<Path> useCacheForLoading(Path path, FileVisitOption[] fileVisitOptions) throws IOException {
try {
return pathStreamLoadingCache.get(Pair.of(path, Integer.MAX_VALUE)).stream();
} catch (ExecutionException e) {
if(e.getCause() instanceof IOException) /* generally always should be */
throw (IOException)e.getCause();
else
throw new IOException(e);
}
}
@Inject(method = "hasResource", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResource(Ljava/lang/String;)Ljava/net/URL;"), cancellable = true)
private void useCacheForExistence(PackType type, ResourceLocation location, CallbackInfoReturnable<Boolean> cir) {
if(!PackTypeHelper.isVanillaPackType(type))
return;
cir.setReturnValue(containedPaths.contains(type.getDirectory() + "/" + location.getNamespace() + "/" + FileUtil.normalize(location.getPath())));
}
/**
* @author embeddedt

View File

@ -10,6 +10,7 @@
"bugfix.edge_chunk_not_saved.ChunkManagerMixin",
"perf.modern_resourcepacks.VanillaPackResourcesMixin",
"perf.modern_resourcepacks.PathPackResourcesMixin",
"perf.modern_resourcepacks.ResourceCacheManagerMixin",
"perf.dynamic_structure_manager.StructureManagerMixin",
"bugfix.chunk_deadlock.ServerChunkCacheMixin",
"perf.dedicated_reload_executor.MinecraftServerMixin",