r/lua • u/Ok_Hovercraft364 • Jan 16 '26
Lua Gotchas
Hi folks, I've been programming with Python for about 2 years now.
My coworker suggested that start embedding Lua into my projects to speed up the slower parts(I think he is getting annoyed by some of the slowness lol). Anyone know something that might bite me in the butt that is easy to overlook? From what I understand, I can install Lupa and get the runtime like that. He told me to use a bunch of tables. Is that correct for the most part?
16
Upvotes
11
u/VeronikaKerman Jan 16 '26
Why would you speed up one scripting language with another scripting language? Is your app in python? I assume you already done performance profiling. Have a look at "cython" and other similar to it.
11
u/pomme_de_yeet Jan 17 '26
probably the biggest gotcha is how nil works in tables, and how that affects array length (arrays are just tables). Any arrays with nils in them are sparse arrays, and makes the length operator
#inaccurate, so you should store the length separately if you need it, or avoid nil in arrays4
u/mtbdork Jan 16 '26
That is correct. Tables in Lua are very generalized. Throw some functions into a table in Lua and you’ll quickly learn how to”methods” work. Metatables and metamethods in Lua are more limited than Python, but they do exist.
4
u/activeXdiamond Jan 17 '26
1 indexing.
Nil in tables, combined with ipairs and the length operator.
Version differences. There are minor differences between 5.1/5.2/5.3/5.4 that might bite you. Some stuff is obvious and will just crash. But others, not so. A great example of this is the __len metamethod and the __ipairs/__pairs metamethods being different (or not existing) in some versions.
Also, if you can use LuaJIT instead of the standard Lua interpreter, definitely do. It is much, MUCH faster. It runs on many platforms, but not as many as the PUC Rio interpreter. Also, it's JITting abilities are not available on all platforms that it runs on (for example, iOS), but even then, it's still faster than PUC Rio (though, obviously not nearly as big of a difference as with JIT compilation enabled).
4
u/likethevegetable Jan 16 '26
Indexing at 1. Expecting errors when indexing with un-used index but instead getting nil.
A big change from Python is scoping policy: if you declare/assign a variable inside a function, its scope is global and you should use "local" for python-like behavior.
Weird but quirk I like: concating numbers with strings.
2
u/Just_a_Thif Jan 17 '26
I always link this, it's basically a rundown of every lua quirk you need to know
After u go through it and get a general idea, just get started and do research as you go
https://www.youtube.com/watch?v=CuWfgiwI73Q
2
u/Daniikk1012 Jan 17 '26 edited Jan 17 '26
What got me personally when I was learning Lua:
Treat
nilnot really likeNone, but instead an actually absent value. All variables, functions arguments, and fields have this value by default, so in some places where python errors (Trying to read variable/field that doesn't exist, or accesing an argument that wasn't passed to the function), lua silently gives younil. Similarly, assigning tonilis how you delete a field.The multiple assignment syntax is not tuple destructuring, but actual multiple return (Like in Go I believe, though I never used Go). Places that expect one value (Most places) will use only the first one of the multiple returned values.
It's 1-indexed, so better to get used to working with inclusive ranges, rather than ranges with exclusive upper bound.
2
u/indolering Jan 20 '26
Nothing more true has ever been spoken of Lua :D
1
u/xoner2 Jan 20 '26
Shouldn't there be an article or essay or booklet about this topic? Similar to JavaScript the Good Parts, but in reverse...
1
u/TheSplashsky Jan 23 '26
Only reasonable way to make that choice.
1
u/appgurueu Jan 17 '26
I'm just gonna leave this blog post here: https://luatic.dev/posts/2025-04-12-lua-misconceptions
Using Lua to try to speed up Python programs is certainly an.. interesting choice. I feel like a systems programming language would be a more natural choice. Many Python libraries that care about performance are implemented in C, for example. But you can also use something more modern, of course.
The difference between a fast Lua implementation and a fast Python implementation need not be that large, and you will be adding significant overhead for converting things back and forth.
0
u/[deleted] Jan 17 '26
[deleted] — view deleted comment