Efficiency with lots of variables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sisyphu2
Prole
Posts: 17
Joined: Mon Sep 28, 2020 3:15 pm

Efficiency with lots of variables

Post by sisyphu2 »

I am making a game which has a lot of data, multiple tables containing tables within the entries with yet more data - e.g. unit list, each unit has its own data, building list, etc. There are hundreds of entries.

For ease I am loading it as a global table at the start (since data is constant and may need to be accessed anywhere). With some tinkering I could make the tables local to one of my main classes that handles most world objects. Is anyone with experience on this able to tell me whether that's likely to improve memory usage/performance.

Currently no performance issues but there is quite a lot more data to come. I am always against premature optimisation but if I am wasting time using globals then I would rather get rid of that habit now.

Of course all 'in game' variables are local, it's just the hard data that's global.

thanks ! :)
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Efficiency with lots of variables

Post by pgimeno »

The space gain of using a local instead of a global for a single variable is negligible. It saves a little bit of performance, but not much (well, it can become significant in some special occasions). You can get that minor speed advantage by turning the global into a local with something like: 'local mytable = mytable'.

sisyphu2 wrote: Fri Jan 08, 2021 11:25 amI am always against premature optimisation
Bad idea. Adapting your coding style to write fast alternatives to certain code patterns from the beginning, isn't that hard, once you have the concepts on which ones work best, costs basically zero effort once you have the gist of it, and can save you profiling and optimisation work later if things don't go as fast as you expected. In some cases the performance problem can have structural causes, and take a huge amount of work to reorganize once the project is finished.

See also https://ubiquity.acm.org/article.cfm?id=1513451

That said, in this particular case, localising the globals as described above takes almost zero effort and there's very little gain, so it's not a terrible idea to postpone it. The main gain is readability: when I see 'local mytable = mytable' I immediately realize that mytable is a global and saves me a search for a definition.
sisyphu2
Prole
Posts: 17
Joined: Mon Sep 28, 2020 3:15 pm

Re: Efficiency with lots of variables

Post by sisyphu2 »

Thank you Pgimeno, this is helpful. I had been doing something like this to some extent (e.g. local lg = love.graphics) but somehow hadn't thought to extend this to my own global tables!
Post Reply

Who is online

Users browsing this forum: Google [Bot], MrFariator and 140 guests