Skip to content

Commit 87d1aad

Browse files
committed
Backport unbaked model caching from 1.21
1 parent 6f4212e commit 87d1aad

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.world.level.block.state.StateDefinition;
1414
import org.embeddedt.modernfix.ModernFix;
1515
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
16+
import org.embeddedt.modernfix.util.CacheUtil;
1617
import org.embeddedt.modernfix.util.LambdaMap;
1718
import org.spongepowered.asm.mixin.Mixin;
1819
import org.spongepowered.asm.mixin.Shadow;
@@ -45,12 +46,14 @@ private void injectDummyBakedRegistry(CallbackInfo ci) {
4546

4647
@Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/ModelManager;loadBlockModels(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"))
4748
private CompletableFuture<Map<ResourceLocation, BlockModel>> deferBlockModelLoad(ResourceManager manager, Executor executor) {
48-
return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockModel(manager, location)));
49+
var cache = CacheUtil.<ResourceLocation, BlockModel>simpleCacheForLambda(location -> loadSingleBlockModel(manager, location), 100L);
50+
return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location)));
4951
}
5052

5153
@Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/ModelManager;loadBlockStates(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"))
5254
private CompletableFuture<Map<ResourceLocation, List<ModelBakery.LoadedJson>>> deferBlockStateLoad(ResourceManager manager, Executor executor) {
53-
return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockState(manager, location)));
55+
var cache = CacheUtil.<ResourceLocation, List<ModelBakery.LoadedJson>>simpleCacheForLambda(location -> loadSingleBlockState(manager, location), 100L);
56+
return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location)));
5457
}
5558

5659
@Redirect(method = "loadModels", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;"))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.embeddedt.modernfix.util;
2+
3+
import com.google.common.cache.CacheBuilder;
4+
import com.google.common.cache.CacheLoader;
5+
import com.google.common.cache.LoadingCache;
6+
7+
import java.util.function.Function;
8+
9+
public class CacheUtil {
10+
public static <K, V> LoadingCache<K, V> simpleCacheForLambda(Function<K, V> function, long maxSize) {
11+
return CacheBuilder.newBuilder().maximumSize(maxSize).build(new CacheLoader<K, V>() {
12+
@Override
13+
public V load(K key) throws Exception {
14+
return function.apply(key);
15+
}
16+
});
17+
}
18+
}

0 commit comments

Comments
 (0)