Question regarding "require"

I have a question regarding the handling of requiring a file:

I have a module with a function, like this

local mod={} local createStuff = function()           local decide=math.random(1,2)      local theFile      if decide==1 then            theFile=require ("infofiles.randomfile\_2")      else            theFile=require ("infofiles.randomfile")      end end mod.createStuff = createStuff return mod

I require the module in a scene, then call createStuff() inside this scene.

Now I wonder if the code above in the modules function is okay and if the local required theFile is correct regarding memory handling? Is there anything negative about doing it like this?

Thanks for your help!

I’m not an expert programmer yet but I suggest you should requiere the module at top of the module you need the required module. In the required module you set variables, functions, etc to use in you code. Maybe some experience programmer have a better answer. Nice day!

I’m reading this tutorial right now. 

I normally would require it on top but because I want a random module loaded and later (with a scene change) getting it removed from memory, so there can be a “new” random one loaded the next time the function is called.

Thanks!

your reference to the code “theFile” will be removed, but code itself loaded by require() will remain.

in fact, you’re likely consuming a bit more memory by having this extra “mod” rather than just calling “randomfile” directly (from wherever “mod” would have been called from)

THank you! I have really huge files to handle  with “randomfile” and “randomfile_2” and so on. That’s why I wanted to do it this way… to not have a very very huge file, but can split it up into different files and then only require the one I need.

Is there a better way to access different randomfiles randomly with each call of createStuff?

Take  a look here

Thanks!

 

what’s your definition of “very very huge”?  any “ordinary sized” source file could easily have 1000 lines.  so, i’d expect a “huge” one to imply at least an order of magnitude bigger, perhaps 10k lines.  then a “very huge” one would imply 100k lines, then a “very very huge” one would imply 1M lines.

is that roughly the scale of what you’re talking about?

if so, then maybe it’s worth mucking about with package.loaded, otherwise just do something like:

local allModules = { require("random1"), require("random2"), require("random3"), -- etc } local oneModule = allModules[math.random(#allModules)] oneModule:createStuff()

@d.mach,

FYI, SSK is an example of a ‘super module’.  That is, it requires several dozen modules once and accumulates them into a single semi-deep table/module hierarchy that is then exposed as a global.

The module (when fully loaded; and you can control this to reduce it) is about 500KB of memory.

This is NOT efficient in some regards, but from an accessibility and usability viewpoint it greatly reduces the complexity of collecting the varied functionality in SSK.

I then alleviate some of the ‘lookup costs’ associated with putting all this functionality in a single deep hierarchy by localizing functions/and features I use most.

For example, if I’m using the math2d sub-module in a game file, I put this at the top:

-- -- SSK 2D Math Library local addVec = ssk.math2d.add;local subVec = ssk.math2d.sub;local diffVec = ssk.math2d.diff local lenVec = ssk.math2d.length;local len2Vec = ssk.math2d.length2; local normVec = ssk.math2d.normalize;local vector2Angle = ssk.math2d.vector2Angle local angle2Vector = ssk.math2d.angle2Vector;local scaleVec = ssk.math2d.scale

Now, instead of this:

local vec = ssk.math2d.angle2Vector( 45 )

I can type this:

local vec = angle2Vector( 45 )

Not only is this easier to type, but by localizing the function I’ve reduced the lookup time to use it repeatedly.

 

dave, your approach is nice but will load ALL random files at the same time. You just call a random method after, but all are in memory.

 

I don’t think its what d.mach wants since in his code he only loads one at a time.

hes method will work fine…but i think he misses a “return theFile” to work properly and if he wants to “unload” the require file, just use package.loaded[“random1”] = nil

I know, that was my point:  unless the OP truly has “very very huge” files, then the topic isn’t even worth worrying about - just require all the files at startup, use any one at random during runtime, and just be done with it.

Otherwise you’re going to an awful lot of trouble (and incurring run-time load penalties as well), all for a not-actually-demonstrated assumed need to reduce code size.  The OP code, if run repeatedly on each scene change (as OP stated it would be) would have eventually loaded all modules ANYWAY as written.

Thanks for all your help and feedback. I guess it is not huge enough to get through all the trouble.

I also wanted to improve performance regarding loading time of scenes… but it seems it is something else which is making this loooooong.

@davebollinger, following your thinking, i would go for 1 file rather than 3 files since they are little.

and calling just the function you need inside that file.

local random\_number=require ("random") -- one file only local number=random\_number["random"..math.random(3)]() -- substitute the number with the number of functions you have

in this example the functions inside random.lua should be something like this:

local m={} function m.random1() local random\_number=math.random(10) return random\_number end function m.random2() local random\_number=math.random(100)+30 return random\_number end function m.random3() local random\_number=math.random(30)+50 return random\_number end return m

saying this…with only 1 file and only 1 function I could accomplish the same thing i did with 3 functions. Using parameters to that function. I don’t know whats inside “randomfile” and “randomfile_2” are so i can’t  conclude that will work in d.mach case.

I’m not an expert programmer yet but I suggest you should requiere the module at top of the module you need the required module. In the required module you set variables, functions, etc to use in you code. Maybe some experience programmer have a better answer. Nice day!

I’m reading this tutorial right now. 

I normally would require it on top but because I want a random module loaded and later (with a scene change) getting it removed from memory, so there can be a “new” random one loaded the next time the function is called.

Thanks!

your reference to the code “theFile” will be removed, but code itself loaded by require() will remain.

in fact, you’re likely consuming a bit more memory by having this extra “mod” rather than just calling “randomfile” directly (from wherever “mod” would have been called from)

THank you! I have really huge files to handle  with “randomfile” and “randomfile_2” and so on. That’s why I wanted to do it this way… to not have a very very huge file, but can split it up into different files and then only require the one I need.

Is there a better way to access different randomfiles randomly with each call of createStuff?

Take  a look here

Thanks!

 

what’s your definition of “very very huge”?  any “ordinary sized” source file could easily have 1000 lines.  so, i’d expect a “huge” one to imply at least an order of magnitude bigger, perhaps 10k lines.  then a “very huge” one would imply 100k lines, then a “very very huge” one would imply 1M lines.

is that roughly the scale of what you’re talking about?

if so, then maybe it’s worth mucking about with package.loaded, otherwise just do something like:

local allModules = { require("random1"), require("random2"), require("random3"), -- etc } local oneModule = allModules[math.random(#allModules)] oneModule:createStuff()