faster methods
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.math.fast_util;
|
||||
|
||||
import net.minecraft.core.Direction;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
|
||||
/**
|
||||
* The JVM has difficulty optimizing these functions due to the use of dynamic dispatch. They can trivially be
|
||||
* implemented as a simple switch lookup table. Switch-on-enum is avoided due to issues in Mixin hotswap.
|
||||
*/
|
||||
public class AxisCycleDirectionMixin {
|
||||
static {
|
||||
assert Direction.Axis.X.ordinal() == 0;
|
||||
assert Direction.Axis.Y.ordinal() == 1;
|
||||
assert Direction.Axis.Z.ordinal() == 2;
|
||||
assert Direction.Axis.values().length == 3;
|
||||
}
|
||||
|
||||
@Mixin(targets = "net/minecraft/core/AxisCycle$2")
|
||||
public static class ForwardMixin {
|
||||
/**
|
||||
* @reason Avoid expensive array/modulo operations
|
||||
* @author JellySquid
|
||||
*/
|
||||
@Overwrite
|
||||
public Direction.Axis cycle(Direction.Axis axis) {
|
||||
switch (axis.ordinal()) {
|
||||
case 0: //X
|
||||
return Direction.Axis.Y;
|
||||
case 1: //Y
|
||||
return Direction.Axis.Z;
|
||||
case 2: //Z
|
||||
return Direction.Axis.X;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
@Mixin(targets = "net/minecraft/core/AxisCycle$3")
|
||||
public static class BackwardMixin {
|
||||
/**
|
||||
* @reason Avoid expensive array/modulo operations
|
||||
* @author JellySquid
|
||||
*/
|
||||
@Overwrite
|
||||
public Direction.Axis cycle(Direction.Axis axis) {
|
||||
switch (axis.ordinal()) {
|
||||
case 0: //X
|
||||
return Direction.Axis.Z;
|
||||
case 1: //Y
|
||||
return Direction.Axis.X;
|
||||
case 2: //Z
|
||||
return Direction.Axis.Y;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.math.fast_util;
|
||||
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.core.Direction;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
@Mixin(AABB.class)
|
||||
public class BoxMixin {
|
||||
static {
|
||||
assert Direction.Axis.X.ordinal() == 0;
|
||||
assert Direction.Axis.Y.ordinal() == 1;
|
||||
assert Direction.Axis.Z.ordinal() == 2;
|
||||
assert Direction.Axis.values().length == 3;
|
||||
}
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double minX;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double minY;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double minZ;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double maxX;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double maxY;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
public double maxZ;
|
||||
|
||||
/**
|
||||
* @reason Simplify the code to better help the JVM optimize it
|
||||
* @author JellySquid
|
||||
*/
|
||||
@Overwrite
|
||||
public double min(Direction.Axis axis) {
|
||||
switch (axis.ordinal()) {
|
||||
case 0: //X
|
||||
return this.minX;
|
||||
case 1: //Y
|
||||
return this.minY;
|
||||
case 2: //Z
|
||||
return this.minZ;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @reason Simplify the code to better help the JVM optimize it
|
||||
* @author JellySquid
|
||||
*/
|
||||
@Overwrite
|
||||
public double max(Direction.Axis axis) {
|
||||
switch (axis.ordinal()) {
|
||||
case 0: //X
|
||||
return this.maxX;
|
||||
case 1: //Y
|
||||
return this.maxY;
|
||||
case 2: //Z
|
||||
return this.maxZ;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//package net.gensokyoreimagined.nitori.mixin.math.fast_util;
|
||||
//
|
||||
//import net.minecraft.core.Direction;
|
||||
//import net.minecraft.core.Direction.Axis;
|
||||
//import net.minecraft.util.RandomSource;
|
||||
//import org.spongepowered.asm.mixin.Final;
|
||||
//import org.spongepowered.asm.mixin.Mixin;
|
||||
//import org.spongepowered.asm.mixin.Overwrite;
|
||||
//import org.spongepowered.asm.mixin.Shadow;
|
||||
//
|
||||
//@Mixin(Direction.class)
|
||||
//public class DirectionMixin {
|
||||
// @Shadow
|
||||
// @Final
|
||||
// private static Direction[] ALL;
|
||||
//
|
||||
// @Shadow
|
||||
// @Final
|
||||
// private int idOpposite;
|
||||
//
|
||||
// /**
|
||||
// * @reason Avoid the modulo/abs operations
|
||||
// * @author JellySquid
|
||||
// */
|
||||
// @Overwrite
|
||||
// public Direction getOpposite() {
|
||||
// return ALL[this.idOpposite];
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @reason Do not allocate an excessive number of Direction arrays
|
||||
// * @author JellySquid
|
||||
// */
|
||||
// @Overwrite
|
||||
// public static Direction getRandom(RandomSource rand) {
|
||||
// return Direction.ALL[rand.nextInt(ALL.length)];
|
||||
// }
|
||||
//}
|
||||
@@ -37,6 +37,9 @@
|
||||
"shapes.blockstate_cache.BlockMixin",
|
||||
"world.inline_height.WorldChunkMixin",
|
||||
"math.fast_blockops.DirectionMixin",
|
||||
"math.fast_blockops.BlockPosMixin"
|
||||
"math.fast_blockops.BlockPosMixin",
|
||||
"math.fast_util.AxisCycleDirectionMixin.BackwardMixin",
|
||||
"math.fast_util.AxisCycleDirectionMixin.ForwardMixin",
|
||||
"math.fast_util.BoxMixin"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user