0

I'm currently working on a simple database structure to store information on components for use in a games engine.

I have two classes that make up a database : DBEntry_Name - the class making up each entry in the database and NameDB which is the database itself.

I don't so much as have a problem, but a query as to how references to database entries are stored (in terms of memory usage) in variables. Some of my entries contain references to other entries in a completely different database. An example of a typical entry is below:

public class BuildingDB {

    private static List<DBEntry_Building> AllBuildings = new List<DBEntry_Building>();
    #region Towers
    public static DBEntry_Building Tower = new DBEntry_Building {
    ID = 1, // <== this is due to by replaced by a reference to the List index of the entry
    Name = "Tower",
    Prefab = Resources.Load("Prefabs/Tower") as GameObject,
    Cost = 10,
    BuildTime = 5.0f,
    AttackSpeed = 1,
    AttackDamage = 10,
    AttackRange = 7,
    ProjectileType = ProjectileDB.Rock, // <==== is this storing excess information? could we request the ID of the projectile and store that instead?
};

#endregion
}

I can replace the line I have the issue with by using a function to exchange the DBentry for an integer instead which could then be exchanged for the index of the database list, but is this necessary? My suspicion is that ProjectileType will take up much more memory than a simple int, hence why I'm asking this question.

How will ProjectileType be stored? Will all of it's values be stored too, or will they be retrieved from the projectile database?

Thanks.

Lich
  • 10
  • 1
  • 2
  • How much more is much more. And what kind of device are you targeting? There is a high likely hood you do not even have to worry about the increase, as it is not that significant. – MX D Sep 06 '16 at 15:23
  • Currently we are targeting PC, but we were toying with the idea of making a mobile port too and thus I don't want to add more work than is necessary when it comes to adapting the code to port because of small inefficiencies like this. Currently ProjectileType contains a float variable, a sprite and an integer. Now, I don't know if or how much more information will be required. I suppose the trade-off here is between the processor and memory. I have put this same question to a programmer friend who came to a similar conclusion to yourself. I appreciate the response all the same! – Lich Sep 06 '16 at 19:55

1 Answers1

0

As I already mentioned in my comments. This is very dependant on which platform you are targeting. And the fastest solution is just observing, or as mentioned at

... You shouldn't optimize because you think you will get a performance gain. There are obvious optimizations (like not doing string concatenation inside a tight loop) but anything that isn't a trivially clear optimization should be avoided until it can be measured. ~ programmers SE

So until you know that the target platform you want to port to, aka the mobile port, cant handle the extra memory load. Or at the very least can measure the difference in performance/impact on the machine, it is not worth thinking about to much.

You can however make a rough assumption. Do I really need these floats, sprites and integers. Or can I get around with only a ID? Will I then be loading the object later anyway? etc. Some other tips to determine when to start these kind of optimisations can be found in this SO post When is optimization premature

Community
  • 1
  • 1
MX D
  • 2,453
  • 4
  • 35
  • 47
  • This is a suitable and useful answer. As you say, it's probably not worth considering before its effect can actually be tested and quantified. My own doubt on the matter came due to my relative inexperience in programming. Advice such as this is useful and I will certainly browse that post for more. For the time being I have left the code as it is. I may consider optimising if conditions change. Thanks. – Lich Sep 07 '16 at 19:07