Should I set g3d to local or global

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
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Should I set g3d to local or global

Post by Bigfoot71 »

So here is the problem and what I want to do, I use the g3d library to make a 3d game (obviously) and I looked at the demo projects done with it and the question very quickly came to me while coding with it, how to define the library, local or global?

The first solution would be to import it globally but from a performance point of view this may not be the best.

The second solution would be to import it locally into each script that uses it, but that will quickly be heavy on memory, I told myself.

And so the third thing I thought would be to import it locally in the 'main.lua' then pass it as a parameter (therefore as a reference) to my objects a bit like this:

Code: Select all

function myObject:new(g3d)
    self = setmetatable({}, myObject)
    
    self.g3d = g3d
    
    return self
end
but is it really a good idea to do it ? That's where my whole question is, so if you have any ideas I'd be interested !
My avatar code for the curious :D V1, V2, V3.
Ross
Citizen
Posts: 98
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Should I set g3d to local or global

Post by Ross »

Bigfoot71 wrote: Mon Sep 12, 2022 12:33 pm The first solution would be to import it globally but from a performance point of view this may not be the best.

The second solution would be to import it locally into each script that uses it, but that will quickly be heavy on memory, I told myself.
I'm not sure where you got these ideas from, but neither of them are true. Either method will work without issues.

The miniscule performance difference for accessing a global variable compared to a local one will not have any impact on your project, unless you are really doing something crazy. IF you end up with performance problems, this is one of the last things I would suspect.

Requiring a module multiple times doesn't use more memory than requiring it once. Lua will only load the module once.

If you use g3d in a lot of different files and get tired of adding the require every time, then put it in a global. It's really just personal preference.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Should I set g3d to local or global

Post by Andlac028 »

Local variables are faster to access, but the performance impact is minimal, unless used many many times per second:

Code: Select all

-- using global
do
  a = 0
  local start = os.clock()
  for i = 0, 1000000 do
    a = a + 1
  end
  print(os.clock() - start) -- In my case, outputs: 0.002259
end

-- using local
do
  local a = 0
  local start = os.clock()
  for i = 0, 1000000 do
    a = a + 1
  end
  print(os.clock() - start) -- In my case, outputs: 0.00178
end
So locals take only ~ 80 % of time taken to access globals, so there is no huge benefit, unless used many many times per second. In my example I use it 1 000 000 times and it the difference is only about 0.000479 second.

So use locals in loops and frequently-called functions and in case, you do not want to use it globally - you can define modules globally, but do not declare temporary variables only used in one file as global, as it may mess up with another files, if they use same name
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Should I set g3d to local or global

Post by ReFreezed »

I think it's slightly unclear what you actually mean by "local". In the code snippet for the "third solution" you assign the value of the local variable/function parameter 'g3d' to a field in the 'self' table. When you want to access the g3d library in another of myObject's methods then I'd assume you'd type 'self.g3d' to do so, but g3d isn't a local variable at this point - it's a field in a table. I.e you're doing a table lookup, which accessing globals also result in. (All globals are stored in the global table '_G'.) If you want 'g3d' to always be a local (as opposed to being a field in a table) then, instead of assigning it to self.g3d, you'd have to pass it as a parameter to every function/method that needs it (which is probably going to be annoying). As for solution 1 and 2, as the others are saying, using a local or global to access the library doesn't make a huge difference.

The mentioning of "references" is also a bit confusing. In Lua, when it comes to garbage collectable objects (like tables and strings, but not numbers and booleans), all you have access to are references - never the "value". It kinda sounds like you might be thinking of C++ or similar where you can always access the value (i.e. the pointer/address to the object's memory, or the memory itself).

In addition to what's been said already, I'd say that anything that you only ever have one of (in this case a reference to a library/module) is fine to store as a global (but it really is just personal preference).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Should I set g3d to local or global

Post by pgimeno »

Using globals has the added disadvantage of loss of readability. You have to search every file to see where a global variable was defined, and in some cases (not this one) where it was changed too, while having every variable as a local has the advantage that you immediately know what file to look up when you want to know where a variable comes from, just by looking at the require.

Also, Ross is right: the return value of require() is cached by Lua into the package.loaded[ ] table, and when you require a file a second time, Lua returns the cached value rather than loading the file again. So, requiring a file multiple times from different modules has virtually no performance or memory penalty. Yes, you have as many local variables as calls to require() for a given module, all with the same value, but the memory cost of that is negligible compared to the typical memory usage of any program.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Should I set g3d to local or global

Post by Bigfoot71 »

Thanks a lot for your answers ! And yes excuse me, I expressed myself badly, it was more suppositions than assertions that I was developing, thank you again it will help me a lot I see more clearly now ^^

edit: and I noticed that I had forgotten to put 'local' to 'self' in my example, but nothing serious I understood anyway in the end.
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: No registered users and 148 guests