From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Wed, 17 Aug 2022 10:48:18 +0800 Subject: [PATCH] Reduce entity allocations This patch is Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish) diff --git a/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeMap.java b/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeMap.java index 9ef8f014af332da129bfcd3370da983ec035ecc6..1e5f018ea357c3431d5aabbe435ce7d922f5c3bf 100644 --- a/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeMap.java +++ b/src/main/java/net/minecraft/world/entity/ai/attributes/AttributeMap.java @@ -22,9 +22,11 @@ public class AttributeMap { private final Map, AttributeInstance> attributes = new Object2ObjectOpenHashMap<>(); private final Set dirtyAttributes = new ObjectOpenHashSet<>(); private final AttributeSupplier supplier; + private final java.util.function.Function, AttributeInstance> createInstance; // Leaves - reduce entity allocations public AttributeMap(AttributeSupplier defaultAttributes) { this.supplier = defaultAttributes; + this.createInstance = attribute -> this.supplier.createInstance(this::onAttributeModified, attribute); } private void onAttributeModified(AttributeInstance instance) { @@ -43,7 +45,13 @@ public class AttributeMap { @Nullable public AttributeInstance getInstance(Holder attribute) { - return this.attributes.computeIfAbsent(attribute, attributex -> this.supplier.createInstance(this::onAttributeModified, attributex)); + // Leaves start - cache lambda, as for some reason java allocates it anyways + if (org.leavesmc.leaves.LeavesConfig.reduceEntityAllocations) { + return this.attributes.computeIfAbsent(attribute, this.createInstance); + } else { + return this.attributes.computeIfAbsent(attribute, attributex -> this.supplier.createInstance(this::onAttributeModified, attributex)); + } + // Leaves end - cache lambda, as for some reason java allocates it anyways } public boolean hasAttribute(Holder attribute) {