From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: etil2jz <81570777+etil2jz@users.noreply.github.com> Date: Wed, 29 Jun 2022 02:49:45 +0200 Subject: [PATCH] Carpet-Fixes: Optimize `Math.round` and `Math.hypot` functions Original license: MIT Original project: https://github.com/fxmorin/carpet-fixes Copyright (c) 2020 Fx Morin diff --git a/src/main/java/carpetfixes/helpers/FastMath.java b/src/main/java/carpetfixes/helpers/FastMath.java new file mode 100644 index 0000000000000000000000000000000000000000..7166a7d7fa9f3c21e7b36a9b8ae2ece07ed55f4d --- /dev/null +++ b/src/main/java/carpetfixes/helpers/FastMath.java @@ -0,0 +1,61 @@ +package carpetfixes.helpers; + +import org.dreeam.leaf.LeafConfig; + +public class FastMath { + + private static final double HYPOT_MAX_MAG = 2^511; + private static final double HYPOT_FACTOR = 2^750; + + /** + * @author FX - PR0CESS + * ~1.25x faster than {@link Math#round(float)} + */ + public static int round(float a) { + if (LeafConfig.optimizedRound) return a > 0F ? (int)(a + .5F) : (int)(a - .5F); + return Math.round(a); + } + + /** + * @author FX - PR0CESS + * ~1.28x faster than {@link Math#round(double)} + */ + public static long round(double a) { + if (LeafConfig.optimizedRound) return a > 0D ? (long)(a + .5D) : (long)(a - .5D); + return Math.round(a); + } + + /** + * @author FX - PR0CESS + * Hypot implementation from the jafama library. Not 100% accurate! (3E-14%, 15 is perfectly accurate) + * ~1.6x faster than {@link Math#hypot(double,double)} + */ + public static double hypot(double x, double y) { + x = Math.abs(x); + y = Math.abs(y); + if (y < x) { // Ensuring x <= y + final double a = x; + x = y; + y = a; + } else if (!(y >= x)) { // Testing if we have some NaN + return x == Double.POSITIVE_INFINITY ? Double.POSITIVE_INFINITY : Double.NaN; + } + if (y-x == y) { // x too small to subtract from y + return y; + } else { + double factor; + if (y > HYPOT_MAX_MAG) { // y is too large: scaling down + x *= (1/HYPOT_FACTOR); + y *= (1/HYPOT_FACTOR); + factor = HYPOT_FACTOR; + } else if (x < (1/HYPOT_MAX_MAG)) { // x is too small: scaling up + x *= HYPOT_FACTOR; + y *= HYPOT_FACTOR; + factor = (1/HYPOT_FACTOR); + } else { + factor = 1.0; + } + return factor * Math.sqrt(x*x+y*y); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/destroystokyo/paper/gui/RAMDetails.java b/src/main/java/com/destroystokyo/paper/gui/RAMDetails.java index fac07dda413002c12276131efbe4ee3832c2265a..69fe2c67430991001bdf8c861b94c816981e0a25 100644 --- a/src/main/java/com/destroystokyo/paper/gui/RAMDetails.java +++ b/src/main/java/com/destroystokyo/paper/gui/RAMDetails.java @@ -81,6 +81,6 @@ public class RAMDetails extends JList { } private static String format(double tps) { - return ( ( tps > 21.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 ); + return ( ( tps > 21.0 ) ? "*" : "" ) + Math.min( carpetfixes.helpers.FastMath.round( tps * 100.0 ) / 100.0, 20.0 ); // Mirai } } diff --git a/src/main/java/com/destroystokyo/paper/gui/RAMGraph.java b/src/main/java/com/destroystokyo/paper/gui/RAMGraph.java index c3e54da4ab6440811aab2f9dd1e218802ac13285..db1319c51a5410ee106d023fce759f1e390872e2 100644 --- a/src/main/java/com/destroystokyo/paper/gui/RAMGraph.java +++ b/src/main/java/com/destroystokyo/paper/gui/RAMGraph.java @@ -128,7 +128,7 @@ public class RAMGraph extends JComponent { graphics.setColor(data.getLineColor()); graphics.fillOval(m.x - 2, 100 - used - 2, 5, 5); setToolTipText(String.format("Used: %s mb (%s%%)
%s", - Math.round(data.getUsedMem() / 1024F / 1024F), + carpetfixes.helpers.FastMath.round(data.getUsedMem() / 1024F / 1024F), // Mirai used, getTime(m.x))); } } diff --git a/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java b/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java index b2e4fb69fd6564484e0ebd120ba87431c5c158e4..88a2ade5f467094660978d2d7ad80359cb051d88 100644 --- a/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java +++ b/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java @@ -387,11 +387,11 @@ public final class PlayerChunkLoader { } protected long getTargetSendPerPlayerAddend() { - return GlobalConfiguration.get().chunkLoading.targetPlayerChunkSendRate <= 1.0 ? 0L : (long)Math.round(1.0e9 / GlobalConfiguration.get().chunkLoading.targetPlayerChunkSendRate); + return GlobalConfiguration.get().chunkLoading.targetPlayerChunkSendRate <= 1.0 ? 0L : (long)carpetfixes.helpers.FastMath.round(1.0e9 / GlobalConfiguration.get().chunkLoading.targetPlayerChunkSendRate); // Mirai } protected long getMaxSendAddend() { - return GlobalConfiguration.get().chunkLoading.globalMaxChunkSendRate <= 1.0 ? 0L : (long)Math.round(1.0e9 / GlobalConfiguration.get().chunkLoading.globalMaxChunkSendRate); + return GlobalConfiguration.get().chunkLoading.globalMaxChunkSendRate <= 1.0 ? 0L : (long)carpetfixes.helpers.FastMath.round(1.0e9 / GlobalConfiguration.get().chunkLoading.globalMaxChunkSendRate); // Mirai } public void onChunkPlayerTickReady(final int chunkX, final int chunkZ) { diff --git a/src/main/java/io/papermc/paper/command/subcommands/FixLightCommand.java b/src/main/java/io/papermc/paper/command/subcommands/FixLightCommand.java index 7784d72ddd6db00c674e22759c00c430222c4b85..0f180d11774f6dec2667b3a3f6a63d1127bac869 100644 --- a/src/main/java/io/papermc/paper/command/subcommands/FixLightCommand.java +++ b/src/main/java/io/papermc/paper/command/subcommands/FixLightCommand.java @@ -95,12 +95,12 @@ public final class FixLightCommand implements PaperSubcommand { ++relitChunks[0]; sender.getBukkitEntity().sendMessage(text().color(DARK_AQUA).append( text("Relit chunk ", BLUE), text(chunkPos.toString()), - text(", progress: ", BLUE), text((int) (Math.round(100.0 * (double) (relitChunks[0]) / (double) pending[0])) + "%") + text(", progress: ", BLUE), text((int) (carpetfixes.helpers.FastMath.round(100.0 * (double) (relitChunks[0]) / (double) pending[0])) + "%") // Mirai )); }, (final int totalRelit) -> { final long end = System.nanoTime(); - final long diff = Math.round(1.0e-6 * (end - start)); + final long diff = carpetfixes.helpers.FastMath.round(1.0e-6 * (end - start)); // Mirai sender.getBukkitEntity().sendMessage(text().color(DARK_AQUA).append( text("Relit ", BLUE), text(totalRelit), text(" chunks. Took ", BLUE), text(diff + "ms") diff --git a/src/main/java/net/minecraft/commands/arguments/TimeArgument.java b/src/main/java/net/minecraft/commands/arguments/TimeArgument.java index b2f1d117abd3828140c1edf2baf314b34d2ccf40..d13bafdfa4175e193fa3e337f93e796dd346e678 100644 --- a/src/main/java/net/minecraft/commands/arguments/TimeArgument.java +++ b/src/main/java/net/minecraft/commands/arguments/TimeArgument.java @@ -48,7 +48,7 @@ public class TimeArgument implements ArgumentType { if (i == 0) { throw ERROR_INVALID_UNIT.create(); } else { - int j = Math.round(f * (float)i); + int j = carpetfixes.helpers.FastMath.round(f * (float)i); // Mirai if (j < this.minimum) { throw ERROR_TICK_COUNT_TOO_LOW.create(j, this.minimum); } else { diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 8cd54a95609df0c5497c00d1a254c22d47e80a15..0447aaaa778e9e7f5ceb3d0f3c868a8289fcdc6f 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -2861,7 +2861,7 @@ public abstract class MinecraftServer extends MinecraftServerBlockableEventLoop } double overuseCount = (double)overuse/(double)MAX_CHUNK_EXEC_TIME; - long extraSleep = (long)Math.round(overuseCount*CHUNK_TASK_QUEUE_BACKOFF_MIN_TIME); + long extraSleep = (long)carpetfixes.helpers.FastMath.round(overuseCount*CHUNK_TASK_QUEUE_BACKOFF_MIN_TIME); // Mirai lastMidTickExecute = currTime + extraSleep; return; diff --git a/src/main/java/net/minecraft/server/gui/StatsComponent.java b/src/main/java/net/minecraft/server/gui/StatsComponent.java index 88f10d729aa1e0a01790521821d691a0ecd373a2..45b2aa542969798a5a3b73af78de21ccc57bcca1 100644 --- a/src/main/java/net/minecraft/server/gui/StatsComponent.java +++ b/src/main/java/net/minecraft/server/gui/StatsComponent.java @@ -88,7 +88,7 @@ public class StatsComponent extends JComponent { // Paper - start Add tps entry private static String format(double tps) { - return (( tps > 21.0 ) ? "*" : "") + Math.min(Math.round(tps * 100.0) / 100.0, 20.0); // only print * at 21, we commonly peak to 20.02 as the tick sleep is not accurate enough, stop the noise + return (( tps > 21.0 ) ? "*" : "") + Math.min(carpetfixes.helpers.FastMath.round(tps * 100.0) / 100.0, 20.0); // only print * at 21, we commonly peak to 20.02 as the tick sleep is not accurate enough, stop the noise // Mirai } // Paper end } diff --git a/src/main/java/net/minecraft/util/Mth.java b/src/main/java/net/minecraft/util/Mth.java index a378a2ddd96e76925fa3409282d9606a86c72334..8f6a109675d3ac160c8ee199400cedc20f529e2e 100644 --- a/src/main/java/net/minecraft/util/Mth.java +++ b/src/main/java/net/minecraft/util/Mth.java @@ -9,6 +9,7 @@ import net.minecraft.core.Vec3i; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; import org.apache.commons.lang3.math.NumberUtils; +import org.dreeam.leaf.LeafConfig; public class Mth { private static final long UUID_VERSION = 61440L; @@ -592,6 +593,7 @@ public class Mth { } public static double length(double a, double b) { + if (LeafConfig.optimizedHypot) return carpetfixes.helpers.FastMath.hypot(a, b); // Mirai return Math.sqrt(lengthSquared(a, b)); } diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index a076e8b852a87b67602a21f07048da1570226a81..a947acfee2d45ba29e293b196250d52af874bcc6 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -1501,7 +1501,7 @@ public abstract class LivingEntity extends Entity implements Attackable { if (this instanceof ServerPlayer) { CriteriaTriggers.ENTITY_HURT_PLAYER.trigger((ServerPlayer) this, source, f1, amount, flag); if (f2 > 0.0F && f2 < 3.4028235E37F) { - ((ServerPlayer) this).awardStat(Stats.DAMAGE_BLOCKED_BY_SHIELD, Math.round(f2 * 10.0F)); + ((ServerPlayer) this).awardStat(Stats.DAMAGE_BLOCKED_BY_SHIELD, carpetfixes.helpers.FastMath.round(f2 * 10.0F)); // Mirai } } @@ -2100,9 +2100,9 @@ public abstract class LivingEntity extends Entity implements Attackable { if (f3 > 0.0F && f3 < 3.4028235E37F) { if (this instanceof ServerPlayer) { - ((ServerPlayer) this).awardStat(Stats.DAMAGE_RESISTED, Math.round(f3 * 10.0F)); + ((ServerPlayer) this).awardStat(Stats.DAMAGE_RESISTED, carpetfixes.helpers.FastMath.round(f3 * 10.0F)); // Mirai } else if (source.getEntity() instanceof ServerPlayer) { - ((ServerPlayer) source.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, Math.round(f3 * 10.0F)); + ((ServerPlayer) source.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, carpetfixes.helpers.FastMath.round(f3 * 10.0F)); // Mirai } } } @@ -2214,9 +2214,9 @@ public abstract class LivingEntity extends Entity implements Attackable { float f3 = (float) -event.getDamage(DamageModifier.RESISTANCE); if (f3 > 0.0F && f3 < 3.4028235E37F) { if (this instanceof ServerPlayer) { - ((ServerPlayer) this).awardStat(Stats.DAMAGE_RESISTED, Math.round(f3 * 10.0F)); + ((ServerPlayer) this).awardStat(Stats.DAMAGE_RESISTED, carpetfixes.helpers.FastMath.round(f3 * 10.0F)); // Mirai } else if (damagesource.getEntity() instanceof ServerPlayer) { - ((ServerPlayer) damagesource.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, Math.round(f3 * 10.0F)); + ((ServerPlayer) damagesource.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, carpetfixes.helpers.FastMath.round(f3 * 10.0F)); // Mirai } } } @@ -2248,7 +2248,7 @@ public abstract class LivingEntity extends Entity implements Attackable { float f2 = absorptionModifier; if (f2 > 0.0F && f2 < 3.4028235E37F && this instanceof net.minecraft.world.entity.player.Player) { - ((net.minecraft.world.entity.player.Player) this).awardStat(Stats.DAMAGE_ABSORBED, Math.round(f2 * 10.0F)); + ((net.minecraft.world.entity.player.Player) this).awardStat(Stats.DAMAGE_ABSORBED, carpetfixes.helpers.FastMath.round(f2 * 10.0F)); // Mirai } if (f2 > 0.0F && f2 < 3.4028235E37F) { Entity entity = damagesource.getEntity(); @@ -2256,7 +2256,7 @@ public abstract class LivingEntity extends Entity implements Attackable { if (entity instanceof ServerPlayer) { ServerPlayer entityplayer = (ServerPlayer) entity; - entityplayer.awardStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(f2 * 10.0F)); + entityplayer.awardStat(Stats.DAMAGE_DEALT_ABSORBED, carpetfixes.helpers.FastMath.round(f2 * 10.0F)); // Mirai } } @@ -2265,7 +2265,7 @@ public abstract class LivingEntity extends Entity implements Attackable { // PAIL: Be sure to drag all this code from the EntityHuman subclass each update. ((net.minecraft.world.entity.player.Player) this).causeFoodExhaustion(damagesource.getFoodExhaustion(), org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.DAMAGED); // CraftBukkit - EntityExhaustionEvent if (f < 3.4028235E37F) { - ((net.minecraft.world.entity.player.Player) this).awardStat(Stats.DAMAGE_TAKEN, Math.round(f * 10.0F)); + ((net.minecraft.world.entity.player.Player) this).awardStat(Stats.DAMAGE_TAKEN, carpetfixes.helpers.FastMath.round(f * 10.0F)); // Mirai } } // CraftBukkit end @@ -2287,7 +2287,7 @@ public abstract class LivingEntity extends Entity implements Attackable { CriteriaTriggers.ENTITY_HURT_PLAYER.trigger((ServerPlayer) this, damagesource, originalDamage, f, true); // Paper - fix taken/dealt param order f2 = (float) -event.getDamage(DamageModifier.BLOCKING); if (f2 > 0.0F && f2 < 3.4028235E37F) { - ((ServerPlayer) this).awardStat(Stats.DAMAGE_BLOCKED_BY_SHIELD, Math.round(originalDamage * 10.0F)); + ((ServerPlayer) this).awardStat(Stats.DAMAGE_BLOCKED_BY_SHIELD, carpetfixes.helpers.FastMath.round(originalDamage * 10.0F)); // Mirai } } @@ -3054,13 +3054,13 @@ public abstract class LivingEntity extends Entity implements Attackable { f2 = this.tickHeadTurn(f1, f2); // Paper start - stop large pitch and yaw changes from crashing the server - this.yRotO += Math.round((this.getYRot() - this.yRotO) / 360.0F) * 360.0F; + this.yRotO += carpetfixes.helpers.FastMath.round((this.getYRot() - this.yRotO) / 360.0F) * 360.0F; // Mirai - this.yBodyRotO += Math.round((this.yBodyRot - this.yBodyRotO) / 360.0F) * 360.0F; + this.yBodyRotO += carpetfixes.helpers.FastMath.round((this.yBodyRot - this.yBodyRotO) / 360.0F) * 360.0F; // Mirai - this.xRotO += Math.round((this.getXRot() - this.xRotO) / 360.0F) * 360.0F; + this.xRotO += carpetfixes.helpers.FastMath.round((this.getXRot() - this.xRotO) / 360.0F) * 360.0F; // Mirai - this.yHeadRotO += Math.round((this.yHeadRot - this.yHeadRotO) / 360.0F) * 360.0F; + this.yHeadRotO += carpetfixes.helpers.FastMath.round((this.yHeadRot - this.yHeadRotO) / 360.0F) * 360.0F; // Mirai // Paper end this.animStep += f2; diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java index 7b0dbfaab5951aaf09c36bc5112d953585f0098a..8814e1320fa716184a7f8ae930717583184b5408 100644 --- a/src/main/java/net/minecraft/world/entity/player/Player.java +++ b/src/main/java/net/minecraft/world/entity/player/Player.java @@ -1072,7 +1072,7 @@ public abstract class Player extends LivingEntity { float f2 = f1 - f; if (f2 > 0.0F && f2 < 3.4028235E37F) { - this.awardStat(Stats.DAMAGE_ABSORBED, Math.round(f2 * 10.0F)); + this.awardStat(Stats.DAMAGE_ABSORBED, carpetfixes.helpers.FastMath.round(f2 * 10.0F)); // Mirai } if (f != 0.0F) { @@ -1082,7 +1082,7 @@ public abstract class Player extends LivingEntity { this.getCombatTracker().recordDamage(damagesource, f3, f); this.setHealth(this.getHealth() - f); if (f < 3.4028235E37F) { - this.awardStat(Stats.DAMAGE_TAKEN, Math.round(f * 10.0F)); + this.awardStat(Stats.DAMAGE_TAKEN, carpetfixes.helpers.FastMath.round(f * 10.0F)); // Mirai } } @@ -1422,7 +1422,7 @@ public abstract class Player extends LivingEntity { if (target instanceof LivingEntity) { float f5 = f3 - ((LivingEntity) target).getHealth(); - this.awardStat(Stats.DAMAGE_DEALT, Math.round(f5 * 10.0F)); + this.awardStat(Stats.DAMAGE_DEALT, carpetfixes.helpers.FastMath.round(f5 * 10.0F)); // Mirai if (j > 0) { // CraftBukkit start - Call a combust event when somebody hits with a fire enchanted item EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(this.getBukkitEntity(), target.getBukkitEntity(), j * 4); @@ -1686,29 +1686,29 @@ public abstract class Player extends LivingEntity { int i; if (this.isSwimming()) { - i = Math.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); // Mirai if (i > 0) { this.awardStat(Stats.SWIM_ONE_CM, i); this.causeFoodExhaustion(level.spigotConfig.swimMultiplier * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.SWIM); // CraftBukkit - EntityExhaustionEvent // Spigot } } else if (this.isEyeInFluid(FluidTags.WATER)) { - i = Math.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); // Mirai if (i > 0) { this.awardStat(Stats.WALK_UNDER_WATER_ONE_CM, i); this.causeFoodExhaustion(level.spigotConfig.swimMultiplier * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.WALK_UNDERWATER); // CraftBukkit - EntityExhaustionEvent // Spigot } } else if (this.isInWater()) { - i = Math.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); // Mirai if (i > 0) { this.awardStat(Stats.WALK_ON_WATER_ONE_CM, i); this.causeFoodExhaustion(level.spigotConfig.swimMultiplier * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.WALK_ON_WATER); // CraftBukkit - EntityExhaustionEvent // Spigot } } else if (this.onClimbable()) { if (dy > 0.0D) { - this.awardStat(Stats.CLIMB_ONE_CM, (int) Math.round(dy * 100.0D)); + this.awardStat(Stats.CLIMB_ONE_CM, (int) carpetfixes.helpers.FastMath.round(dy * 100.0D)); // Mirai } } else if (this.onGround) { - i = Math.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); // Mirai if (i > 0) { if (this.isSprinting()) { this.awardStat(Stats.SPRINT_ONE_CM, i); @@ -1722,10 +1722,10 @@ public abstract class Player extends LivingEntity { } } } else if (this.isFallFlying()) { - i = Math.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); // Mirai this.awardStat(Stats.AVIATE_ONE_CM, i); } else { - i = Math.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); + i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dz * dz) * 100.0F); // Mirai if (i > 25) { this.awardStat(Stats.FLY_ONE_CM, i); } @@ -1736,7 +1736,7 @@ public abstract class Player extends LivingEntity { private void checkRidingStatistics(double dx, double dy, double dz) { if (this.isPassenger()) { - int i = Math.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); + int i = carpetfixes.helpers.FastMath.round((float) Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); // Mirai if (i > 0) { Entity entity = this.getVehicle(); @@ -1763,7 +1763,7 @@ public abstract class Player extends LivingEntity { return false; } else { if (fallDistance >= 2.0F) { - this.awardStat(Stats.FALL_ONE_CM, (int) Math.round((double) fallDistance * 100.0D)); + this.awardStat(Stats.FALL_ONE_CM, (int) carpetfixes.helpers.FastMath.round((double) fallDistance * 100.0D)); // Mirai } return super.causeFallDamage(fallDistance, damageMultiplier, damageSource); diff --git a/src/main/java/net/minecraft/world/item/Item.java b/src/main/java/net/minecraft/world/item/Item.java index dd2f6c67533cc3344a171fe3ae9b3704796d8c50..78e641cf4f5857712f60b168be803f311e7def10 100644 --- a/src/main/java/net/minecraft/world/item/Item.java +++ b/src/main/java/net/minecraft/world/item/Item.java @@ -169,7 +169,7 @@ public class Item implements FeatureElement, ItemLike { } public int getBarWidth(ItemStack stack) { - return Math.round(13.0F - (float)stack.getDamageValue() * 13.0F / (float)this.maxDamage); + return carpetfixes.helpers.FastMath.round(13.0F - (float)stack.getDamageValue() * 13.0F / (float)this.maxDamage); // Mirai } public int getBarColor(ItemStack stack) { diff --git a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java index ecf640b00007a386290f8dfe9935a8aa610079fd..8b199643eda44a6f020768af70e85175fbe2be5d 100644 --- a/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java +++ b/src/main/java/net/minecraft/world/item/enchantment/EnchantmentHelper.java @@ -350,7 +350,7 @@ public class EnchantmentHelper { } else { level += 1 + random.nextInt(i / 4 + 1) + random.nextInt(i / 4 + 1); float f = (random.nextFloat() + random.nextFloat() - 1.0F) * 0.15F; - level = Mth.clamp(Math.round((float)level + (float)level * f), 1, Integer.MAX_VALUE); + level = Mth.clamp(carpetfixes.helpers.FastMath.round((float)level + (float)level * f), 1, Integer.MAX_VALUE); // Mirai List list2 = getAvailableEnchantmentResults(level, stack, treasureAllowed); if (!list2.isEmpty()) { WeightedRandom.getRandomItem(random, list2).ifPresent(list::add); diff --git a/src/main/java/net/minecraft/world/level/block/DaylightDetectorBlock.java b/src/main/java/net/minecraft/world/level/block/DaylightDetectorBlock.java index 81376e725151f723dad8a7b5c1a4bd597e60294e..5a33c98d29e1a76e5239dab166514ab95779ddc6 100644 --- a/src/main/java/net/minecraft/world/level/block/DaylightDetectorBlock.java +++ b/src/main/java/net/minecraft/world/level/block/DaylightDetectorBlock.java @@ -62,7 +62,7 @@ public class DaylightDetectorBlock extends BaseEntityBlock { float f1 = f < 3.1415927F ? 0.0F : 6.2831855F; f += (f1 - f) * 0.2F; - i = Math.round((float) i * Mth.cos(f)); + i = carpetfixes.helpers.FastMath.round((float) i * Mth.cos(f)); // Mirai } i = Mth.clamp(i, 0, 15); diff --git a/src/main/java/net/minecraft/world/level/levelgen/SurfaceSystem.java b/src/main/java/net/minecraft/world/level/levelgen/SurfaceSystem.java index 3152d995f87beca1e0243a7d594d352ec458b0ed..d4dbee946d292e0d01910cc4fd1c67cda8fd7dc8 100644 --- a/src/main/java/net/minecraft/world/level/levelgen/SurfaceSystem.java +++ b/src/main/java/net/minecraft/world/level/levelgen/SurfaceSystem.java @@ -301,7 +301,7 @@ public class SurfaceSystem { } protected BlockState getBand(int x, int y, int z) { - int i = (int)Math.round(this.clayBandsOffsetNoise.getValue((double)x, 0.0D, (double)z) * 4.0D); + int i = (int)carpetfixes.helpers.FastMath.round(this.clayBandsOffsetNoise.getValue((double)x, 0.0D, (double)z) * 4.0D); // Mirai return this.clayBands[(y + i + this.clayBands.length) % this.clayBands.length]; } } diff --git a/src/main/java/net/minecraft/world/level/levelgen/feature/ScatteredOreFeature.java b/src/main/java/net/minecraft/world/level/levelgen/feature/ScatteredOreFeature.java index 06f27fc8eda9ec160c54759ec95fdade19876d29..567d653469c964922dc5662e48c45dc289222bc2 100644 --- a/src/main/java/net/minecraft/world/level/levelgen/feature/ScatteredOreFeature.java +++ b/src/main/java/net/minecraft/world/level/levelgen/feature/ScatteredOreFeature.java @@ -46,6 +46,6 @@ public class ScatteredOreFeature extends Feature { } private int getRandomPlacementInOneAxisRelativeToOrigin(RandomSource randomSource, int spread) { - return Math.round((randomSource.nextFloat() - randomSource.nextFloat()) * (float)spread); + return carpetfixes.helpers.FastMath.round((randomSource.nextFloat() - randomSource.nextFloat()) * (float)spread); // Mirai } } diff --git a/src/main/java/net/minecraft/world/level/storage/loot/functions/LootingEnchantFunction.java b/src/main/java/net/minecraft/world/level/storage/loot/functions/LootingEnchantFunction.java index 31918fa2eb38e42a5ea5366e559f25ea9d7d59ae..fe0bcd4295e96556d1c5282d6acc426980697675 100644 --- a/src/main/java/net/minecraft/world/level/storage/loot/functions/LootingEnchantFunction.java +++ b/src/main/java/net/minecraft/world/level/storage/loot/functions/LootingEnchantFunction.java @@ -61,7 +61,7 @@ public class LootingEnchantFunction extends LootItemConditionalFunction { float f = (float) i * this.value.getFloat(context); - stack.grow(Math.round(f)); + stack.grow(carpetfixes.helpers.FastMath.round(f)); // Mirai if (this.hasLimit() && stack.getCount() > this.limit) { stack.setCount(this.limit); } diff --git a/src/main/java/net/minecraft/world/level/storage/loot/providers/number/NumberProvider.java b/src/main/java/net/minecraft/world/level/storage/loot/providers/number/NumberProvider.java index 2a7c75ea447b179ea1ab9db56e8a39d03faa0bce..53ca87c7659a4fff9e99d21217d1ad08c8765836 100644 --- a/src/main/java/net/minecraft/world/level/storage/loot/providers/number/NumberProvider.java +++ b/src/main/java/net/minecraft/world/level/storage/loot/providers/number/NumberProvider.java @@ -7,7 +7,7 @@ public interface NumberProvider extends LootContextUser { float getFloat(LootContext context); default int getInt(LootContext context) { - return Math.round(this.getFloat(context)); + return carpetfixes.helpers.FastMath.round(this.getFloat(context)); // Mirai } LootNumberProviderType getType(); diff --git a/src/main/java/net/minecraft/world/phys/shapes/Shapes.java b/src/main/java/net/minecraft/world/phys/shapes/Shapes.java index 731c7dd15f131dc124be6af8f342b122cb89491b..24a82111a5485da1e3903977d0519e9ed502f34d 100644 --- a/src/main/java/net/minecraft/world/phys/shapes/Shapes.java +++ b/src/main/java/net/minecraft/world/phys/shapes/Shapes.java @@ -59,8 +59,8 @@ public final class Shapes { int j = 1 << i; double d = min * (double)j; double e = max * (double)j; - boolean bl = Math.abs(d - (double)Math.round(d)) < 1.0E-7D * (double)j; - boolean bl2 = Math.abs(e - (double)Math.round(e)) < 1.0E-7D * (double)j; + boolean bl = Math.abs(d - (double)carpetfixes.helpers.FastMath.round(d)) < 1.0E-7D * (double)j; // Mirai + boolean bl2 = Math.abs(e - (double)carpetfixes.helpers.FastMath.round(e)) < 1.0E-7D * (double)j; // Mirai if (bl && bl2) { return i; } diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java index 918631209956adae59b83dd28e7492290362311e..607b02f77c76760f607ce763ea2ea17e48323c0e 100644 --- a/src/main/java/org/dreeam/leaf/LeafConfig.java +++ b/src/main/java/org/dreeam/leaf/LeafConfig.java @@ -188,12 +188,18 @@ public class LeafConfig { public static boolean villagerLobotomizeEnabled = false; public static int villagerLobotomizeCheckInterval = 100; public static boolean useSpigotItemMergingMechanism = true; + public static boolean optimizedRound = true; + public static boolean optimizedHypot = true; private static void performance() { laggingThreshold = getDouble("performance.lagging-threshold", laggingThreshold); tpsCatchup = getBoolean("performance.tps-catchup", tpsCatchup); villagerLobotomizeEnabled = getBoolean("performance.villager.lobotomize.enabled", villagerLobotomizeEnabled); villagerLobotomizeCheckInterval = getInt("performance.villager.lobotomize.check-interval", villagerLobotomizeCheckInterval); useSpigotItemMergingMechanism = getBoolean("performance.use-spigot-item-merging-mechanism", useSpigotItemMergingMechanism); + optimizedRound = getBoolean("performance.optimize-math-round", optimizedRound, + "Whether or not Math.round should be replaced by a faster version."); + optimizedHypot = getBoolean("performance.optimize-math-hypot", optimizedHypot, + "Whether or not Math.hypot should be replaced by a faster version."); } public static String commandTPSBarOutput = "Tpsbar toggled for "; diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java index b42b31cd33232a99befd8334bc24b8ae8b021ccb..b339fd4b3b4bc48caa10d2707071b08e284b1992 100644 --- a/src/main/java/org/spigotmc/TicksPerSecondCommand.java +++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java @@ -69,7 +69,7 @@ public class TicksPerSecondCommand extends Command private static String format(double tps) // Paper - Made static { return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString() - + ( ( tps > 21.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 ); // Paper - only print * at 21, we commonly peak to 20.02 as the tick sleep is not accurate enough, stop the noise + + ( ( tps > 21.0 ) ? "*" : "" ) + Math.min( carpetfixes.helpers.FastMath.round( tps * 100.0 ) / 100.0, 20.0 ); // Paper - only print * at 21, we commonly peak to 20.02 as the tick sleep is not accurate enough, stop the noise // Mirai } // Gale start - YAPFA - last tick time - in TPS command