From d200e406d5aa7ee6645544015a6e4d8fe6ed3e39 Mon Sep 17 00:00:00 2001 From: Phr00t Date: Tue, 29 Apr 2014 14:33:01 -0400 Subject: [PATCH 1/4] performance improvements to PhysicsSpace --- .../java/com/jme3/bullet/PhysicsSpace.java | 21 +++++++------- .../java/com/jme3/bullet/PhysicsSpace.java | 28 +++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index fd12dcb89..1109a92c6 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -46,11 +46,14 @@ import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.Node; import com.jme3.scene.Spatial; +import com.jme3.util.SafeArrayList; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -91,8 +94,8 @@ public class PhysicsSpace { private Map physicsBodies = new ConcurrentHashMap(); private Map physicsJoints = new ConcurrentHashMap(); private Map physicsVehicles = new ConcurrentHashMap(); - private List collisionListeners = new LinkedList(); - private List collisionEvents = new LinkedList(); + private ArrayList collisionListeners = new ArrayList(); + private Stack collisionEvents = new Stack(); private Map collisionGroupListeners = new ConcurrentHashMap(); private ConcurrentLinkedQueue tickListeners = new ConcurrentLinkedQueue(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); @@ -331,7 +334,7 @@ public class PhysicsSpace { // } private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { // System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); - collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); + collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); } /** @@ -361,17 +364,15 @@ public class PhysicsSpace { public void distributeEvents() { //add collision callbacks -// synchronized (collisionEvents) { - for (Iterator it = collisionEvents.iterator(); it.hasNext();) { - PhysicsCollisionEvent physicsCollisionEvent = it.next(); - for (PhysicsCollisionListener listener : collisionListeners) { - listener.collision(physicsCollisionEvent); + int clistsize = collisionListeners.size(); + while( collisionEvents.empty() == false ) { + PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop(); + for(int i=0;i Future enqueueOnThisThread(Callable callable) { diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 484f52133..6d4db3a09 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -66,11 +66,13 @@ import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.Node; import com.jme3.scene.Spatial; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -111,8 +113,8 @@ public class PhysicsSpace { private Map physicsVehicles = new ConcurrentHashMap(); private Map collisionGroupListeners = new ConcurrentHashMap(); private ConcurrentLinkedQueue tickListeners = new ConcurrentLinkedQueue(); - private List collisionListeners = new LinkedList(); - private List collisionEvents = new LinkedList(); + private ArrayList collisionListeners = new ArrayList(); + private Stack collisionEvents = new Stack(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); private Vector3f worldMin = new Vector3f(-10000f, -10000f, -10000f); private Vector3f worldMax = new Vector3f(10000f, 10000f, 10000f); @@ -294,7 +296,7 @@ public class PhysicsSpace { CollisionObject rBody1 = (CollisionObject) body1; node = (PhysicsCollisionObject) rBody0.getUserPointer(); node1 = (PhysicsCollisionObject) rBody1.getUserPointer(); - collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp)); + collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp)); } return true; } @@ -332,19 +334,17 @@ public class PhysicsSpace { public void distributeEvents() { //add collision callbacks -// synchronized (collisionEvents) { - for (Iterator it = collisionEvents.iterator(); it.hasNext();) { - PhysicsCollisionEvent physicsCollisionEvent = it.next(); - for (PhysicsCollisionListener listener : collisionListeners) { - listener.collision(physicsCollisionEvent); - } - //recycle events - eventFactory.recycle(physicsCollisionEvent); - it.remove(); + int clistsize = collisionListeners.size(); + while( collisionEvents.empty() == false ) { + PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop(); + for(int i=0;i Future enqueueOnThisThread(Callable callable) { AppTask task = new AppTask(callable); System.out.println("created apptask"); From ecf62977838522f6031245dc0467d828a6199aeb Mon Sep 17 00:00:00 2001 From: Phr00t Date: Mon, 19 May 2014 13:46:35 -0400 Subject: [PATCH 2/4] stack to deque --- .../src/main/java/com/jme3/bullet/PhysicsSpace.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 1109a92c6..fb7fe1b31 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -46,14 +46,13 @@ import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.Node; import com.jme3.scene.Spatial; -import com.jme3.util.SafeArrayList; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -95,7 +94,7 @@ public class PhysicsSpace { private Map physicsJoints = new ConcurrentHashMap(); private Map physicsVehicles = new ConcurrentHashMap(); private ArrayList collisionListeners = new ArrayList(); - private Stack collisionEvents = new Stack(); + private ArrayDeque collisionEvents = new ArrayDeque(); private Map collisionGroupListeners = new ConcurrentHashMap(); private ConcurrentLinkedQueue tickListeners = new ConcurrentLinkedQueue(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); @@ -365,7 +364,7 @@ public class PhysicsSpace { public void distributeEvents() { //add collision callbacks int clistsize = collisionListeners.size(); - while( collisionEvents.empty() == false ) { + while( collisionEvents.isEmpty() == false ) { PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop(); for(int i=0;i Date: Mon, 19 May 2014 13:47:24 -0400 Subject: [PATCH 3/4] stack to deque --- .../src/main/java/com/jme3/bullet/PhysicsSpace.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 6d4db3a09..ba9707cb0 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -66,13 +66,13 @@ import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.Node; import com.jme3.scene.Spatial; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -114,7 +114,7 @@ public class PhysicsSpace { private Map collisionGroupListeners = new ConcurrentHashMap(); private ConcurrentLinkedQueue tickListeners = new ConcurrentLinkedQueue(); private ArrayList collisionListeners = new ArrayList(); - private Stack collisionEvents = new Stack(); + private ArrayDeque collisionEvents = new ArrayDeque(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); private Vector3f worldMin = new Vector3f(-10000f, -10000f, -10000f); private Vector3f worldMax = new Vector3f(10000f, 10000f, 10000f); @@ -335,7 +335,7 @@ public class PhysicsSpace { public void distributeEvents() { //add collision callbacks int clistsize = collisionListeners.size(); - while( collisionEvents.empty() == false ) { + while( collisionEvents.isEmpty() == false ) { PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop(); for(int i=0;i Date: Sun, 25 May 2014 12:20:56 +0200 Subject: [PATCH 4/4] - change push() --> add() for physics collision events --- jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 2 +- jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index fb7fe1b31..06210c388 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -333,7 +333,7 @@ public class PhysicsSpace { // } private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { // System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); - collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); + collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); } /** diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index ba9707cb0..2d48bcb2f 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -296,7 +296,7 @@ public class PhysicsSpace { CollisionObject rBody1 = (CollisionObject) body1; node = (PhysicsCollisionObject) rBody0.getUserPointer(); node1 = (PhysicsCollisionObject) rBody1.getUserPointer(); - collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp)); + collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp)); } return true; }