From 7359258ca6688b93d5ef010008a42a0318f15ab7 Mon Sep 17 00:00:00 2001 From: Floris Date: Sat, 6 Apr 2019 13:05:09 +0200 Subject: [PATCH 1/3] Reduce garbage for AlignedNode --- .../BulletSharp/SoftBody/AlignedNodeArray.cs | 14 +++++++++++++- Scripts/SoftBody/BSoftBody.cs | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs index 00a7998..1ac828e 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs @@ -74,10 +74,12 @@ public void Reset() public class AlignedNodeArray : IList { private IntPtr _native; + private Dictionary _nodeObjects; internal AlignedNodeArray(IntPtr native) { _native = native; + _nodeObjects = new Dictionary(); } public int IndexOf(Node item) @@ -103,7 +105,17 @@ public Node this[int index] { throw new ArgumentOutOfRangeException("index"); } - return new Node(btAlignedSoftBodyNodeArray_at(_native, index)); + + IntPtr nodePtr = btAlignedSoftBodyNodeArray_at(_native, index); + long nodePtrInt = nodePtr.ToInt64(); + Node node; + if (!_nodeObjects.TryGetValue(nodePtrInt, out node)) + { + node = new Node(nodePtr); + _nodeObjects.Add(nodePtrInt, node); + } + + return node; } set { diff --git a/Scripts/SoftBody/BSoftBody.cs b/Scripts/SoftBody/BSoftBody.cs index 53be399..01152af 100644 --- a/Scripts/SoftBody/BSoftBody.cs +++ b/Scripts/SoftBody/BSoftBody.cs @@ -106,8 +106,9 @@ public void DumpDataFromBullet() } for (int i = 0; i < m_BSoftBody.Nodes.Count; i++) { - verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity(); - norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity(); + Node node = m_BSoftBody.Nodes[i]; + verts[i] = node.Position.ToUnity(); + norms[i] = node.Normal.ToUnity(); } } } From 486d532c822bc9754e2af2cdd2fd5ad16ea24603 Mon Sep 17 00:00:00 2001 From: Floris Date: Sat, 6 Apr 2019 13:15:56 +0200 Subject: [PATCH 2/3] Improve SoftBody update performance --- .../BulletSharp/SoftBody/AlignedNodeArray.cs | 11 ++++++++++- .../BulletSharp/SoftBody/SoftBody.cs | 4 ++-- Scripts/SoftBody/BSoftBody.cs | 19 ++++++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs index 1ac828e..d00a092 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs @@ -103,7 +103,7 @@ public Node this[int index] { if ((uint)index >= (uint)Count) { - throw new ArgumentOutOfRangeException("index"); + throw new ArgumentOutOfRangeException(nameof(index)); } IntPtr nodePtr = btAlignedSoftBodyNodeArray_at(_native, index); @@ -123,6 +123,15 @@ public Node this[int index] } } + public IntPtr GetNodePointer(int index) + { + if ((uint)index >= (uint)Count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + return btAlignedSoftBodyNodeArray_at(_native, index); + } + public void Add(Node item) { btAlignedSoftBodyNodeArray_push_back(_native, item._native); diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs index 8b93d81..e2d5b69 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs @@ -2335,13 +2335,13 @@ public Vector3 Velocity [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern IntPtr btSoftBody_Node_getLeaf(IntPtr obj); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] - static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value); + public static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_getQ(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_getV(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] - static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value); + public static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_setArea(IntPtr obj, float value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] diff --git a/Scripts/SoftBody/BSoftBody.cs b/Scripts/SoftBody/BSoftBody.cs index 01152af..98d1f85 100644 --- a/Scripts/SoftBody/BSoftBody.cs +++ b/Scripts/SoftBody/BSoftBody.cs @@ -96,19 +96,24 @@ public void DumpDataFromBullet() if (isInWorld) { SoftBody m_BSoftBody = (SoftBody)m_collisionObject; - if (verts.Length != m_BSoftBody.Nodes.Count) + int nodeCount = m_BSoftBody.Nodes.Count; + if (verts.Length != nodeCount) { - verts = new Vector3[m_BSoftBody.Nodes.Count]; + verts = new Vector3[nodeCount]; } if (norms.Length != verts.Length) { - norms = new Vector3[m_BSoftBody.Nodes.Count]; + norms = new Vector3[nodeCount]; } - for (int i = 0; i < m_BSoftBody.Nodes.Count; i++) + for (int i = 0; i < nodeCount; i++) { - Node node = m_BSoftBody.Nodes[i]; - verts[i] = node.Position.ToUnity(); - norms[i] = node.Normal.ToUnity(); + BulletSharp.Math.Vector3 pos; + Node.btSoftBody_Node_getX(m_BSoftBody.Nodes.GetNodePointer(i), out pos); + verts[i] = pos.ToUnity(); + + BulletSharp.Math.Vector3 normal; + Node.btSoftBody_Node_getN(m_BSoftBody.Nodes.GetNodePointer(i), out normal); + norms[i] = normal.ToUnity(); } } } From 11095560dbaf2c65e4b3c8a2a80d94f8ad7793f0 Mon Sep 17 00:00:00 2001 From: Floris Weers Date: Mon, 8 Apr 2019 21:01:29 +0200 Subject: [PATCH 3/3] Remove nameof because of C# version --- Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs index d00a092..b437fca 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs @@ -103,7 +103,7 @@ public Node this[int index] { if ((uint)index >= (uint)Count) { - throw new ArgumentOutOfRangeException(nameof(index)); + throw new ArgumentOutOfRangeException("index"); } IntPtr nodePtr = btAlignedSoftBodyNodeArray_at(_native, index); @@ -127,7 +127,7 @@ public IntPtr GetNodePointer(int index) { if ((uint)index >= (uint)Count) { - throw new ArgumentOutOfRangeException(nameof(index)); + throw new ArgumentOutOfRangeException("index"); } return btAlignedSoftBodyNodeArray_at(_native, index); }