Files
LuminolMC/patches/server/0046-Gale-Use-platform-math-functions.patch
2024-12-31 21:24:17 +08:00

160 lines
5.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <wangxyper@163.com>
Date: Wed, 31 Jul 2024 13:38:04 +0800
Subject: [PATCH] Gale Use platform math functions
License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following patch:
"Use Math.floor instead of fastfloor"
By: Xymb <xymb@endcrystal.me>
As part of: Kaiiju (https://github.com/KaiijuMC/Kaiiju)
Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
* Comparison of floor methods used in Paper *
Measure shown is floored number of milliseconds
(nanoseconds integer-divided by 1_000_000
taken to get the floor of 1000 randomly chosen doubles
(all in the range of [-Integer.MAX_VALUE + 10, Integer.MAX_VALUE - 10])
100_000 times (making it 100_000_000 floor operations total)
and adding it to a total.
We are testing the following methods:
* net.minecraft.util.Mth.floor
* java.lang.Math.floor
* java.lang.StrictMath.floor
* org.apache.commons.math3.util.FastMath.floor
* org.apache.commons.math3.util.FastMath.floor, but with a hot start (see comment in code)
* io.papermc.paper.util.MCUtil.fastFloor
The tests performed clearly show that Math.floor is the fastest.
This is most likely due to java.lang.Math's usage of the @IntrinsicCandidate
annotation, which allows the JVM to use a more optimized implementation at runtime.
However, in the case that there is no intrinsic replacement for Math.floor,
it defers to StrictMath.floor, which relies on a number of native methods, and is
still much faster than the existing Minecraft utility functions.
Therefore, using Math.floor instead of these functions is better regardless.
In Apache Commons Math 4, FastMath.floor has also been removed in favor of Math.floor.
The versions used:
* Windows 10 Home 21H2 19044.3086
* OpenJDK Runtime Environment Temurin-19.0.2+7 (build 19.0.2+7)
* Paper a3c760e6af1e8c7244ef75c6da6e6df278a79e14 on Minecraft 1.20.1
* Apache Commons Math 3.6.1
Results:
Total is of type int Total is of type double
----------------------------------------------------------------------------------
Mth.floor 2113 (double) Mth.floor 2658
(int) Math.floor 130 Math.floor 194
(int) StrictMath.floor 835 StrictMath.floor 381
(int) FastMath.floor 412 FastMath.floor 376
(int) FastMath.floor with hot start 359 FastMath.floor with hot start 321
MCUtil.fastFloor 2284 (double) MCUtil.fastFloor 2469
Code is below:
```java
package somepackage;
import io.papermc.paper.util.MCUtil;
import net.minecraft.util.Mth;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// IF FastMath.floor with a hot start:
// FastMath.floor(37485.5);
var random = new Random(4889338);
int size = 1000;
var values = new double[size];
double bound = Integer.MAX_VALUE - 10;
for (int i = 0; i < size; i++) {
values[i] = random.nextDouble(bound * 2) - bound;
}
int repeats = 100_000;
// int total = 0;
// OR
// double total = 0;
long start = System.nanoTime();
for (int repeat = 0; repeat < repeats; repeat++) {
for (int index = 0; index < size; index++) {
total += insert_function_being_tested_here(values[index]);
}
}
long diff = System.nanoTime() - start;
System.out.println(total);
System.out.println(diff / 1_000_000L);
}
}
```
diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java
index f298cdfcf1539e467f57f9f7789de3cf2ca54665..3468b7fbc7440f220fce8039f237658a593df296 100644
--- a/src/main/java/net/minecraft/util/Mth.java
+++ b/src/main/java/net/minecraft/util/Mth.java
@@ -58,18 +58,15 @@ public class Mth {
}
public static int floor(float value) {
- int i = (int)value;
- return value < (float)i ? i - 1 : i;
+ return (int) Math.floor(value); // Gale - use platform math functions
}
public static int floor(double value) {
- int i = (int)value;
- return value < (double)i ? i - 1 : i;
+ return (int) Math.floor(value); // Gale - use platform math functions
}
public static long lfloor(double value) {
- long l = (long)value;
- return value < (double)l ? l - 1L : l;
+ return (long) Math.floor(value); // Gale - use platform math functions
}
public static float abs(float value) {
@@ -81,13 +78,11 @@ public class Mth {
}
public static int ceil(float value) {
- int i = (int)value;
- return value > (float)i ? i + 1 : i;
+ return (int) Math.ceil(value); // Gale - use platform math functions
}
public static int ceil(double value) {
- int i = (int)value;
- return value > (double)i ? i + 1 : i;
+ return (int) Math.ceil(value); // Gale - use platform math functions
}
public static int clamp(int value, int min, int max) {
@@ -123,15 +118,7 @@ public class Mth {
}
public static double absMax(double a, double b) {
- if (a < 0.0) {
- a = -a;
- }
-
- if (b < 0.0) {
- b = -b;
- }
-
- return Math.max(a, b);
+ return Math.max(Math.abs(a), Math.abs(b)); // Gale - use platform math functions
}
public static int floorDiv(int dividend, int divisor) {