Basic question about optimization (store variable vs accessing)

Hi,

I have a minor question about optimization, and I know that in my case it probably won’t matter but I’m interested in learning more about these kind of things for the future.

I have a Vector3 array that i store the waypoints or “path” that the enemies in my tower defense game should take, the array is created at the start of the game and stored in a gameHandler script. Every time an enemy is spawned they get assigned a path to follow.

Should I copy the array created in my gameHandler script to each enemy when created, or should the enemy script simply call the gameHandler whenever they need to fetch the next waypoint.
I guess the choice is between efficiency (many calls to gameHandler for each enemy) vs memory usage (storing the array).

In my case I guess I will just copy the array, but lets imagine that they grow really large, is there a point where I should approach this differently. Also it is a mobile game if that change anything.

I’m assuming that the path is single path shared between all enemies (for given level) and the path doesn’t change during play.

My answer is written under these assumptions;

Simple answer is no. If you copy the array (I mean real copy - allocate new array, copy values), you will have unnecessary redundancy of data in your code. There is lots of articles describing why we’re trying to avoid data redundancy - in short, it will be hard in future to keep all redundant data consistent and updated.

If you do not copy the array, but just take reference (sharing same data), it’s still not good, because a) you’re exposing inner working of class working with the path and b) some code might accidentally change it breaking it for everyone. Read about encapsulation.

So the best solution is not to provide path at all (unless really needed), but provide functions which do work on the path. For example GetNextDestination(currentPosition).

Now, about optimizations:

  • General rule is: don’t do it.
  • Second rule is: don’t do it - yet.

Before you do an optimization, you should always measure what the slowest part of the game is and start there. In this particular case, the overhead done by calling function can be rounded to zero when you compare it with overhead caused by rendering the unit. When you add - let’s say - thousands enemies and game slows down, avoiding one method call will save you like less then 1% of time needed for one frame. If you optimize meshes of enemies (less polygons, single batchable material, etc.), you’ll save much more. I already saw performance gains reaching 90% (but that’s extreme). In other words, premature optimization is waste of time (unless you’re really experienced). I suggest reading articles/books about that. (On the other hand “avoiding premature optimization” is NOT “wasting resources all the time”. Of course you should try to write fast code, but readability, good design like encapsulation, etc. are top priority. Optimizations comes after “good designed code” is too slow, which is actually bit rare.)