Possible performance boost: cache the "reference point" APIs

I have recently been tuning up my game and designing some basic menu screens and GUI elements. One API aspect I use frequently is the “reference points”, as in:

object:setReferencePoint(display.BottomRightReferencePoint)

Anybody who uses these knows how valuable they are to aligning text and other objects properly.

Then I remembered a little tip from the Optimization section of Corona’s docs…


_ Cache properties in a local variable
If you are constantly accessing a property of a table but not changing its value, then you should cache that value. There is a slight performance penalty to doing property lookups in a table. For objects created by Ansca’s api’s — such as the display object returned by display.newImage() — the penalty is even higher._

So I considered, can any API beginning with " display." be cached for a slight performance boost? Can somebody (Ansca) confirm this?

Most people already do (or should) cache their math functions in local variables, i.e. local cos = math.cos. This prevents a constant “lookup” of the cos function if you’re using it frequently, especially in a loop.

So why not the same for the " display." API?

I tested putting my reference points into local variables, as follows:

local centerLeft = display.CenterLeftReferencePoint --near top of file  
--and further down, the call:  
object:setReferencePoint(centerLeft)  

This seems to work but I’m unsure if it results in any performance boost. In theory, it might only do so if you’re using this alignment principle alot, as in a large menu of many text items that must be aligned center-left.

Thoughts or opinions on this?

Brent
[import]uid: 9747 topic_id: 9016 reply_id: 309016[/import]

Hi Brent,

Great point! It hasn’t been thoroughly tested, but in theory, it *should* provide some kind of performance boost. It definitely won’t hinder performance so it won’t hurt either way.

Thinking about it a little more, it could really be of use if you have to create a TON of objects and set their reference point for each object (such as a for loop that creates a ton of objects, or tiles, etc).

Thanks for bringing this up, because when it’s time for optimization, you can never do too much :slight_smile: [import]uid: 52430 topic_id: 9016 reply_id: 32916[/import]

I believe that the reference points may change if your container’s content changes, like adding display objects to groups or moving those objects while they are a group member - in those cases the display-group’s reference points will change - other operations, like scaling can also affect individual object’s RPs.

All those cases make if dangerous to cache reference point values, unless you’re absolutely sure they remain constant… may be a nasty bug to find if you accidentally use the incorrect cached values… not sure if it is worth the risk…

-FrankS.
[import]uid: 8093 topic_id: 9016 reply_id: 33001[/import]