Improve performance of vertex format mapping by caching hashcodes (#4370)

This commit is contained in:
Ben Staddon authored and LexManos committed 2017-10-02 16:45:58 -07:00
1 parent 4073891058
commit 0d34b02f88
2 files changed
+48 -16

No files matched your search

@@ -0,0 +1,38 @@
--- ../src-base/minecraft/net/minecraft/client/renderer/vertex/VertexFormat.java
+++ ../src-work/minecraft/net/minecraft/client/renderer/vertex/VertexFormat.java
@@ -17,6 +17,7 @@
private int field_177354_e;
private final List<Integer> field_177351_f;
private int field_177352_g;
+ private int hashCode;
public VertexFormat(VertexFormat p_i46097_1_)
{
@@ -47,6 +48,7 @@
this.field_177351_f.clear();
this.field_177352_g = -1;
this.field_177353_d = 0;
+ this.hashCode = 0;
}
@SuppressWarnings("incomplete-switch")
@@ -75,6 +77,7 @@
}
this.field_177353_d += p_181721_1_.func_177368_f();
+ this.hashCode = 0;
return this;
}
}
@@ -204,9 +207,11 @@
public int hashCode()
{
+ if (this.hashCode != 0) return this.hashCode;
int i = this.field_177355_b.hashCode();
i = 31 * i + this.field_177356_c.hashCode();
i = 31 * i + this.field_177353_d;
+ this.hashCode = i;
return i;
}
}
@@ -30,9 +30,8 @@ import net.minecraftforge.client.ForgeHooksClient;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class LightUtil
{
@@ -90,16 +89,7 @@ public class LightUtil
}
}
private static final LoadingCache<Pair<VertexFormat, VertexFormat>, int[]> formatMaps = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<Pair<VertexFormat, VertexFormat>, int[]>()
{
@Override
public int[] load(Pair<VertexFormat, VertexFormat> pair)
{
return mapFormats(pair.getLeft(), pair.getRight());
}
});
private static final ConcurrentMap<Pair<VertexFormat, VertexFormat>, int[]> formatMaps = new ConcurrentHashMap<>();
public static void putBakedQuad(IVertexConsumer consumer, BakedQuad quad)
{
@@ -110,20 +100,19 @@ public class LightUtil
consumer.setQuadTint(quad.getTintIndex());
}
consumer.setApplyDiffuseLighting(quad.shouldApplyDiffuseLighting());
//int[] eMap = mapFormats(consumer.getVertexFormat(), DefaultVertexFormats.ITEM);
float[] data = new float[4];
VertexFormat formatFrom = consumer.getVertexFormat();
VertexFormat formatTo = quad.getFormat();
int countFrom = formatFrom.getElementCount();
int countTo = formatTo.getElementCount();
int[] eMap = formatMaps.getUnchecked(Pair.of(formatFrom, formatTo));
int[] eMap = mapFormats(formatFrom, formatTo);
for(int v = 0; v < 4; v++)
{
for(int e = 0; e < countFrom; e++)
{
if(eMap[e] != countTo)
{
unpack(quad.getVertexData(), data, quad.getFormat(), v, eMap[e]);
unpack(quad.getVertexData(), data, formatTo, v, eMap[e]);
consumer.put(e, data);
}
else
@@ -135,6 +124,11 @@ public class LightUtil
}
public static int[] mapFormats(VertexFormat from, VertexFormat to)
{
return formatMaps.computeIfAbsent(Pair.of(from, to), pair -> generateMapping(pair.getLeft(), pair.getRight()));
}
private static int[] generateMapping(VertexFormat from, VertexFormat to)
{
int fromCount = from.getElementCount();
int toCount = to.getElementCount();