EntitySolverUpdatable.UpdateConnectedMembers

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

EntitySolverUpdatable.UpdateConnectedMembers

Post by ecosky »

Hi,

I've been trying to find & fix various garbage generators and I came across something in Bepu which I would like to understand better.

The profiler pointed me to this callstack as a source of objects being generated & discarded (I don't know why the right edge with more stats is being cut off here, but visit the link directly to see the full pic):
Image
In my contrived situation of just having a weapon fired repeatedly into the ground, this allocation is around 20% of all garbage my app is creating so I'm interested in fixing it if possible.

What is happening in game is a weapon creates a capsule as a slow moving projectile that that is removed from the scene after it hits the ground.

What I find curious is down in EntitySolverUpdatable.UpdateConnectedMembers, I see this bit of code:

Code: Select all

        void UpdateConnectedMembers()
        {
            //Since we're about to change this updateable's connections, make sure the 
            //simulation islands hear about it.  This is NOT thread safe.
            var deactivationManager = simulationIslandConnection.DeactivationManager;
            if (deactivationManager != null)
            {
                simulationIslandConnection.Owner = null; //Orphan the simulation island connection.
                deactivationManager.Remove(simulationIslandConnection);
            }
            else if (!simulationIslandConnection.SlatedForRemoval) //If it's not already going to be cleaned up, then we need to do it here.
                Resources.GiveBack(simulationIslandConnection); //Well, since we're going to orphan the connection, we'll need to take care of its trash.

            //The SimulationIslandConnection is immutable.
            //So create a new one!
            //Assume we've already dealt with the old connection.
            simulationIslandConnection = Resources.GetSimulationIslandConnection();
            for (int i = 0; i < involvedEntities.count; i++)
            {
                simulationIslandConnection.members.Add(involvedEntities.Elements[i].activityInformation);
            }
            simulationIslandConnection.Owner = this;

            //Add the new reference back.
            if (deactivationManager != null)
                deactivationManager.Add(simulationIslandConnection);
        }
The call to Resources.GetSimulationIslandConnection() is what is creating the new object. What I have noticed is the involvedEntities.count is 1, and contains only the capsule being removed. This is not a leak technically because it does eventually get garbage collected (not sure when/how yet), but it would of course be preferred if it were recycled as is I think intended. Or better yet, is it possible that having a single involved entity is an edge case that should cause this function to skip everything below the "create a new one" comment? If not, it seems that maybe the connection isn't being recycled properly because if it were it wouldn't need to keep allocating new ones.

While I expect I can configure Bepu to not even create simulation islands for these projectiles (although I forget offhand how to do it) I do have other objects created in a similar manner that need to roll around a bit before being removed so that optimization won't always make sense.

Thanks for any feedback,
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

Re: EntitySolverUpdatable.UpdateConnectedMembers

Post by ecosky »

I did a few tests and drilled into some more related code, it looks like the simple changes I was hoping might work aren't correct.
I should mention that I do have various local changes to Bepu, while I don't think they would interfere with this system it does mean if this behavior sounds buggy maybe I have done something wrong along the way.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: EntitySolverUpdatable.UpdateConnectedMembers

Post by Norbo »

My limited attempt at reproducing the problem failed. Could you reproduce the issue in the development version demos so I can take a closer look? Simulation island connection management gets pretty complicated as far as ownership goes, so I don't trust myself to efficiently guess what's going on :)
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

Re: EntitySolverUpdatable.UpdateConnectedMembers

Post by ecosky »

Thanks Norbo.

I'll try to isolate the behavior in a tester. I dug into the related code for a few hours and I see what you mean about the ownership being complicated. I followed some SimulationIslandConnections through the system and from what I can tell it does look like it is all set up correctly, so I'm guessing it is somehow related either to my local changes or perhaps just my usage pattern. I'll let you know what I find out either way.

Thanks again
Post Reply