Improving b2 performance

Hey guys,

Without getting into using Alchemy, what have you tried to improve the performance of Box2d?

I have been swimming around in the codebase over the past week and was really surprised at the number of one-off instantiations of complex objects that happen all over the place, and felt like the library could benefit from some object pooling, or just holding onto variables in the local scope. It seemed to me that the most commonly instantiated items were b2Vec2, so I used them as a kind of "canary in the coal mine" as they're instantiated all over the place as member variables of other objects. Also, the math functions needlessly instantiate them as well as the convenience "Negative()" function

Aside from requiring fewer gc runs, pooling b2Vec2 doesn't seem to offer much performance benefits (the overhead of maintaining the pool takes away what you save from reduced instantiation of such a simple object, and in the debug player the overhead is greater than the instantation... only in the release player does the b2vec2 pool save you anything), pooling other objects like b2Mat22, b2XForm, b2OBB, and ClipVertex seemed to save a great deal of time. The latter case is especially interesting, in that every iteration in Step creates 6 ClipVertex instances... but you don't need to re-create them. Just reuse the ones you already have. It's not a big deal with only a small number of objects, but I was able to reduce my instantiations of b2Vec2 from 2400 per frame during the opening salvo of blasting letters, down to 800 by pooling ONLY the above listed objects, and by hanging onto and reusing b2Islands and other frequently instantiated objects.

My solution isn't perfect. I'm still testing it out, and after a while something goes wrong with the pooling... but while it works it *really* works. On my Droid 2, the old version of the banner on my site barely ran at all after more than 2 words piled up. Now it runs (still slowly) with more than that. Part of my problem is my own bitmap filtering scheme, but I wasn't sure where to optimize that just yet. 😉
what i've did:
replacing b2Math.max , min and abs. using (a < 0)? -a:a thingy. and i got surprisingly performance bost. i'm planing to do it all along the source. i thing the code in 2.1a i well writen by boris, in a term of Math and phisic. sadly as3 says abit differently. by using those "hard to read code" 2.1 are super, speed and features.

in my opinion the biggest problem is b2body not completely destroyed. i try to profile it and have no clue why they still live after b2World.destroyBody().

i try to focus on
- b2Body destroying
- b2ContactEdge destroying

only if some one could point me to something that i missed.
Hi there,

You're absolutely right about the min/max functions. I got rid of those myself, and never use the Flash native versions either. Any kind of convenience functions of the sort you're refering to are just needless overhead. Same with IsStatic and whatnot... calling a function to return a boolean is far more expensive than just determining the value of it yourself, even if it means writing out a crazy bitwise comparison instead of a friendly function call.

Yeah, that also means never using b2Vec2.Copy(), or b2Vec2.Negative(), or b2Vec2.Set(), or really anything that involves calling a function when the function's operation is so simple you could just write it out manually.

You know I would love to move on to 2.1, but I only have CS3 at home and can't target Flash Player 10. That means I can't use any libraries that rely on Vector.<Foo> objects. 🙁 I'd look into the kinds of body destroying issues you're seeing if I were able to use the latest library.
@maskedMan, box2DFlash 2.1 available in two version for FlashPlayer 9 and 10. why don't you check the FP 9 version(no vectors).

i spot something else. it seems b2Joint haven't fully ported yet. b2Joint.Destroy() is an empty function. and i don't know what to write there. may be it is one of the leaking cause. i just do some brutal lazy and stupid nullifiying there. L_O_L. and some how it is getting better (may be). i wish i was better in physics so i could contrib some code in the svn.

how about your progress? find something new?
Well I think the pooling is sorted out now. I was getting *really* zealous trying to track down new b2Vec2 instantiations and replace all of them with calls to the pool if possible. I think it was starting to cross some wires. For now I'm happy with the reduction and I'm not actively looking to weed them out just yet. Seems to be a case of diminishing returns.

I've got one other issue though but it's in my own application code. I think I'm destroying joints redundantly... and bodies for that matter. I've put in a safety check inside the b2World.DestroyBody and b2World.DestroyJoint functions, as well as an "isDestroyed" variable in the base classes for b2Body and b2Joint. These are booleans that are simply set to true by the destroy function if they are not true already, and if they are already true the function bails out before even attempting anything else. Fortunately this seems to have zero performance consequences in my own application, but I'm not sure whether that single safety check would be detrimental on larger projects.

The b2Joint.Destroy IS empty yes. It's called when a joint is sent to the b2World.DestroyJoint function, and you can use it to call cleanup functions on the joint that you're destroying. I'm actually doing something very similar in the DestroyBody function (which also calls an empty b2Body.Destroy function).

Actually, b2Joint.Destroy and b2Body.Destroy would be perfect hooks for object pooling, as the new instances all come from Joint/Body.Create()... if you're brave enough to believe that you're not hanging onto "destroyed" joints or bodies in your own code.
I totally missed that there was a Flash 9 version of the new lib. 😳

I'll dig into it and see what I can find there.

I wonder about the feasability of forking the project, or perhaps integrating some of these changes into the main library. I don't have enough test cases to really thoroughly say "I haven't broken everything". I would hope that the pooling strategy (both mine and any theoretical pooling of b2body and b2joint) wouldn't break other people's work, but you never know what they might be doing that relies on a continuous stream of freshly created b2Islands, Vec2s, XForms, and whatnot.
i found another clue.

when i create a body with afixture, that i didn't set it's density and friction. the body sucessfully garbage collected. when density and friction set to 1 gc fails.

Original thread: https://box2d.org/forum/viewtopic.php?t=5581