When Flash Player 10 was released, you heard about this new Vector class, if you are not using it already in your FP10 projects, well you definitely should. When talking with some guys at DotEmu, they confessed me that their whole emulation engine (ported from Java to AS3) which was able to emulate any kind of 8bit and 16bit console was not fast enough to be used in production in Flash Player 9.
When Flash Player 10 was released, they switched from Array to Vectors and realized that their engine was running much faster thanks to methods like BitmapData.setVector() and the Vector class.
After a few days I realised that the JPEG encoding class used in the corelib package was using a lot of Arrays and could benefit from the Vector class and I was pretty sure some tricky optimization could be done. That's what I did and modified its code to make it faster, I was happy to see that the new version was around 2.5 times faster (on my machine). Now more than 4 times.
Here is a little demo (encoding a 2880*2880 BitmapData) showing the difference between the old version and the new one, if you could post your results through the comments that would be interesting :
So what did I do ?
- I used bitwise operators as much as possible.
- I replaced all Arrays with fixed length Vectors.
- I used pre-incrementation rather than post-incrementation (thanks Joa for this one ;)).
- I casted to int almost all my Vector indices access.
- Other minor stuff you always do to optimize your code
The encoding process could be even faster thanks to Pixel Bender for the RGB to YUV process, unfortunately as you may know loops are forbidden when Pixel Bender jam is running in the Flash Player. Pre-processed the RGB to YUV with a simple ColorMatrixFilter or Pixel Bender, this didn't bring performance improvements. I am sure this can still be optimized if ported to haXe for instance, if some of you are using an asynchronous version of the original version, you can integrate the optimization from this one.
Download it here.
Update : 05/24/09 : New version with tiny additions, which should make it run a little bit faster.
Update : 05/26/09 : New version with tiny additions, a little bit faster. (Thanks Joa and Kyle for your tips)
Update : 05/27/09 : New version with some unuseful code removed.
Update : 05/30/09 : New version with tiny additions, a little bit faster again. (Thanks tst)