Optimize VanillaPack.resourceExists further

This commit is contained in:
embeddedt 2023-01-01 19:34:15 -05:00
parent 57773db489
commit 9a4952aa42
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -8,7 +8,9 @@ import net.minecraft.resources.VanillaPack;
import net.minecraft.util.ResourceLocation;
import org.apache.commons.lang3.tuple.Pair;
import org.embeddedt.modernfix.FileWalker;
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.Redirect;
@ -17,27 +19,20 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.*;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
@Mixin(VanillaPack.class)
public class VanillaPackMixin {
@Shadow @Final private static Map<ResourcePackType, FileSystem> FILE_SYSTEMS_BY_PACK_TYPE;
private static LoadingCache<Pair<Path, Integer>, List<Path>> pathStreamLoadingCache = CacheBuilder.newBuilder()
.build(FileWalker.INSTANCE);
private static EnumMap<ResourcePackType, HashMap<ResourceLocation, Boolean>> resourceContainmentCache = new EnumMap<>(ResourcePackType.class);
static {
for(ResourcePackType type : ResourcePackType.values()) {
resourceContainmentCache.put(type, new HashMap<>());
}
}
@Redirect(method = "collectResources", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;walk(Ljava/nio/file/Path;I[Ljava/nio/file/FileVisitOption;)Ljava/util/stream/Stream;"))
private static Stream<Path> useCacheForLoading(Path path, int maxDepth, FileVisitOption[] fileVisitOptions) throws IOException {
try {
@ -50,15 +45,9 @@ public class VanillaPackMixin {
}
}
@Inject(method = "resourceExists", at = @At(value = "RETURN", ordinal = 1), locals = LocalCapture.CAPTURE_FAILHARD)
private void storeExistenceToCache(ResourcePackType type, ResourceLocation location, CallbackInfoReturnable<Boolean> cir, String s) {
resourceContainmentCache.get(type).put(new ResourceLocation(location.getNamespace().intern(), location.getPath()), cir.getReturnValue());
}
@Inject(method = "resourceExists", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResource(Ljava/lang/String;)Ljava/net/URL;"), cancellable = true)
private void useCacheForExistence(ResourcePackType type, ResourceLocation location, CallbackInfoReturnable<Boolean> cir) {
Boolean b = resourceContainmentCache.get(type).getOrDefault(location, null);
if(b != null)
cir.setReturnValue(b);
@Inject(method = "resourceExists", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResource(Ljava/lang/String;)Ljava/net/URL;"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
private void useCacheForExistence(ResourcePackType type, ResourceLocation location, CallbackInfoReturnable<Boolean> cir, String path) {
FileSystem fs = FILE_SYSTEMS_BY_PACK_TYPE.get(type);
cir.setReturnValue(Files.exists(fs.getPath(path)));
}
}