Skip to content

Commit 4747761

Browse files
authored
Merge pull request #42 from HaHaWTH/master
Optimize AABB intersect
2 parents 4a5dbe6 + 71c2fe7 commit 4747761

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

src/main/java/github/kasuminova/stellarcore/common/config/category/Performance.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public static class Vanilla {
193193
@Config.Name("PropertyEnumHashCodeCache")
194194
public boolean propertyEnumHashCodeCache = true;
195195

196+
@Config.Comment("(Client/Server Performance) Optimize BoundingBox intersect check.")
197+
@Config.RequiresMcRestart
198+
@Config.Name("BoundingBoxIntersectCheck")
199+
public boolean boundingBoxIntersectCheck = true;
200+
196201
@Config.Comment("(Server Performance) Improving BlockStateContainer$BlockStateImplementation#hashCode Performance with hashCode cache.")
197202
@Config.RequiresMcRestart
198203
@Config.Name("BlockStateImplementationHashCodeCache")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package github.kasuminova.stellarcore.common.util;
2+
3+
@SuppressWarnings("unused")
4+
public class RuntimeEnv {
5+
private RuntimeEnv() {
6+
}
7+
8+
public static final boolean IS_CLEANROOM_LOADER = isClassInPath("com.cleanroommc.common.CleanroomContainer");
9+
10+
public static boolean isClassInPath(String className) {
11+
String classPath = className.replace('.', '/') + ".class";
12+
try {
13+
return Thread.currentThread().getContextClassLoader().getResource(classPath) != null;
14+
} catch (Throwable e) {
15+
return false;
16+
}
17+
}
18+
19+
public static boolean isClassLoaded(String className) {
20+
try {
21+
Class.forName(className);
22+
return true;
23+
} catch (Throwable e) {
24+
return false;
25+
}
26+
}
27+
}

src/main/java/github/kasuminova/stellarcore/mixin/StellarCoreEarlyMixinLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import github.kasuminova.stellarcore.common.config.StellarCoreConfig;
44
import github.kasuminova.stellarcore.common.integration.censoredasm.CensoredASMCompat;
55
import github.kasuminova.stellarcore.common.mod.Mods;
6+
import github.kasuminova.stellarcore.common.util.RuntimeEnv;
67
import github.kasuminova.stellarcore.common.util.StellarEnvironment;
78
import github.kasuminova.stellarcore.common.util.StellarLog;
89
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
@@ -41,6 +42,7 @@ public class StellarCoreEarlyMixinLoader implements IFMLLoadingPlugin {
4142
addMixinCFG("mixins.stellar_core_minecraft_nnlist.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.nonNullList);
4243
addMixinCFG("mixins.stellar_core_minecraft_noglerror.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.noGlError);
4344
addMixinCFG("mixins.stellar_core_minecraft_property.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.propertyEnumHashCodeCache);
45+
addMixinCFG("mixins.stellar_core_minecraft_phys.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.boundingBoxIntersectCheck && !RuntimeEnv.IS_CLEANROOM_LOADER);
4446
addMixinCFG("mixins.stellar_core_minecraft_randomtick.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.parallelRandomBlockTicker);
4547
addMixinCFG("mixins.stellar_core_minecraft_renderglobal.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.alwaysDeferChunkUpdates);
4648
addMixinCFG("mixins.stellar_core_minecraft_resourcelocation.json", () -> StellarCoreConfig.PERFORMANCE.vanilla.resourceLocationCanonicalization && !StellarCoreConfig.PERFORMANCE.vanilla.resourceLocationCanonicalizationAsync);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package github.kasuminova.stellarcore.mixin.minecraft.phys;
2+
3+
import net.minecraft.util.math.AxisAlignedBB;
4+
import org.spongepowered.asm.mixin.Final;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Overwrite;
7+
import org.spongepowered.asm.mixin.Shadow;
8+
9+
@Mixin(value = AxisAlignedBB.class, priority = 999)
10+
public abstract class MixinAxisAlignedBB {
11+
@Shadow @Final public double minX;
12+
13+
@Shadow @Final public double minY;
14+
15+
@Shadow @Final public double minZ;
16+
17+
@Shadow @Final public double maxX;
18+
19+
@Shadow @Final public double maxY;
20+
21+
@Shadow @Final public double maxZ;
22+
23+
/**
24+
* @author Creeam
25+
* @reason Optimize AABB
26+
*/
27+
@Overwrite
28+
public AxisAlignedBB intersect(AxisAlignedBB other) {
29+
return new AxisAlignedBB(
30+
this.minX > other.minX ? this.minX : other.minX,
31+
this.minY > other.minY ? this.minY : other.minY,
32+
this.minZ > other.minZ ? this.minZ : other.minZ,
33+
this.maxX < other.maxX ? this.maxX : other.maxX,
34+
this.maxY < other.maxY ? this.maxY : other.maxY,
35+
this.maxZ < other.maxZ ? this.maxZ : other.maxZ
36+
);
37+
}
38+
39+
/**
40+
* @author Creeam
41+
* @reason Direct comparison, avoid method call
42+
*/
43+
@Overwrite
44+
public boolean intersects(AxisAlignedBB other) {
45+
return this.minX < other.maxX &&
46+
this.maxX > other.minX &&
47+
this.minY < other.maxY &&
48+
this.maxY > other.minY &&
49+
this.minZ < other.maxZ &&
50+
this.maxZ > other.minZ;
51+
}
52+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"package": "github.kasuminova.stellarcore.mixin.minecraft.phys",
3+
"refmap": "mixins.stellar_core.refmap.json",
4+
"target": "@env(DEFAULT)",
5+
"minVersion": "0.8",
6+
"compatibilityLevel": "JAVA_8",
7+
"mixins": [
8+
"MixinAxisAlignedBB"
9+
]
10+
}

0 commit comments

Comments
 (0)