From 178f8b28395c21fd8ba82cc3e7d392323badc084 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Fri, 16 Feb 2024 16:02:31 -0500 Subject: [PATCH] Memory Allocation Improvements (#11) * Get rid of egregious Parameter overallocation * Allocate Point instances conditionally by using mutable Vector3d for calculations * Update minimum GTNHLib version for JOML --- .../java/net/malisis/core/MalisisCore.java | 2 +- .../core/renderer/MalisisRenderer.java | 10 ++- .../net/malisis/core/renderer/Parameter.java | 4 +- .../java/net/malisis/core/util/Point.java | 8 ++ src/main/java/net/malisis/core/util/Ray.java | 76 ++++++++++++++----- 5 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/malisis/core/MalisisCore.java b/src/main/java/net/malisis/core/MalisisCore.java index 81d8173..242730a 100644 --- a/src/main/java/net/malisis/core/MalisisCore.java +++ b/src/main/java/net/malisis/core/MalisisCore.java @@ -58,7 +58,7 @@ modid = MalisisCore.modid, name = MalisisCore.modname, version = MalisisCore.version, - dependencies = "required-after:gtnhlib@[0.0.10,)") + dependencies = "required-after:gtnhlib@[0.2.4,)") public class MalisisCore implements IMalisisMod { /** Mod ID. */ diff --git a/src/main/java/net/malisis/core/renderer/MalisisRenderer.java b/src/main/java/net/malisis/core/renderer/MalisisRenderer.java index 99d35d8..5e20dd2 100644 --- a/src/main/java/net/malisis/core/renderer/MalisisRenderer.java +++ b/src/main/java/net/malisis/core/renderer/MalisisRenderer.java @@ -112,7 +112,7 @@ public class MalisisRenderer extends TileEntitySpecialRenderer /** Current parameters for the shape being rendered. */ protected RenderParameters rp = new RenderParameters(); /** Current parameters for the face being rendered. */ - protected RenderParameters params; + protected static RenderParameters params = new RenderParameters(); /** Base brightness of the block. */ protected int baseBrightness; /** An override texture set by the renderer. */ @@ -676,7 +676,11 @@ public void drawShape(Shape s, RenderParameters params) { if (s == null) return; shape = s; - rp = params != null ? params : new RenderParameters(); + if (params != null) { + rp = params; + } else { + rp.reset(); + } // apply transformations s.applyMatrix(); @@ -716,7 +720,7 @@ protected void drawFace(Face f, RenderParameters faceParams) { } face = f; - params = new RenderParameters(); + params.reset(); params.merge(rp); params.merge(faceParams); diff --git a/src/main/java/net/malisis/core/renderer/Parameter.java b/src/main/java/net/malisis/core/renderer/Parameter.java index c8ce5d5..daa6053 100644 --- a/src/main/java/net/malisis/core/renderer/Parameter.java +++ b/src/main/java/net/malisis/core/renderer/Parameter.java @@ -23,7 +23,7 @@ public class Parameter { /** Default value. */ private T defaultValue; - /** Current alue. */ + /** Current value. */ private T value; /** @@ -57,7 +57,7 @@ public T getValue() { * Resets the value to its default. */ public void reset() { - value = null; + value = defaultValue; } /** diff --git a/src/main/java/net/malisis/core/util/Point.java b/src/main/java/net/malisis/core/util/Point.java index 7dd8aaa..9816c2e 100644 --- a/src/main/java/net/malisis/core/util/Point.java +++ b/src/main/java/net/malisis/core/util/Point.java @@ -16,6 +16,8 @@ import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.Vec3; +import org.joml.Vector3d; + /** * * @author Ordinastie @@ -45,6 +47,12 @@ public Point(double x, double y, double z) { this.z = z; } + public Point(Vector3d vector) { + this.x = vector.x; + this.y = vector.y; + this.z = vector.z; + } + /** * Instantiates a new point. * diff --git a/src/main/java/net/malisis/core/util/Ray.java b/src/main/java/net/malisis/core/util/Ray.java index 838bdeb..239a613 100644 --- a/src/main/java/net/malisis/core/util/Ray.java +++ b/src/main/java/net/malisis/core/util/Ray.java @@ -21,6 +21,7 @@ import net.minecraftforge.common.util.ForgeDirection; import org.apache.commons.lang3.tuple.Pair; +import org.joml.Vector3d; /** * @@ -67,14 +68,14 @@ public Ray(Vec3 src, Vec3 dest) { } /** - * Gets the {@link Point} at the specified distance. + * Gets the point at the specified distance into a {@link Vector3d}. * - * @param t the distance - * @return the point at the distance t + * @param vector {@link Vector3d} to store the result in + * @param t the distance */ - public Point getPointAt(double t) { - if (Double.isNaN(t)) return null; - return new Point(origin.x + t * direction.x, origin.y + t * direction.y, origin.z + t * direction.z); + public void getPointAt(Vector3d vector, double t) { + if (Double.isNaN(t)) return; + vector.set(origin.x + t * direction.x, origin.y + t * direction.y, origin.z + t * direction.z); } /** @@ -123,23 +124,60 @@ public List> intersect(AxisAlignedBB aabb) { double iY = intersectY(aabb.maxY); double iz = intersectZ(aabb.minZ); double iZ = intersectZ(aabb.maxZ); - Point interx = ix >= 0 ? getPointAt(ix) : null; - Point interX = iX >= 0 ? getPointAt(iX) : null; - Point intery = iy >= 0 ? getPointAt(iy) : null; - Point interY = iY >= 0 ? getPointAt(iY) : null; - Point interz = iz >= 0 ? getPointAt(iz) : null; - Point interZ = iZ >= 0 ? getPointAt(iZ) : null; List> list = new ArrayList<>(); - if (interx != null && interx.isInside(aabb)) list.add(Pair.of(ForgeDirection.WEST, interx)); - if (interX != null && interX.isInside(aabb)) list.add(Pair.of(ForgeDirection.EAST, interX)); - if (intery != null && intery.isInside(aabb)) list.add(Pair.of(ForgeDirection.DOWN, intery)); - if (interY != null && interY.isInside(aabb)) list.add(Pair.of(ForgeDirection.UP, interY)); - - if (interz != null && interz.isInside(aabb)) list.add(Pair.of(ForgeDirection.NORTH, interz)); - if (interZ != null && interZ.isInside(aabb)) list.add(Pair.of(ForgeDirection.SOUTH, interZ)); + Vector3d intersector = new Vector3d(); + if (ix >= 0) { + getPointAt(intersector, ix); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.WEST, new Point(intersector))); + } + } + + if (iX >= 0) { + getPointAt(intersector, iX); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.EAST, new Point(intersector))); + } + } + + if (iy >= 0) { + getPointAt(intersector, iy); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.DOWN, new Point(intersector))); + } + } + + if (iY >= 0) { + getPointAt(intersector, iY); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.UP, new Point(intersector))); + } + } + + if (iz >= 0) { + getPointAt(intersector, iz); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.NORTH, new Point(intersector))); + } + } + + if (iZ >= 0) { + getPointAt(intersector, iZ); + if (isPointInsideAABB(intersector, aabb)) { + list.add(Pair.of(ForgeDirection.SOUTH, new Point(intersector))); + } + } return list; } + + private boolean isPointInsideAABB(Vector3d point, AxisAlignedBB aabb) { + return point.x >= aabb.minX && point.x <= aabb.maxX + && point.y >= aabb.minY + && point.y <= aabb.maxY + && point.z >= aabb.minZ + && point.z <= aabb.maxZ; + } }