mirror of
https://github.com/Dreeam-qwq/Gale.git
synced 2025-12-19 14:59:29 +00:00
188 lines
6.7 KiB
Diff
188 lines
6.7 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Martijn Muijsers <martijnmuijsers@live.nl>
|
|
Date: Tue, 8 Aug 2023 20:43:20 +0200
|
|
Subject: [PATCH] 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/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
|
|
index d02546b18cb689724887b4e85e8d32a18828a4ad..3bf983d4c51d517db613a00cb31dea5440cec300 100644
|
|
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
|
|
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
|
|
@@ -166,13 +166,11 @@ public final class MCUtil {
|
|
}
|
|
|
|
public static int fastFloor(double x) {
|
|
- int truncated = (int)x;
|
|
- return x < (double)truncated ? truncated - 1 : truncated;
|
|
+ return (int) Math.floor(x); // Gale - use platform math functions
|
|
}
|
|
|
|
public static int fastFloor(float x) {
|
|
- int truncated = (int)x;
|
|
- return x < (double)truncated ? truncated - 1 : truncated;
|
|
+ return (int) Math.floor(x); // Gale - use platform math functions
|
|
}
|
|
|
|
public static float normalizeYaw(float f) {
|
|
@@ -233,11 +231,11 @@ public final class MCUtil {
|
|
}
|
|
|
|
public static int getChunkCoordinate(final double coordinate) {
|
|
- return MCUtil.fastFloor(coordinate) >> 4;
|
|
+ return ((int) Math.floor(coordinate)) >> 4; // Gale - use platform math functions
|
|
}
|
|
|
|
public static int getBlockCoordinate(final double coordinate) {
|
|
- return MCUtil.fastFloor(coordinate);
|
|
+ return (int) Math.floor(coordinate); // Gale - use platform math functions
|
|
}
|
|
|
|
public static long getBlockKey(final int x, final int y, final int z) {
|
|
diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java
|
|
index 33ace08d15bbcad66c09c7bd65dd03e784246e8f..d04586223a9b2a30efb17da01fb7f3e8f2a6af7b 100644
|
|
--- a/src/main/java/net/minecraft/util/Mth.java
|
|
+++ b/src/main/java/net/minecraft/util/Mth.java
|
|
@@ -51,13 +51,11 @@ 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) {
|
|
@@ -74,13 +72,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) {
|
|
@@ -116,15 +112,7 @@ public class Mth {
|
|
}
|
|
|
|
public static double absMax(double a, double b) {
|
|
- if (a < 0.0D) {
|
|
- a = -a;
|
|
- }
|
|
-
|
|
- if (b < 0.0D) {
|
|
- 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) {
|