From d7d7680f53e06a131b80183a06a0179a11f89570 Mon Sep 17 00:00:00 2001 From: KosmX Date: Fri, 31 Jul 2026 20:31:19 +0000 Subject: [PATCH] Un-memory-unoptimize Avoid using "globals to avoid allocation", this way JIT can do proper escape analysis, do proper optimizations (and make the function thread-safe too) --- .../api/block/BlockSubLevelLiftProvider.java | 77 ++++++++----------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/dev/ryanhcode/sable/api/block/BlockSubLevelLiftProvider.java b/common/src/main/java/dev/ryanhcode/sable/api/block/BlockSubLevelLiftProvider.java index 0233bb3b..a6019247 100644 --- a/common/src/main/java/dev/ryanhcode/sable/api/block/BlockSubLevelLiftProvider.java +++ b/common/src/main/java/dev/ryanhcode/sable/api/block/BlockSubLevelLiftProvider.java @@ -23,26 +23,6 @@ public interface BlockSubLevelLiftProvider { Direction[] DIRECTIONS = Direction.values(); - // memory optimization - Vector3d LIFT_FORCE = new Vector3d(); - Vector3d LIFT_POS = new Vector3d(); - Vector3d LIFT_NORMAL = new Vector3d(); - - Vector3d LIFT_VELO = new Vector3d(); - Vector3d DRAG = new Vector3d(); - Vector3d TEMP = new Vector3d(); - - /** - * Resets the vectors to their identity. - */ - static void resetVectors() { - LIFT_VELO.set(0, 0, 0); - LIFT_POS.set(0, 0, 0); - LIFT_FORCE.set(0, 0, 0); - LIFT_NORMAL.set(0, 0, 0); - DRAG.set(0, 0, 0); - } - static List groupLiftProviders(final Collection liftProviders) { final List groups = new ObjectArrayList<>(); final Set positions = new ObjectOpenHashSet<>(liftProviders.size()); @@ -133,52 +113,56 @@ static List groupLiftProviders(final Collection 0) { // DRAG = NORMAL * (NORMAL dot VELO) // FORCE = DRAG * scalars - final double dragStrength = LIFT_NORMAL.dot(LIFT_VELO) * this.sable$getParallelDragScalar() * pressure * timeStep; - final Vector3d parallelDrag = LIFT_NORMAL.mul(dragStrength, DRAG); - LIFT_FORCE.add(parallelDrag); + final double dragStrength = liftNormal.dot(liftVelo) * this.sable$getParallelDragScalar() * pressure * timeStep; + final Vector3d parallelDrag = liftNormal.mul(dragStrength, drag); + liftForce.add(parallelDrag); if (group != null) { group.totalDrag.sub(parallelDrag); - group.dragCenter.fma(Math.abs(dragStrength), LIFT_POS); + group.dragCenter.fma(Math.abs(dragStrength), liftPos); group.totalDragStrength += Math.abs(dragStrength); } } if (this.sable$getDirectionlessDragScalar() > 0) { + final Vector3d scaledVelo = new Vector3d(); // TEMP = VELO * scalars // FORCE += TEMP final double dragStrength = this.sable$getDirectionlessDragScalar() * pressure * timeStep; - final Vector3d directionlessDrag = LIFT_VELO.mul(dragStrength, TEMP); - LIFT_FORCE.add(directionlessDrag); + final Vector3d directionlessDrag = liftVelo.mul(dragStrength, scaledVelo); + liftForce.add(directionlessDrag); if (group != null) { group.totalDrag.sub(directionlessDrag); - group.dragCenter.fma(directionlessDrag.length(), LIFT_POS); + group.dragCenter.fma(directionlessDrag.length(), liftPos); group.totalDragStrength += directionlessDrag.length(); } } @@ -187,22 +171,25 @@ static List groupLiftProviders(final Collection