9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0091-Optimize-Entity-distanceToSqr.patch
Dreeam 7e89480eef Updated Upstream (Paper/Gale/Purpur)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@1004374a Add further information to thread check errors
PaperMC/Paper@e2f0efd1 Remove nms.Entity#isChunkLoaded
PaperMC/Paper@54b2e9d9 Add buffer to CraftWorld#warnUnsafeChunk
PaperMC/Paper@d4a95784 Experimental annotation changes (#12028)
PaperMC/Paper@5bcfb12a Fix activation range config and water animal status (#12047)
PaperMC/Paper@e0711af5 Deprecate UnsafeValues#getSpawnEggLayerColor (#12041)
PaperMC/Paper@8927091a Do not record movement for vehicles/players unaffected by blocks
PaperMC/Paper@5395ae37 Fix composter block setting bukkit owner twice (#12058)

Gale Changes:
Dreeam-qwq/Gale@534623a0 Updated Upstream (Paper)
Dreeam-qwq/Gale@7274f287 Updated Upstream (Paper)
Dreeam-qwq/Gale@e9ffadfd Updated Upstream (Paper)
Dreeam-qwq/Gale@9e6d19de Updated Upstream (Paper)
Dreeam-qwq/Gale@f35382c4 Updated Upstream (Paper)

Purpur Changes:
PurpurMC/Purpur@9b575764 Updated Upstream (Paper)
PurpurMC/Purpur@88ed7442 Updated Upstream (Paper)
PurpurMC/Purpur@79c11927 only initialize the config once, closes #1637
PurpurMC/Purpur@c7bb955e Updated Upstream (Paper)
PurpurMC/Purpur@2fe4cf18 [ci/skip] fix indentation
PurpurMC/Purpur@65637fa6 fix(mobs/bee): Set `takes-damage-from-water` back to false by default, closes #1639
PurpurMC/Purpur@16cfd04b feat(mobs/bee): Add `can-instantly-start-drowning` option, defaults to `true`
PurpurMC/Purpur@4f481858 fix: correctly call force when sending particles
2025-02-03 15:56:12 -05:00

81 lines
3.4 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 40bc2f9ffc565229f1ba8910ebc3bb7be5f849c6..3beb2b05265cf274f52170018cc22243b06c05e9 100644
--- a/net/minecraft/world/entity/Entity.java
+++ b/net/minecraft/world/entity/Entity.java
@@ -2193,31 +2193,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) {
}
@@ -5229,4 +5204,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(org.dreeam.leaf.LeafBootstrap.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 org.dreeam.leaf.LeafBootstrap.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 org.dreeam.leaf.LeafBootstrap.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
}