A simple question I cannot find an answer to anywhere but find quite important to know.
Does Roblox internally re-use or de-duplicate code?
It always seemed horribly ineffecient to me to copy-paste scripts into everything because eventually you’ll have to edit every copy of the script, so naturally you’d typically place a script into a singular object that gets cloned so that doesn’t become an issue.
I’m not sure if there’s a way to profile or check but but I’ve always had the suspicion that having 100 NPCs walking around with animation and AI scripts in them, you’d use 100x the memory that all that code would take up.
I’m not speaking of the memory that variables would take up but rather the actual raw source (or compiled) code itself, the Lua instructions.
Does Roblox internally de-duplicate or re-use identical code in scripts or does every script truly have it’s own copy of it’s source code and instructions?
The reason I want to know is mainly for optimization reasons really but also curiosity and whether it’s something I should worry about in case I have a ton of entities, objects and items.
I would’ve guessed it would work like that while in studio.
However, what about runtime script cloning?
If I were to Clone() a script at runtime, it’s contents will be 100% identical.
The only thing that would change practically is the script keyword referencing a different script instance.
Wouldn’t it make sense to de-duplicate something that was cloned during game run?
Like I said, that would create unwanted behavior; you cannot optimize a highly dynamic system.
The same reason goes for getfenv and setfenv; when the environment is modified, all optimizations are disabled because now you cannot exactly know what is where.
Oh I see now, that makes sense.
I assume by that logic using module scripts would also just be a lot more optimized since it essentially manually “de-duplicates” code by reusing the same function and environment.
Exactly like I’ve been thinking for years pretty much.
Thanks for the answer though, I like the more in-depth stuff like this that the engine documentations don’t tell you about.