Arbiter.ConstructArbiter generating garbage?

Topics: Developer Forum
Sep 23, 2008 at 6:46 PM
Edited Sep 23, 2008 at 6:47 PM

I've been profiling a bit lately and found that a lot of garbage (relative to the rest of my game) was being generated during the Update of the physics simulator. I traced the allocations down to the contact comparer being created every time Arbiter.ConstructArbiter is called. I see that there is an attempt to not create garbage by creating the comparer once per arbiter creation, but I think this can be further improved by creating only one comparer for the lifespan of the application.

    Line 40:    private Comparison<Contact> contactComparer;

Could become the following with no adverse side effect:

    Line 40:    private static Comparison<Contact> contactComparer = CompareSeperation;

You could then remove the comparer initialization on Line 77. I've tested the change and it has cut down on garbage by asignificant amount. The comparer points to the same static method each time, so nothing is functionally different.


Sep 24, 2008 at 1:06 AM
There are similar issues with the SelectiveSweepCollider.DetectInternal method, to a lesser degree.

    Line 258:    private void DetectInternal(bool doX)
    Line 259:    {
    Line 260:        List<Stub> stubs = (doX) ? (xStubs) : (yStubs);
    Line 261:        LinkedList<Wrapper> currentBodies = new LinkedList<Wrapper>();

could be changed to:

    Line 258:    private LinkedList<Wrapper> currentBodies = new LinkedList<Wrapper>();
    Line 259:   
    Line 260:    private void DetectInternal(bool doX)
    Line 261:    {
    Line 262:        List<Stub> stubs = (doX) ? (xStubs) : (yStubs);
    Line 263:        currentBodies.Clear();

This removes unnecessary list creations each update as well. Has anyone else played with profiling Farseer? I'd be interested to know the best way for me to make suggestions for improvements such as these.
Sep 24, 2008 at 12:18 PM
Thanks a lot wmfwlr!

I have been profiling the Farseer engine to look for hotspots (not already known hotspots ;) ) and I've just begun to look for garbage buildup in the Farseer Physics 2.0 source code.

Garbage is a big issue for us when it comes to performance, missing just a single line of code in a heavily instantiated class can cause a big buildup of garbage and potential huge performance penalties when GC comes by and collect. (not to mention the Xbox GC... phew)

I use CLR profiler to look for garbage. Keeping an eye out for the Gen 0 collection and % time spend in GC. I also use the ANTs profiler and the one build into VS2008 Team System to look for memory hogs and the like.

Do you have any preferences or tips? :)

If you find more garbage, please tell me and I will have a look at it.