mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
39 lines
2.2 KiB
Diff
39 lines
2.2 KiB
Diff
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 69992ebc999ea3ff9e47e4e049bcc514c01150ca..2118ad41e94e8f5e5fac50286ceb24ab70db88f2 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
|
|
@@ -23,9 +23,11 @@ public class AttributeMap {
|
|
private final Set<AttributeInstance> attributesToSync = new ObjectOpenHashSet<>();
|
|
private final Set<AttributeInstance> attributesToUpdate = new ObjectOpenHashSet<>();
|
|
private final AttributeSupplier supplier;
|
|
+ private final java.util.function.Function<Holder<Attribute>, 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) {
|
|
@@ -49,7 +51,13 @@ public class AttributeMap {
|
|
|
|
@Nullable
|
|
public AttributeInstance getInstance(Holder<Attribute> 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> attribute) {
|