diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 5f05c255..9a60bb6f 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1987,6 +1987,7 @@ Performance improvements - Behavior tree addition/subtraction from scene optimization - Reduce bones on LOD human model - Shallow clone on physics cell creation + - More vector pool usage Increase human move speed LOD components re-attach physics diff --git a/src/main/java/electrosphere/data/utils/DataFormatUtil.java b/src/main/java/electrosphere/data/utils/DataFormatUtil.java index 37cab40c..12716277 100644 --- a/src/main/java/electrosphere/data/utils/DataFormatUtil.java +++ b/src/main/java/electrosphere/data/utils/DataFormatUtil.java @@ -6,6 +6,8 @@ import java.util.List; import org.joml.Quaterniond; import org.joml.Vector3d; +import electrosphere.mem.VectorPool; + /** * Converts data structures between formats saved to disk vs formats used in engine */ @@ -43,12 +45,12 @@ public class DataFormatUtil { */ public static Vector3d getDoubleListAsVector(List values){ if(values == null){ - return new Vector3d(); + return VectorPool.getD(); } if(values.size() > 0){ - return new Vector3d(values.get(0),values.get(1),values.get(2)); + return VectorPool.getD(values.get(0),values.get(1),values.get(2)); } else { - return new Vector3d(); + return VectorPool.getD(); } } diff --git a/src/main/java/electrosphere/entity/state/attach/AttachUtils.java b/src/main/java/electrosphere/entity/state/attach/AttachUtils.java index babff618..e790952f 100644 --- a/src/main/java/electrosphere/entity/state/attach/AttachUtils.java +++ b/src/main/java/electrosphere/entity/state/attach/AttachUtils.java @@ -6,6 +6,7 @@ import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityTags; import electrosphere.entity.EntityUtils; import electrosphere.logger.LoggerInterface; +import electrosphere.mem.VectorPool; import electrosphere.renderer.actor.Actor; import electrosphere.server.datacell.ServerDataCell; import electrosphere.server.datacell.utils.ServerEntityTagUtils; @@ -346,7 +347,7 @@ public class AttachUtils { Vector3d parentScale ){ //transform bone space - Vector3d position = new Vector3d(offsetVector); + Vector3d position = VectorPool.getD(offsetVector); position = position.rotate(new Quaterniond(boneRotation)); position = position.add(bonePosition); position = position.mul(parentScale); diff --git a/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java b/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java index 4fbf3bcd..12068dec 100644 --- a/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java +++ b/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java @@ -26,6 +26,7 @@ import electrosphere.entity.EntityUtils; import electrosphere.entity.state.attach.AttachUtils; import electrosphere.entity.state.hitbox.HitboxCollectionState.HitboxState.HitboxShapeType; import electrosphere.logger.LoggerInterface; +import electrosphere.mem.VectorPool; import electrosphere.server.datacell.Realm; import electrosphere.server.entity.poseactor.PoseActor; import electrosphere.util.math.SpatialMathUtils; @@ -461,22 +462,29 @@ public class HitboxCollectionState { Quaterniond offsetRotation = new Quaterniond(); //the bone's transform - Vector3d bonePositionD = new Vector3d(); + Vector3d bonePositionD = VectorPool.getD(); if(bonePosition != null){ - bonePositionD = new Vector3d(bonePosition); + bonePositionD.set(bonePosition); } Quaterniond boneRotation = new Quaterniond(); //the parent's transform Vector3d parentPosition = EntityUtils.getPosition(parent); Quaterniond parentRotation = EntityUtils.getRotation(parent); - Vector3d parentScale = new Vector3d(EntityUtils.getScale(parent)); + Vector3d parentScale = VectorPool.getD(); + parentScale.set(EntityUtils.getScale(parent)); //calculate Vector3d hitboxPos = AttachUtils.calculateBoneAttachmentLocalPosition(offsetPosition, offsetRotation, bonePositionD, boneRotation, parentPosition, parentRotation, parentScale); + //actually set value PhysicsEntityUtils.setGeometryOffsetPosition(collisionEngine, geom, hitboxPos, new Quaterniond()); + + //free vecs used in computation + VectorPool.release(bonePositionD); + VectorPool.release(parentScale); + VectorPool.release(hitboxPos); } /** diff --git a/src/main/java/electrosphere/mem/VectorPool.java b/src/main/java/electrosphere/mem/VectorPool.java index 216a5b23..fbb3d1db 100644 --- a/src/main/java/electrosphere/mem/VectorPool.java +++ b/src/main/java/electrosphere/mem/VectorPool.java @@ -73,6 +73,41 @@ public class VectorPool { return rVal; } + /** + * Gets a Vector3d from the pool. Allocates if no free one is available. + * @param source The source to copy from + * @return A Vector3d + */ + public static Vector3d getD(Vector3d source){ + Vector3d rVal = null; + lock.lock(); + if(vec3dPool.size() > 0){ + rVal = vec3dPool.remove(0); + } else { + rVal = new Vector3d(); + } + rVal.set(source); + lock.unlock(); + return rVal; + } + + /** + * Gets a Vector3d from the pool. Allocates if no free one is available. + * @return A Vector3d + */ + public static Vector3d getD(double x, double y, double z){ + Vector3d rVal = null; + lock.lock(); + if(vec3dPool.size() > 0){ + rVal = vec3dPool.remove(0); + } else { + rVal = new Vector3d(); + } + rVal.set(x,y,z); + lock.unlock(); + return rVal; + } + /** * Releases a Vector3d back into the pool * @param data The object to release