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/net/minecraft/world/entity/ai/attributes/AttributeMap.java b/net/minecraft/world/entity/ai/attributes/AttributeMap.java index bed9b564c493cd84bf53fc49368fda736f3fbc2b..69ec8e21c96e26bafedcdd979fc9d050b1f3846f 100644 --- a/net/minecraft/world/entity/ai/attributes/AttributeMap.java +++ b/net/minecraft/world/entity/ai/attributes/AttributeMap.java @@ -18,9 +18,11 @@ public class AttributeMap { private final Set attributesToSync = new ObjectOpenHashSet<>(); private final Set attributesToUpdate = new ObjectOpenHashSet<>(); private final AttributeSupplier supplier; + private final java.util.function.Function, AttributeInstance> createInstance; // Leaves - reduce entity allocations public AttributeMap(AttributeSupplier supplier) { this.supplier = supplier; + this.createInstance = attribute -> this.supplier.createInstance(this::onAttributeModified, attribute); } private void onAttributeModified(AttributeInstance instance) { @@ -44,7 +46,13 @@ public class AttributeMap { @Nullable public AttributeInstance getInstance(Holder attribute) { - return this.attributes.computeIfAbsent(attribute, holder -> this.supplier.createInstance(this::onAttributeModified, (Holder)holder)); + // Leaves start - cache lambda, as for some reason java allocates it anyways + if (org.leavesmc.leaves.LeavesConfig.performance.reduceEntityAllocations) { + return this.attributes.computeIfAbsent(attribute, this.createInstance); + } else { + return this.attributes.computeIfAbsent(attribute, holder -> this.supplier.createInstance(this::onAttributeModified, (Holder)holder)); + } + // Leaves end - cache lambda, as for some reason java allocates it anyways } public boolean hasAttribute(Holder attribute) {