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. 😉
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. 😉