Do required modules cache?

Hello everyone!
Recently, I was working on the project, my items settings have their modules for settings (because that’s easier to work), but I am worried about memory usage.
Do requiring single module affects on perfomance?
How is it going to work with huge module systems, which takes some time to load?

Almost everything you do affects game performance in some way. Requiring modules should be fine, it really depends on what you’re calling in that module.

I suggest reading some posts by people that have the same worry as you.

I don’t know much about this personally, but this is a helpful topic about it I was reading yesterday. There is some discussion around it, so make sure to read all of the replies: Contents of Module Script being modified from a script

I personally have a framework that uses 235 modules I counted using Instance:GetDescendants and then filtering the non-modules using Instance:IsA.

Those are just the visible ones. There is another folder named the shared folder that you can’t even see.

When using this to try and get the time it takes to load:

local main = require(script:WaitForChild("MainModule"))

local initialTime = time()
main:Load()
print(time() - initialTime, time())

It returns 0,0. In other words, if you know how the global time function works, it loads so fast that the server hasn’t even fully set up yet (time() still returns zero). And those modules are not empty, not in the slightest. Then, when changing time with os.clock for proper benchmarking, it took 0.011595382005908 seconds to initialize. Now, I’m sure you can agree that the time it took to initialize is nominal. However, the real time for startup is probably even less, because while the initialization phase requires all the modules and stores them in the framework, it performs other operations.

Technical Details

Now, taking what I have said above, keep in mind that this is Roblox Studio we are talking about. This means the server code is actually running on my local machine. A local machine which is, relatively speaking, not even near top-of-the-line. It’s a refurbished (although I’ve had it for about 6 years) 2013 MacBook Air with a dual-core Intel Core i5 processor. The processor clock speed is 1.3 GHz, or 1 billion and 300 million cycles per second. Despite this, it is still able to do this in such a short time.

Now, as I’m sure you know, the game server the code will actually run on when in the experience is many times faster and stronger than my local machine (probably using one or more XENON processors), so it will execute this code in even less of a time.

Now, the impact on memory usage is dependent on the amount of RAM the client has. As I’m sure you know, when in studio the developer console returns the amount of memory the client is using, the amount the ‘server’ is using, and the amount studio itself is using. In this case, when testing my game, it used 2148 megabytes or 2.148 gigabytes of memory. This may seem like a lot, as the minimum RAM Roblox requires for a device is 1 gigabyte. However, in-game the amount will be closer to 0.5-0.7 gigabytes (for this specific experience). Most of the memory being used is not the client framework but the Roblox engine. In other words, memory should not be a problem.

Now, as for cacheing, it is stated in the documentation for ModuleScripts, " ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require". This is a lengthy way of saying that they cache.

5 Likes

Thanks for the info.
I asked this question, because. I couldn’t find any good way to save already required modules (using _G doesn’t sound really good for me).