diff --git a/common/src/main/java/org/embeddedt/modernfix/resources/PackResourcesCacheEngine.java b/common/src/main/java/org/embeddedt/modernfix/resources/PackResourcesCacheEngine.java index 479c8398..a49ff9a6 100644 --- a/common/src/main/java/org/embeddedt/modernfix/resources/PackResourcesCacheEngine.java +++ b/common/src/main/java/org/embeddedt/modernfix/resources/PackResourcesCacheEngine.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.stream.Stream; @@ -24,6 +25,8 @@ import java.util.stream.Stream; */ public class PackResourcesCacheEngine { private static final Joiner SLASH_JOINER = Joiner.on('/'); + private static final ConcurrentHashMap PATH_COMPONENT_INTERNER = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap CACHED_SPLIT_PATHS = new ConcurrentHashMap<>(); static class Node { Map children; @@ -100,7 +103,6 @@ public class PackResourcesCacheEngine { // used for log message this.debugPath = basePathRetriever.apply(PackType.CLIENT_RESOURCES).toAbsolutePath(); this.root.children = new Object2ObjectOpenHashMap<>(); - ObjectOpenHashSet pathKeys = new ObjectOpenHashSet<>(); for(PackType type : PackType.values()) { var typeRoot = new Node(); this.root.children.put(type.getDirectory(), typeRoot); @@ -114,8 +116,12 @@ public class PackResourcesCacheEngine { .filter(PackResourcesCacheEngine::isValidCachedResourcePath) .forEach(path -> { var node = typeRoot; - for (Path component : path) { - String key = pathKeys.addOrGet(component.toString()); + int nameCount = path.getNameCount(); + for (int i = 0; i < nameCount; i++) { + String key = path.getName(i).toString(); + if (i < (nameCount - 1)) { + key = PATH_COMPONENT_INTERNER.computeIfAbsent(key, Function.identity()); + } if (node.children == null) { node.children = new Object2ObjectOpenHashMap<>(); } @@ -205,4 +211,16 @@ public class PackResourcesCacheEngine { } node.collectResources(resourceNamespace, this.rootPathsByType.get(type).resolve(resourceNamespace), components, 0, maxDepth, output); } + + private static String[] decompose(String path) { + String[] components = path.split("/"); + for (int i = 0; i < components.length; i++) { + components[i] = PATH_COMPONENT_INTERNER.computeIfAbsent(components[i], Function.identity()); + } + return components; + } + + public static String[] decomposeCached(String path) { + return CACHED_SPLIT_PATHS.computeIfAbsent(path, PackResourcesCacheEngine::decompose); + } } diff --git a/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcepacks/ForgePathPackResourcesMixin.java b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcepacks/ForgePathPackResourcesMixin.java index 9abf6a84..4be3be65 100644 --- a/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcepacks/ForgePathPackResourcesMixin.java +++ b/forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/resourcepacks/ForgePathPackResourcesMixin.java @@ -102,6 +102,6 @@ public abstract class ForgePathPackResourcesMixin implements ICachingResourcePac if(!PackTypeHelper.isVanillaPackType(type)) return; ci.cancel(); - this.generateResourceCache().collectResources(type, namespace, path.split("/"), Integer.MAX_VALUE, resourceOutput); + this.generateResourceCache().collectResources(type, namespace, PackResourcesCacheEngine.decomposeCached(path), Integer.MAX_VALUE, resourceOutput); } }