Not so long ago, I found out about some parts of Luau that aren’t mentioned a lot. As I dug deeper, I became more and more confused, and basically, I don’t know what to do with my game scripts now.
Basically, from what I know, luau is a very edited version of lua. In fact, it is so edited that you can literally make code that looks like heavily edited C. Let me show you
--!strict
type MyClass = {
X: number;
Y: number;
};
local Class: MyClass = {X = 1; Y = 1;};
print(type(Class)); -- Prints MyClass in Luau compiler
If someone showed me this code five months ago, I wouldn’t say it’s luau. But in fact, it is. And the point is that roblox also supports such interactions (except type() because of limitations) In fact, declaring variables and setting their type actually improves performance. This works for anything, and it especially becomes problematic with functions where you can set what they return since you have to think like a low-level programmer and you need to know what your function actually does return. This is the part that makes luau look more like edited C than lua.
Since I’m making a game that tries to be performant, I really need to know if it’s even worth wasting time adapting scripts to it. And I’m asking people who actually know Luau and not Lua
Evidently from @dthecoolest ’s reply the silly me made a mistake. Please ignore type notation performance ideas. Yes, in some other languages it contributes to runtime improvement, but not here! (dthecoolest provided key info hence I’d mark their answer as the solution)
Yup, I’m a dummy sometimes, sorry!
—————————
The more common use for type checking is intended for intellisense. When you call a method in a type notated class you immediately see the parameters, their types, and return - excellent.
As you said already, that comes at a cost of thinking more like a low-lang programmer, and requires quite some additional writing.
Personally, I only use types for modules that are higher in the hierarchy and used directly. So if I have a camera module, I export the types and define all private and public members, but I don’t do type notation for pretty much any sub-modules like transition module or pepper cam used as a dependency.
If you’re concerned about performance, I’d look into writing a code that is balanced between maintainability and efficiency. Always be careful with demanding loops, mindful with run service events, mindful with memory use and memory leaks, look into Parallel Luau (Parallel Luau | Documentation - Roblox Creator Hub), stick to DRY principle and practical solutions, spend a short while designing a good system and not worry too much about premature optimization.
According to Luau documentation, type definition does not improve performance.
However declaring variables local does slightly.
Type definition does not improve performance evidence from documentation:
Luau compiler currently doesn’t use type information to do further optimizations, however early experiments suggest that we can extract further wins. Because we control the entire stack (unlike e.g. TypeScript where the type information is discarded completely before reaching the VM), we have more flexibility there and can make some tradeoffs during codegen even if the type system isn’t completely sound. For example, it might be reasonable to assume that in presence of known types, we can infer absence of side effects for arithmetic operations and builtins - if the runtime types mismatch due to intentional violation of the type safety through global injection, the code will still be safely sandboxed; this may unlock optimizations such as common subexpression elimination and allocation hoisting without a JIT. This is speculative pending further research
Defining local variables improve performance:
The same optimization is applied to the custom globals declared in the script, although it’s best to avoid these altogether by using locals instead. Still, this means that the difference between function and local function is less pronounced in Luau.