Skip to content

Commit

Permalink
Cleanup Lithium patches & Faster distanceTo & Optimize worldgen math (#…
Browse files Browse the repository at this point in the history
…166)

* dbqq

* Cleanup patch

* uwu

* import now back

* include info in patch header

* FMA for distanceTo

* Add detailed headers
  • Loading branch information
HaHaWTH authored Nov 19, 2024
1 parent 27134f6 commit 3b82780
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 22 deletions.
6 changes: 3 additions & 3 deletions patches/server/0135-Optimize-Entity-distanceToSqr.patch
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Date: Fri, 29 Oct 2077 00:00:00 +0800
Subject: [PATCH] Optimize Entity distanceToSqr

This patch optimizes Entity#distanceToSqr call by using Math#fma which is around 1.2x to 4x faster than original method,
avoids multiple casting in Entity#distanceTo, using Math#hypot instead of Mojang's Mth#sqrt. Additionally, this patch makes
avoids multiple casting in Entity#distanceTo, using Math#sqrt directly instead of Mojang's Mth#sqrt. Additionally, this patch makes
these methods more able to be inlined by the JIT compiler.

diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 08a3714c530fb375ee729e91965c65efb9e6e3d2..976d72b611011cccf4b4efadc9772ce1c68ef5ee 100644
index 08a3714c530fb375ee729e91965c65efb9e6e3d2..6a19390aff7c8f7170467d2c6306b00779f23082 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -2368,33 +2368,41 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand All @@ -28,7 +28,7 @@ index 08a3714c530fb375ee729e91965c65efb9e6e3d2..976d72b611011cccf4b4efadc9772ce1
+ // Leaf end - Optimize distanceTo - Avoid casting

- return Mth.sqrt(f * f + f1 * f1 + f2 * f2);
+ return (float) Math.hypot(dx, Math.hypot(dy, dz)); // Leaf - Use Math#hypot instead of Mojang's Mth#sqrt
+ return (float) Math.sqrt(org.dreeam.leaf.LeafBootstrap.enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz); // Leaf - Use Math#sqrt instead of Mojang's Mth#sqrt - only cast once
}
+ // Leaf end - Optimize distanceTo - inlining

Expand Down
31 changes: 12 additions & 19 deletions patches/server/0152-Lithium-equipment-tracking.patch
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ index 22da75d8197de29a150c9eade7994deecae53a10..aa8a5938984d6860deb67a36f85c83d9
public boolean equals(Object object) {
if (this == object) {
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 9de85972ba32fd2373f70f708aa1bfc6067e6e1c..3ae75230a4250665df7a72aff1d84317b81fc077 100644
index 9de85972ba32fd2373f70f708aa1bfc6067e6e1c..0e68d854955aea1068d4a605bca701f0a0e2f297 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -157,7 +157,7 @@ import org.bukkit.event.entity.EntityTeleportEvent;
Expand Down Expand Up @@ -371,7 +371,7 @@ index 9de85972ba32fd2373f70f708aa1bfc6067e6e1c..3ae75230a4250665df7a72aff1d84317
Map<EquipmentSlot, ItemStack> map = null;
EquipmentSlot[] aenumitemslot = EquipmentSlot.VALUES; // Gale - JettPack - reduce array allocations
int i = aenumitemslot.length;
@@ -4848,6 +4855,80 @@ public abstract class LivingEntity extends Entity implements Attackable {
@@ -4848,6 +4855,79 @@ public abstract class LivingEntity extends Entity implements Attackable {
flag = true;
return flag;
}
Expand Down Expand Up @@ -419,8 +419,7 @@ index 9de85972ba32fd2373f70f708aa1bfc6067e6e1c..3ae75230a4250665df7a72aff1d84317
+ @Override
+ public void notifyCount(ItemStack publisher, int zero, int newCount) {
+ if (newCount == 0) {
+ //noinspection unchecked
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<ItemStack>) (Object) publisher).unsubscribeWithData(this, zero);
+ publisher.unsubscribeWithData(this, zero);
+ }
+
+ this.onEquipmentReplaced(publisher, ItemStack.EMPTY);
Expand All @@ -442,10 +441,10 @@ index 9de85972ba32fd2373f70f708aa1bfc6067e6e1c..3ae75230a4250665df7a72aff1d84317
+ }
+
+ if (!oldStack.isEmpty()) {
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<ItemStack>) oldStack).unsubscribeWithData(this, 0);
+ oldStack.unsubscribeWithData(this, 0);
+ }
+ if (!newStack.isEmpty()) {
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<ItemStack>) newStack).subscribe(this, 0);
+ newStack.subscribe(this, 0);
+ }
+ }
+ // Leaf end - Lithium entity equipment tracking
Expand Down Expand Up @@ -599,7 +598,7 @@ index 3bb46ed871fd56bbbe52cfd2575f9e853e03cd73..6d73ae2a453feeae0aea67bb555c64a0
+ // Leaf end - Lithium equipment tracking
}
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd55f81d1fd 100644
index 933b7519da5330ea8acd05c337201f52cab12c3c..e5579b15f305fb216dadd8023c16178a342b4add 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -120,7 +120,7 @@ import org.bukkit.event.player.PlayerItemDamageEvent;
Expand All @@ -623,7 +622,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5

private static DataResult<ItemStack> validateStrict(ItemStack stack) {
DataResult<Unit> dataresult = ItemStack.validateComponents(stack.getComponents());
@@ -1368,6 +1373,23 @@ public final class ItemStack implements DataComponentHolder {
@@ -1368,6 +1373,21 @@ public final class ItemStack implements DataComponentHolder {
}

public void setCount(int count) {
Expand All @@ -633,9 +632,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5
+ countChangeSubscriber.notifyCount(this, this.subscriberData, count);
+ }
+ if (count == 0) {
+ //Safe because ComponentMapImplMixin implements the interface
+ //noinspection unchecked
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<PatchedDataComponentMap>) (Object) this.components).unsubscribe(this);
+ this.components.unsubscribe(this);
+ if (this.subscriber != null) {
+ this.subscriber.forceUnsubscribe(this, this.subscriberData);
+ this.subscriber = null;
Expand All @@ -647,7 +644,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5
this.count = count;
}

@@ -1423,4 +1445,91 @@ public final class ItemStack implements DataComponentHolder {
@@ -1423,4 +1443,87 @@ public final class ItemStack implements DataComponentHolder {
public boolean canBeHurtBy(DamageSource source) {
return !this.has(DataComponents.FIRE_RESISTANT) || !source.is(DamageTypeTags.IS_FIRE);
}
Expand Down Expand Up @@ -681,8 +678,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5
+ this.subscriber = net.caffeinemc.mods.lithium.common.util.change_tracking.ChangeSubscriber.without(this.subscriber, subscriber);
+
+ if (this.subscriber == null) {
+ //noinspection unchecked
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<PatchedDataComponentMap>) (Object) this.components).unsubscribe(this);
+ this.components.unsubscribe(this);
+ }
+ return retval;
+ }
Expand All @@ -697,8 +693,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5
+ this.subscriber = net.caffeinemc.mods.lithium.common.util.change_tracking.ChangeSubscriber.without(this.subscriber, subscriber, subscriberData, true);
+
+ if (this.subscriber == null) {
+ //noinspection unchecked
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<PatchedDataComponentMap>) (Object) this.components).unsubscribe(this);
+ this.components.unsubscribe(this);
+ }
+ }
+
Expand Down Expand Up @@ -733,9 +728,7 @@ index 933b7519da5330ea8acd05c337201f52cab12c3c..b4fc8fcfb7994705a4307d9b0731bcd5
+ }
+
+ private void startTrackingChanges() {
+ //Safe because ComponentMapImplMixin
+ //noinspection unchecked
+ ((net.caffeinemc.mods.lithium.common.util.change_tracking.ChangePublisher<PatchedDataComponentMap>) (Object) this.components).subscribe(this, 0);
+ this.components.subscribe(this, 0);
+ }
+ // Leaf end - Lithium equipment tracking
}
91 changes: 91 additions & 0 deletions patches/server/0154-C2ME-Optimize-world-gen-math.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <[email protected]>
Date: Tue, 9 Nov 2077 00:00:00 +0800
Subject: [PATCH] C2ME: Optimize world gen math

This patch is based on following mixins:
* "com/ishland/c2me/opts/math/mixin/MixinChunkPos.java"
* "com/ishland/c2me/opts/worldgen/vanilla/mixin/aquifer/MixinNoiseChunkGenerator.java"

As part of: C2ME-fabric (https://github.com/RelativityMC/C2ME-fabric)
Licensed under: MIT

Co-authored-by: Taiyou06 <[email protected]>

diff --git a/src/main/java/net/minecraft/world/level/ChunkPos.java b/src/main/java/net/minecraft/world/level/ChunkPos.java
index fa58eeec2b652f0fa251eedf11cfabde5fd3198b..4976627098381f70b10c7370529bb5000bc5626f 100644
--- a/src/main/java/net/minecraft/world/level/ChunkPos.java
+++ b/src/main/java/net/minecraft/world/level/ChunkPos.java
@@ -84,7 +84,12 @@ public class ChunkPos {

@Override
public boolean equals(Object object) {
- return this == object || object instanceof ChunkPos chunkPos && this.x == chunkPos.x && this.z == chunkPos.z;
+ // Leaf start - Use standard equals
+ if (object == this) return true;
+ if (object == null || object.getClass() != this.getClass()) return false;
+ ChunkPos thatPos = (ChunkPos) object;
+ return this.x == thatPos.x && this.z == thatPos.z;
+ // Leaf end - Use standard equals
}

public int getMiddleBlockX() {
diff --git a/src/main/java/net/minecraft/world/level/levelgen/Beardifier.java b/src/main/java/net/minecraft/world/level/levelgen/Beardifier.java
index ca93a97256350789ca56f910862c9d717ca7670b..3a1a5257e1a98cc1d520f407bb1f8c745cadd3df 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/Beardifier.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/Beardifier.java
@@ -132,8 +132,14 @@ public class Beardifier implements DensityFunctions.BeardifierOrMarker {
}

private static double getBuryContribution(double x, double y, double z) {
- double d = Mth.length(x, y, z);
- return Mth.clampedMap(d, 0.0, 6.0, 1.0, 0.0);
+ // Leaf start - Optimize method for beardifier
+ double d = Math.sqrt(x * x + y * y + z * z);
+ if (d > 6.0) {
+ return 0.0;
+ } else {
+ return 1.0 - d / 6.0;
+ }
+ // Leaf end - Optimize method for beardifier
}

private static double getBeardContribution(int x, int y, int z, int yy) {
diff --git a/src/main/java/net/minecraft/world/level/levelgen/NoiseBasedChunkGenerator.java b/src/main/java/net/minecraft/world/level/levelgen/NoiseBasedChunkGenerator.java
index c991da3d975e07f3e1e59d5b2e91ed629ea608e6..4a56c06fef4efbed9fa72cf11583f25854c5a9c2 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/NoiseBasedChunkGenerator.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/NoiseBasedChunkGenerator.java
@@ -76,14 +76,13 @@ public final class NoiseBasedChunkGenerator extends ChunkGenerator {
}

private static Aquifer.FluidPicker createFluidPicker(NoiseGeneratorSettings settings) {
+ // Leaf start - Optimize world gen
Aquifer.FluidStatus aquifer_b = new Aquifer.FluidStatus(-54, Blocks.LAVA.defaultBlockState());
int i = settings.seaLevel();
Aquifer.FluidStatus aquifer_b1 = new Aquifer.FluidStatus(i, settings.defaultFluid());
- Aquifer.FluidStatus aquifer_b2 = new Aquifer.FluidStatus(DimensionType.MIN_Y * 2, Blocks.AIR.defaultBlockState());
-
- return (j, k, l) -> {
- return k < Math.min(-54, i) ? aquifer_b : aquifer_b1;
- };
+ final int min = Math.min(-54, i);
+ return (j, k, l) -> k < min ? aquifer_b : aquifer_b1;
+ // Leaf end - Optimize world gen
}

@Override
diff --git a/src/main/java/net/minecraft/world/level/levelgen/synth/PerlinNoise.java b/src/main/java/net/minecraft/world/level/levelgen/synth/PerlinNoise.java
index 022dda9dded1bd96dcaf377b1d1a9711ea9c49e7..1b0c92f3c20a4edc85a976a0eec620d0a96b5bba 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/synth/PerlinNoise.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/synth/PerlinNoise.java
@@ -215,7 +215,9 @@ public class PerlinNoise {
}

public static double wrap(double value) {
- return value - (double)Mth.lfloor(value / 3.3554432E7 + 0.5) * 3.3554432E7;
+ // Leaf start - Avoid casting
+ return value - Math.floor(value / 3.3554432E7 + 0.5) * 3.3554432E7;
+ // Leaf end - Avoid casting
}

protected int firstOctave() {

0 comments on commit 3b82780

Please sign in to comment.