Use smarter iteration order in model bake event registry

This heavily improves the rate of cache hits when mods are baking
many variants of the same model via iteration over the keys,
as now all the variants will be obtained at once rather than
being retrieved randomly.

Cable Tiers used to take 8 seconds in the event before this change
on a Ryzen 7700X, and is now nearly instant.
This commit is contained in:
embeddedt 2025-04-26 12:06:12 -04:00
parent 455b56749c
commit 51c4f3eae8
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -6,6 +6,7 @@ import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery;
@ -54,7 +55,7 @@ public class ModelBakeEventHelper {
private final MutableGraph<String> dependencyGraph;
public ModelBakeEventHelper(Map<ResourceLocation, BakedModel> modelRegistry) {
this.modelRegistry = modelRegistry;
this.topLevelModelLocations = new HashSet<>(modelRegistry.keySet());
this.topLevelModelLocations = new ObjectLinkedOpenHashSet<>();
// Skip going through ModelLocationCache because most of the accesses will be misses
ForgeRegistries.BLOCKS.getEntries().forEach(entry -> {
var location = entry.getKey().location();
@ -63,6 +64,7 @@ public class ModelBakeEventHelper {
}
});
ForgeRegistries.ITEMS.getKeys().forEach(key -> topLevelModelLocations.add(new ModelResourceLocation(key, "inventory")));
this.topLevelModelLocations.addAll(modelRegistry.keySet());
this.dependencyGraph = GraphBuilder.undirected().build();
ModList.get().forEachModContainer((id, mc) -> {
this.dependencyGraph.addNode(id);