Lua: RAM difference, when defining function or not?

Post Reply
User avatar
TalkLounge
Member
Posts: 318
Joined: Sun Mar 26, 2017 12:42
GitHub: TalkLounge
In-game: TalkLounge
Location: Germany

Lua: RAM difference, when defining function or not?

by TalkLounge » Post

Does this function will be saved in the RAM, when that's the complete init.lua?

Code: Select all

local enabled = false

if enabled then
  function superComplex(param)
    --Super complex funtion code
  end
end
Like this?

Code: Select all

function superComplex(param)
  --Super complex funtion code
end
Because I am thinking, if I should do something like the first example to just save some RAM space, when the function will never be used anyway. But if the function will be saved anyway, I should save RAM by not defining the variable "enabled" and the if clause.

Does someone know how lua works in this case? Thanks.
Subgames Server: Sky World Subgames German Survival Server: Wildes Land 2 E-Mail: talklounge@yahoo.de

Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

Re: Lua: RAM difference, when defining function or not?

by Eran » Post

The first example should save some memory since the function never becomes declared.
According to the lua docs the garbage collector will free the memory once the function is no longer accessible from lua. So you could also set superComplex to nil after the function is no longer needed and propably achieve the same result.

User avatar
TalkLounge
Member
Posts: 318
Joined: Sun Mar 26, 2017 12:42
GitHub: TalkLounge
In-game: TalkLounge
Location: Germany

Re: Lua: RAM difference, when defining function or not?

by TalkLounge » Post

Yeah, the garbage collector. Completely forgot him. I am to tired for logical thinking.
But the other question is, how does the garbage collector detect these don't needed variables? Black magic, divination or with a AI? It will be always a mystery. I don't even understand my own source code. It works, but why. And the garbage collector knows which variable isn't needed any more. Wow. I want to be a garbage collector, too. :D
Subgames Server: Sky World Subgames German Survival Server: Wildes Land 2 E-Mail: talklounge@yahoo.de

User avatar
duane
Member
Posts: 1693
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: Lua: RAM difference, when defining function or not?

by duane » Post

Believe in people and you don't need to believe anything else.

ShadMOrdre
Member
Posts: 741
Joined: Mon Dec 29, 2014 08:07
GitHub: ShadMOrdre
In-game: shadmordre
Location: USA

Re: Lua: RAM difference, when defining function or not?

by ShadMOrdre » Post

So, I get the garbage collection. That's nice info to know.

If I do something like this:

init.lua

Code: Select all

some_variable = {}
some_variable.some_function = dofile(some_function.lua)
some_function.lua

Code: Select all

local some_local_function = function(param1, param2)

     local return_value
     if param1 then
          if param2 then
               return_value = true
          else
               return false = false
          end
     end
     return return_value
end

return some_local_function
And then to get this value in code, elsewhere:
init.lua for some random code elsewhere

Code: Select all

     some_other_mod = {}
     some_other_mod.some_var = some_variable.some_function(1, 2)
So in the context of the above code, can I load the some_function.lua file, not as a lua file with dofile, but as a text file, using assert(loadstring(text_of_file_as_string))(param1, param2)? If this is true, does this mean that I can subjectively and selectively load and run code, which also provides the smallest the smallest RAM footprint?

On a side note, if the abve is indeed possible, is it then also possible to write lua code to text files and load or reload the lua as runnable code, from say, the world folder, instead of the mod or game folder?

Why would I want to do this? Essentially, to provide a library, and perhaps an API, that can be configured to only provide the functionality desired. A library that can load into memory only the parts of the library actually being used, and subsequently garbage collected, or set to nil otherwise, if no longer used.

MT, being essentially single-threaded, this seems to be a way to provide rich functionality, without the heavy RAM load that some mods impose on the engine. I'm currently building a simple test mod for the Game Dev API project, and what I'd like to achieve is essentially just in time execution of some code, like node registration. If the node data is contained in a .csv file, once the .csv based data had been ran through minetest.register_node, the code for register_node, reading the .csv file, and perhaps other extraneous code could really be unloaded. The .csv loading of the data already trims the amount of lua that the engine has to process at loading, in that there aren't X number of minetest.register_node definitions in the code itself, nor are there node definitions in the lua code that need to be processed, even if through a single function and a lua table. The .csv data exists in a file, the file can be read at will, but the data itself is no longer attached to any executable lua code, and therefore should also trim RAM usage even further.

Does all this make sense? Please, expert lua programmers, teach us better ways to make cool mods?

Shad
MY MODS: lib_ecology lib_materials lib_clouds lib_node_shapes ---- Inspired By: Open Source Virtual World Simulator Opensimulator.

Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

Re: Lua: RAM difference, when defining function or not?

by Eran » Post

It could propably work if used correctly. But be aware that accessing a hard drive or even just a solid state is REALLY slow compared to accessing non-persistent memory. So just in time accessing will only really work if you make some kind of loading screen system.
Using a more selective mod loading system than the default one should already reduce your memory footprint by a bunch though for a big library.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests