From 10658f682e5c962ff029e2ba1e29468cf9048dca Mon Sep 17 00:00:00 2001 From: RainWarrior Date: Wed, 30 Dec 2015 16:08:08 +0300 Subject: [PATCH] Fixed broken leaky caching of OBJ models. --- .../client/model/obj/OBJModel.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java b/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java index e32bff82b..c11676bae 100644 --- a/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java +++ b/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java @@ -11,6 +11,7 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; @@ -54,9 +55,13 @@ import org.apache.commons.lang3.tuple.Pair; import com.google.common.base.Charsets; import com.google.common.base.Function; import com.google.common.base.Optional; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; @SuppressWarnings("deprecation") public class OBJModel implements IRetexturableModel, IModelCustomData @@ -1142,7 +1147,7 @@ public class OBJModel implements IRetexturableModel, IModelCustomData public static class OBJState implements IModelState { - protected Map visibilityMap = new HashMap(); + protected Map visibilityMap = Maps.newHashMap(); public IModelState parent; protected Operation operation = Operation.SET_TRUE; @@ -1236,6 +1241,27 @@ public class OBJModel implements IRetexturableModel, IModelCustomData return builder.toString(); } + @Override + public int hashCode() + { + return Objects.hash(visibilityMap, parent, operation); + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OBJState other = (OBJState) obj; + return Objects.equals(visibilityMap, other.visibilityMap) && + Objects.equals(parent, other.parent) && + operation == other.operation; + } + public enum Operation { SET_TRUE, @@ -1546,15 +1572,17 @@ public class OBJModel implements IRetexturableModel, IModelCustomData } } - private final Map cache = new HashMap(); + private final LoadingCache cache = CacheBuilder.newBuilder().maximumSize(20).build(new CacheLoader() + { + public OBJBakedModel load(IModelState state) throws Exception + { + return new OBJBakedModel(model, state, format, textures); + } + }); public OBJBakedModel getCachedModel(IModelState state) { - if (!cache.containsKey(state)) - { - cache.put(state, new OBJBakedModel(this.model, state, this.format, this.textures)); - } - return cache.get(state); + return cache.getUnchecked(state); } public OBJModel getModel()