Box2D AS3 port showdown

You might recall all the way back in my first Box2D tutorial, that I briefly mentioned there was two main ports of the Box2D engine for ActionScript 3.0, those being BorisTheBrave’s version and Jesse Sternberg’s alchemy version.
For all these tutorials we have been using BorisTheBrave’s 2.1a, which leads me to discuss a recent video tutorial publish by the Adobe evangelist, Lee Brimelow. Lee’s excellent tutorial shows how to use the WCK framework to create Box2D worlds by laying out the various elements on the stage. This WCK framework makes use of the alchemy version of Box2D which led to a few readers to ask if the alchemy version of Box2D was faster than BorisTheBrave’s version.
Now I have never really tested the two versions before, and so was curious to find out myself, however, before we get to that, a little background information on the alchemy port.
The alchemy version of Box2D leverages Adobe’s Alchmey research project. Richard Szalay sums it up best as
“Simply put, it’s an virtual machine that uses a ByteArray as it’s memory store. The C code compiled by Alchemy has direct access to “memory” (via some opcodes introduced in Flash 10), allowing it to chunk memory around at it’s leisure (including pointers to objects). This results in some, but by no means all, code running faster. Some types of code will actually run slower in Alchemy due to it being a VM running on top of the AVM (another VM). Additionally, Alchemy does not have native access to ActionScript classes and must access them through interop classes.” - Richard Szalay
So while alchemy can provide a speed increase, it is by no means a silver bullet for performance.
My first test was to modify one of my earlier tutorials by increasing the ball count to 400 and test both Box2D ports on a Windows XP machine at work. Early results suggested they were both on par for performance, but surprisingly BorisTheBrave’s version used half as much ram.
These results surprised me, so I decided to recreate a new test from scratch, running on my home machine which is using Windows 7 64bit. BorisTheBrave’s version averaged about 14 FPS and used on average 17MB of ram. The Alchemy port averaged about 19 FPS and used on average 13MB of ram. Apart from outpeforming BorisTheBrave’s port, it was also much more consistent with memory usage. As it is currently the holidays I have been unable to reexamine my original test, but I feel these latest tests are more accurate. So please, feel free to post your results.
I should point out that you cannot simply plug in the Alchemy port of Box2D when you have written code for BorisTheBrave’s port and vice versa. That being said, it is not every hard to tweak it. The biggest change I found was regarding the usage of Vectors.
With BorisTheBrave’s version we always use a b2Vec2 class. With Alchemy we sometimes use the b2Vec2 and sometimes use another new class called V2. V2 is what you create instances of, you never create instances of b2Vec2 anymore. This is because it now only accepts one parameter which acts like a pointer. However, there are still classes that use the b2Vec2 so simply just change the x and y properties and don’t assign a new instance to it.
Apart from that, the other changes are typically properties that were actually functions that are now real properties. For example, b2ChircleShape, the radius is set by setting the m_radius property. Feel free to browse my code to see the changes. If you have any comments, just post them below.



