Make ModelLocationCache more low-memory friendly

This commit is contained in:
embeddedt 2023-04-08 20:01:40 -04:00
parent 775bc5f027
commit 1796d80d40
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 16 additions and 24 deletions

View File

@ -1,5 +1,8 @@
package org.embeddedt.modernfix.dynamicresources;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
@ -14,28 +17,23 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ModelLocationCache {
private static Map<BlockState, ModelResourceLocation> locationCache = Collections.emptyMap();
public static void rebuildLocationCache() {
ArrayList<CompletableFuture<Pair<BlockState, ModelResourceLocation>>> futures = new ArrayList<>();
for(Block block : Registry.BLOCK) {
block.getStateDefinition().getPossibleStates().forEach((state) -> {
futures.add(CompletableFuture.supplyAsync(() -> {
return Pair.of(state, BlockModelShaper.stateToModelLocation(state));
}, Util.backgroundExecutor()));
private static final LoadingCache<BlockState, ModelResourceLocation> locationCache = CacheBuilder.newBuilder()
.maximumSize(10000)
.build(new CacheLoader<BlockState, ModelResourceLocation>() {
@Override
public ModelResourceLocation load(BlockState key) throws Exception {
return BlockModelShaper.stateToModelLocation(key);
}
});
}
ImmutableMap.Builder<BlockState, ModelResourceLocation> builder = ImmutableMap.builder();
for(CompletableFuture<Pair<BlockState, ModelResourceLocation>> future : futures) {
Pair<BlockState, ModelResourceLocation> pair = future.join();
builder.put(pair.getFirst(), pair.getSecond());
}
locationCache = builder.build();
}
public static ModelResourceLocation get(BlockState state) {
return locationCache.get(state);
try {
return locationCache.get(state);
} catch(ExecutionException e) {
throw new RuntimeException(e.getCause());
}
}
}

View File

@ -128,8 +128,6 @@ public abstract class ModelBakeryMixin {
private UnbakedModel missingModel;
private static boolean firstReload = true;
/**
* @author embeddedt
* @reason don't load any models initially, just set up initial data structures
@ -137,10 +135,6 @@ public abstract class ModelBakeryMixin {
@Overwrite
protected void processLoading(ProfilerFiller arg, int maxMipLevels) {
ModelLoaderRegistry.onModelLoadingStart();
if(firstReload) {
ModelLocationCache.rebuildLocationCache();
firstReload = false;
}
try {
this.missingModel = this.loadBlockModel(MISSING_MODEL_LOCATION);
} catch (IOException var10) {