9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/patches/server/0117-Optimize-Entity-distanceToSqr.patch
Dreeam a1415c3f29 Updated Upstream (Gale)
Upstream has released updates that appear to apply and compile correctly

Gale Changes:
Dreeam-qwq/Gale@f2c8aaf Sync update from ver/1.21.1 branch
2024-12-24 11:49:20 -05:00

83 lines
4.2 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/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index d203ab8cd02432e05d6e3b8766d5a733e570173d..c1a7799dc4c44e5988b15f878633dafb20e257eb 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -2310,33 +2310,41 @@ 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());
+ // Leaf start - Optimize distanceTo - inlining
+ public final float distanceTo(Entity entity) {
+ // Leaf start - Optimize distanceTo - Avoid casting
+ final double dx = this.getX() - entity.getX();
+ final double dy = this.getY() - entity.getY();
+ final double dz = this.getZ() - entity.getZ();
+ // Leaf end - Optimize distanceTo - Avoid casting
- return Mth.sqrt(f * f + f1 * f1 + f2 * f2);
+ 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
- public double distanceToSqr(double x, double y, double z) {
- double d3 = this.getX() - x;
- double d4 = this.getY() - y;
- double d5 = this.getZ() - z;
+ // Leaf start - Optimize distanceToSqr - inlining
+ 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 d3 * d3 + d4 * d4 + d5 * d5;
+ return org.dreeam.leaf.LeafBootstrap.enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz; // Leaf - Use FMA for distanceToSqr
}
+ // Leaf end - Optimize distanceToSqr
public double distanceToSqr(Entity entity) {
return this.distanceToSqr(entity.position());
}
- public double distanceToSqr(Vec3 vector) {
- double d0 = this.getX() - vector.x;
- double d1 = this.getY() - vector.y;
- double d2 = this.getZ() - vector.z;
+ // Leaf start - Optimize distanceToSqr - inlining
+ 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 d0 * d0 + d1 * d1 + d2 * d2;
+ return org.dreeam.leaf.LeafBootstrap.enableFMA ? Math.fma(dx, dx, Math.fma(dy, dy, dz * dz)) : dx * dx + dy * dy + dz * dz; // Leaf - Use FMA for distanceToSqr
}
+ // Leaf end - Optimize distanceToSqr - inlining
public void playerTouch(Player player) {}
diff --git a/src/main/java/org/dreeam/leaf/LeafBootstrap.java b/src/main/java/org/dreeam/leaf/LeafBootstrap.java
index 316654051b80ac0fd62cf3b7a0e1b91010ec24b7..0ffa8fb14d02bccc44685ece8cb9d128bfaec405 100644
--- a/src/main/java/org/dreeam/leaf/LeafBootstrap.java
+++ b/src/main/java/org/dreeam/leaf/LeafBootstrap.java
@@ -4,6 +4,7 @@ import io.papermc.paper.PaperBootstrap;
import joptsimple.OptionSet;
public class LeafBootstrap {
+ public static final boolean enableFMA = Boolean.parseBoolean(System.getProperty("Leaf.enableFMA", "false")); // Leaf - FMA feature
public static void boot(final OptionSet options) {
runPreBootTasks();