Also, keep in mind that the function you are calling might be expensive. The optimization I outlined above only helps you in large, tight loops. In the vast majority of cases, it won’t improve performance at all.
#Lua c script executor roblox code#
OK, now you’re ready to modify all your code and make it faster, right? Not so fast! Here is what happens under the hood, if you don’t declare the variable as local: We nearly wiped out the gain made in our optimization. This is because global variables are themselves syntactic sugar, for _G. My benchmark revealed that using a global variable cut the speed boost from 33% to 20%. What if some other code uses a global “setCell” variable? Our definition will overwrite it! When in doubt, use the “local” keyword. Local variables don’t clobber global variables.Whenever you do something clever that is more complicated than a more elegant expression, be sure to explain why.Īnother thing to note: I very consciously declared the “setCell” variable as a local variable. Notice how I put some comments in the code, including an invariant, which states an assumption I’m making about tCell being immutable. Now we do the ‘SetCell’ lookup only once, and call SetCell 10,000 times. We can safely assume that the definition of the SetCell function is the same each time, right? All we want to do is call the same function 10,000 times with a different set of arguments. Think back to the “syntactic sugar” above.
![lua c script executor roblox lua c script executor roblox](https://i.ytimg.com/vi/c97s1Ea3iK0/maxresdefault.jpg)
There must be something we can do to make this more efficient. This code calls SetCell 10,000 times, which means it does 10,000 lookups of the SetCell member function. I ran some benchmarks and found that it can take up 33% of the script’s execution time. It takes more time than a function lookup in many other languages like C++, Java or C#. Then, the script calls that function with terrain as the first argument. First, it looks up a function named ‘SetCell’ in terrain’s internal list of members. By pulling out the sugar, we see that in a member function call the script actually does two things. Syntactic sugar makes code easier to read, but it can hide some of the stuff that happens under the hood. I’ll also discuss the pro’s and con’s of code optimization. This guide is intended for more advanced scripting.
![lua c script executor roblox lua c script executor roblox](https://i.gyazo.com/30969fa06e1277c47b2d7fe28571d39e.png)
Today, we will show you a way to make some Lua code faster. Roblox Lua C Apoc ScriptĪny programming language has its little tricks for squeezing out better performance. Today, Chief Scientist at ROBLOX, Erik Cassel, discusses a way to optimize Lua. World of Warcraft, Angry Birds and Mafia II all use Lua script. Lua is a powerful, lightweight, scripting language, and is often used in video games due to fast execution and a short learning curve.