I’ve done a lot of reading on performance enhancements and testing solutions for Flash, but I’ve never actually done any A/B testing of my own. Let’s fix that.
I’m using yesterday’s SolarCraft as a template; it has a few game logic loops and a bunch of movement on the screen, vector calculations and Vector iterations. I think it’s a good “realistic” platform, as opposed to looping through display-less routines or something. I hope to gain practical data here, not theoretical!
For each of my tests I simply adjust the number of ”collectors” that spawn when you click on the base in SolarCraft. This gives me a quick and easy item count, and I’ve tacked on a profiler to see my memory use and framerate.
Setting the “.cacheAsBitmap” property to “true” on DisplayObjects is supposed to make normally-vector art display faster, but only if the art translates (not if it rotates or scales). Let’s see!
With cacheAsBitmap set to FALSE, I can maintain a constant 30FPS with a maximum of 800 collectors on screen. Memory use peaks at about 8.25mb. Or roughly 0.010mb per collector.
With cacheAsBitmap set to TRUE, I can maintain a constant 30 FPS with a maximum of 1,900 collectors on screen. Memory use peaks at about 26mb, or 0.013mb per collector.
A slight (but marginal) increase in memory usage for more than double the performance! That’s pretty good.
As it stands, the collectors are only translating; let’s see what happens if we add a slow rotation to the collectors each frame ( collector.rotation += 0.01 ):
Eek. Using the previous 1,900 collectors, my performs drops from 30FPS down to 8FPS. Attempting to regain my 30FPS, I had to drop my collector count to 500 – below our baseline 800 figure when cacheAsBitmap is turned off.
With cacheAsBitmap turned off and rotations left on, I easily reach back up to the 800 mark at 30 FPS.
Seems like vectors are indeed much better performers when it comes to rotation (and memory usage didn’t change).
I’m replacing my rotation code with scaling code ( collector.scaleX += 0.01 ) to see if we get similar performance numbers as we did with rotations.
With cacheAsBitmap turned off, I hit 30 FPS with 700 collectors on screen. a Slight performance drop from our Scaling experiment.
With cacheAsBitmap turned on, I hit 30 FPS with 500 collectors on screen.
So it looks like cacheAsBitmap performs equally as poorly with scaling as it does with rotation; but default vector behaviour handles rotation better than scaling.
Rotation AND Scaling
The results of this test were identical to the Scaling test, so I won’t reproduce them here.
It looks like Adobe’s documentation was factually accurate. cacheAsBitmap gives you significant performance gains if you don’t rotate or scale your vector art at all, and stick to simple x/y translations. cacheAsBitmap will also marginally increase your memory footprint.
But those results I expected.
What I didn’t expect to see was scaling performing worse than rotation in the vector realm. That’s interesting…