While I was reading the PDF “Introduction to Starling” (downloadable here), I stumbled upon a pixel-level collision detection example.

When I noticed the code involved instantiating a new Point for each images you need to compare, and then passing in their x and y values in the constructor – I found that disturbing (in terms of instantiation performance).

From one point of view, some would say that it’s easy, faster and more intuitive to do it this way. But from another point of view (which I share), if you cycle multiple times through sections of code that unnecessarly creates new primitive data times, they shouldn’t be considered harmless to your application performance. It would likely cause a snowball effect of memory consumption during runtime.

To address this issue, I would encourage the use of reusable instances. Doesn’t it make much more sense to reuse the same Compare instance to invoke it against two Bitmaps? (In the same above mentionned scenario, about the Starling pixel-level collision example).

If you need to compare more than once throughout your application, either make reference to the same instance, or instantiate a pool of Compare instances that you can request from (via a static method, like “getAvailableInst():Compare” for example.) and then if you ever need to destroy it, you’ll actually be releasing it and nullifying its properties and it’ll go back into the static array that holds all available Compare objects. Neat yes?

So that’s my rant / advice to you for today!

Enjoy!