Posted on February 20, 2008
What is that annoying AS3 Hiccup? Part 2
Well, I initiated my buddy Chris Cutler to help me out today and discovered a couple things about optimizing AS3 apps, but also opened up some questions.
Fist, we installed the AWESOME Active Graph Application. It gives you a graph of memory usage and I can now see the clean and sweep operations in graph form. Every time I have a hiccup in my game, it is during a clean and sweep, so I guess Mike and Jobe were right. The problem is figuring out how to handle this problem. We searched through my code and found quite a few simple things to fix first:
1. I am getting rid of as many local object instantiations as possible and replacing them with global objects. This might sound backward to a ‘good coder’, but for game optimization in AS3, it seems to be helping quite a bit. I have not completed my search for all of the offending code, but after replacing the code in just my sound manager, I can see a noticeable decrease in memory usage spikes. It is these spikes that cause the annoying hiccup, and it seems that I need to do my own disposal as often as possible to make sure that the object GUNK doesn’t build up too much and cause a major garbage collection clean and sweep.
2. I have been looking at my blitting engine to see what optimizations I can make. I want to do a double buffer, but I am having trouble figuring out exactly how to proceed. I first tried to add a second canvas not attached to a display , did a copy pixels it on each frame, and then cloned it to my BitmapData canvas that is attached to my one display object. I don’t want to just assign the on screen canvas to the off screen with a = operation because then they would essentially be the same object. The cone just wouldn’t work though. For some reason, the clone operation results in my display not updating – very strange. I next decided to just (for shits and giggles) try to use the draw operation from one BitmapData to the other. It works fine, but I know draw is pretty slow, so I don’t think there is much of an advantage to it. After playing around with using a general = assignment operation I came up with a new version. This current version uses a lock on both BitmapData (on and off screen) before the = assignment and and unlock after. I don’t see much improvement here yet, but I will keep trying.
Next I will try the beginBitmapFill method instead of copyPixels and see if that gets me anywhere.
If you know of a method to double buffer that works well, please send it my way. I know I am just being obtuse and might be thinking about this the wrong way.