More aggressive interning of cached path components

This commit is contained in:
embeddedt 2023-04-16 12:48:16 -04:00
parent 3922c3ec26
commit ea86bc6850
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 87 additions and 12 deletions

View File

@ -1,12 +1,15 @@
package org.embeddedt.modernfix.mixin.perf.resourcepacks;
import com.google.common.base.Joiner;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.ResourcePackFileNotFoundException;
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
import net.minecraftforge.fml.packs.ModFileResourcePack;
import org.apache.commons.lang3.tuple.Triple;
import org.embeddedt.modernfix.util.CachedResourcePath;
import org.embeddedt.modernfix.util.FileUtil;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@ -32,8 +35,8 @@ public abstract class ModFileResourcePackMixin {
@Shadow(remap = false) @Final private ModFile modFile;
private EnumMap<PackType, Set<String>> namespacesByType;
private EnumMap<PackType, HashMap<String, List<Triple<Integer, String, String>>>> rootListingByNamespaceAndType;
private Set<String> containedPaths;
private EnumMap<PackType, HashMap<String, List<CachedResourcePath>>> rootListingByNamespaceAndType;
private Set<CachedResourcePath> containedPaths;
private boolean useNamespaceCaches;
private FileSystem resourcePackFS;
private static Joiner slashJoiner = Joiner.on('/');
@ -51,21 +54,21 @@ public abstract class ModFileResourcePackMixin {
this.containedPaths = new HashSet<>();
for(PackType type : PackType.values()) {
Set<String> namespaces = this.namespacesByType.get(type);
HashMap<String, List<Triple<Integer, String, String>>> rootListingForNamespaces = new HashMap<>();
HashMap<String, List<CachedResourcePath>> rootListingForNamespaces = new HashMap<>();
for(String namespace : namespaces) {
try {
Path root = modFile.getLocator().findPath(modFile, type.getDirectory(), namespace).toAbsolutePath();
try (Stream<Path> stream = Files.walk(root)) {
ArrayList<Triple<Integer, String, String>> rootListingPaths = new ArrayList<>();
ArrayList<CachedResourcePath> rootListingPaths = new ArrayList<>();
stream
.map(path -> root.relativize(path.toAbsolutePath()))
.filter(this::isValidCachedResourcePath)
.forEach(path -> {
if(!path.toString().endsWith(".mcmeta")) {
rootListingPaths.add(Triple.of(path.getNameCount(), path.getFileName().toString(), slashJoiner.join(path)));
rootListingPaths.add(new CachedResourcePath(path));
}
String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path);
this.containedPaths.add(mergedPath);
this.containedPaths.add(new CachedResourcePath(mergedPath));
});
rootListingPaths.trimToSize();
rootListingForNamespaces.put(namespace, rootListingPaths);
@ -100,7 +103,7 @@ public abstract class ModFileResourcePackMixin {
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true)
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(this.containedPaths.contains(FileUtil.normalize(path)));
cir.setReturnValue(this.containedPaths.contains(new CachedResourcePath(path)));
}
@Inject(method = "getResource(Ljava/lang/String;)Ljava/io/InputStream;", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;exists(Ljava/nio/file/Path;[Ljava/nio/file/LinkOption;)Z"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
@ -123,13 +126,12 @@ public abstract class ModFileResourcePackMixin {
pathIn = pathIn + "/";
final String testPath = pathIn;
cir.setReturnValue(this.rootListingByNamespaceAndType.get(type).getOrDefault(resourceNamespace, Collections.emptyList()).stream().
filter(path -> path.getLeft() <= maxDepth). // Make sure the depth is within bounds
filter(path -> path.getRight().startsWith(testPath)). // Make sure the target path is inside this one
filter(path -> filter.test(path.getMiddle())). // Test the file name against the predicate
filter(path -> path.getNameCount() <= maxDepth). // Make sure the depth is within bounds
filter(path -> path.getFullPath().startsWith(testPath)). // Make sure the target path is inside this one
filter(path -> filter.test(path.getFileName())). // Test the file name against the predicate
// Finally we need to form the RL, so use the first name as the domain, and the rest as the path
// It is VERY IMPORTANT that we do not rely on Path.toString as this is inconsistent between operating systems
// Join the path names ourselves to force forward slashes
map(path -> new ResourceLocation(resourceNamespace, path.getRight())).
map(path -> new ResourceLocation(resourceNamespace, path.getFullPath())).
collect(Collectors.toList()));
}
}

View File

@ -0,0 +1,73 @@
package org.embeddedt.modernfix.util;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.google.common.collect.Streams;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
public class CachedResourcePath {
private final ImmutableList<String> pathComponents;
private int hashCode = 0;
private static final Interner<String> PATH_COMPONENT_INTERNER = Interners.newStrongInterner();
private static final Splitter SLASH_SPLITTER = Splitter.on('/');
private static final Joiner SLASH_JOINER = Joiner.on('/');
private WeakReference<String> fullPathCache = new WeakReference<>(null);
public CachedResourcePath(Iterable<String> components) {
ImmutableList.Builder<String> b = ImmutableList.builder();
for(String s : components) {
if(s == null || s.length() == 0)
continue;
b.add(PATH_COMPONENT_INTERNER.intern(s));
}
pathComponents = b.build();
}
public CachedResourcePath(Path path) {
this(() -> Streams.stream(path.iterator()).map(Path::toString).iterator());
}
public CachedResourcePath(String s) {
this(SLASH_SPLITTER.split(s));
}
@Override
public int hashCode() {
int result = hashCode;
if(result != 0)
return result;
hashCode = pathComponents.hashCode();
return hashCode;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CachedResourcePath that = (CachedResourcePath) o;
return pathComponents.equals(that.pathComponents);
}
public String getFileName() {
return pathComponents.get(pathComponents.size() - 1);
}
public int getNameCount() {
return pathComponents.size();
}
public String getFullPath() {
String fPath = fullPathCache.get();
if(fPath == null) {
fPath = SLASH_JOINER.join(pathComponents);
fullPathCache = new WeakReference<>(fPath);
}
return fPath;
}
}