9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-archived-patches/unapplied/mcserver/0170-Optimize-Entity-distanceToSqr.patch
Dreeam 8bffdef317 More patches
Shallou - Habitat
Genre: Progressive House (maybe)
2025-09-29 14:23:25 -04:00

89 lines
3.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
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#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/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
index d544d66542d53db1a847a6b1ab8cf5ee848f31de..e563b636a05a299d860a334987594dbd5c7d3511 100644
--- a/net/minecraft/world/entity/Entity.java
+++ b/net/minecraft/world/entity/Entity.java
@@ -394,6 +394,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
private int sectionY = Integer.MIN_VALUE;
private int sectionZ = Integer.MIN_VALUE;
private boolean updatingSectionStatus;
+ private static final boolean enableFMA = Boolean.getBoolean("Leaf.enableFMA"); // Leaf - Optimize Entity distanceTo
@Override
public final boolean moonrise$isHardColliding() {
@@ -2260,31 +2261,6 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
return new Vec3(this.xOld, this.yOld, this.zOld);
}
- public float distanceTo(Entity entity) {
- float f = (float)(this.getX() - entity.getX());
- float f1 = (float)(this.getY() - entity.getY());
- float f2 = (float)(this.getZ() - entity.getZ());
- return Mth.sqrt(f * f + f1 * f1 + f2 * f2);
- }
-
- public double distanceToSqr(double x, double y, double z) {
- double d = this.getX() - x;
- double d1 = this.getY() - y;
- double d2 = this.getZ() - z;
- return d * d + d1 * d1 + d2 * d2;
- }
-
- public double distanceToSqr(Entity entity) {
- return this.distanceToSqr(entity.position());
- }
-
- public double distanceToSqr(Vec3 vec) {
- double d = this.getX() - vec.x;
- double d1 = this.getY() - vec.y;
- double d2 = this.getZ() - vec.z;
- return d * d + d1 * d1 + d2 * d2;
- }
-
public void playerTouch(Player player) {
}
@@ -5509,4 +5485,34 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
return false;
}
// Purpur end - Ridables
+
+ // Leaf start - Optimize Entity distanceTo
+ // Inlining and avoid casting
+ // Use Math#sqrt instead of Mojang's Mth#sqrt - only cast once
+ // Added option to enable FMA acceleration
+ public final float distanceTo(Entity entity) {
+ final double dx = this.getX() - entity.getX();
+ final double dy = this.getY() - entity.getY();
+ final double dz = this.getZ() - entity.getZ();
+ return (float) Math.sqrt(enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz);
+ }
+
+ public final double distanceToSqr(final double x, final double y, final double z) {
+ final double dx = this.getX() - x;
+ final double dy = this.getY() - y;
+ final double dz = this.getZ() - z;
+ return enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz;
+ }
+
+ public final double distanceToSqr(Vec3 vector) {
+ final double dx = this.getX() - vector.x;
+ final double dy = this.getY() - vector.y;
+ final double dz = this.getZ() - vector.z;
+ return enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz;
+ }
+
+ public double distanceToSqr(Entity entity) {
+ return this.distanceToSqr(entity.position());
+ }
+ // Leaf end - Optimize Entity distanceToSqr
}