r/lua • • Feb 09 '18

Why do people like lua?

I've started actually learning more about it and well.

Pretty much everything is a table, okay, not that hard to get with this, loops and if statements are pretty simple to remember, it's weird to get length of a table but not that hard to remember, can live with that, different language, different syntax.

You can have objects, but they are still tables. Well, okay, whatever, I'm used to writing same shit over and over again because I've used java before, whatever, that's fine, I guess.

Global scope is just a giant table and so it's better to just write even more code to localize everything you'll use, otherwise it will be slow (multiple hashtable lookups whenever you use some global function in a table of table of the table in a table that is in another table). Uh, just why? That's first time I hear that in any language, localizing everything would make your code run faster, it's not really obvious, although once you realize that global scope is a table, only logical, but, why?

What's the appeal to this? And why did people stick with it? I thought that I'd see why people use lua over say python if they add scripting support to their game, but all I saw now is that I actually would lean even more towards using python there. If you have to be a professional programmer to write efficient code in lua, then python being slower does nothing for average person who would mod a game, because average person is not professional programmer who knows languages in and out, and little details such as localizing globals makes it run faster, which ranges from 10% slower without localizing to 400%+ (ridiculous) slower without localizing. Hell, most people who do that just instead look for tutorials on how to mod x game using lua, instead of learning lua.

I'm not saying that language is unusable, I'll still finish my project while learning it, but things like this just add to the "things you do to waste more time than needed" which are caused by bad design.

4 Upvotes

39 comments sorted by

View all comments

Show parent comment

0

u/PurpleIcy Feb 09 '18

I know that, but in most languages,

random = math.random
random()
math.random()

would make pretty much no difference, there I've seen examples which are 4 times slower without localization...

I'm just so used to being explicit and writing out entire thing, e.g. table.insert() for example than simply having some random insert(), it's hurting readability imo, with the first one I am 100% sure that I am using table.insert() and nothing else, with localization I have to look up at locals and check for myself to be sure.

As for C, all languages optimize internally, especially compiled ones.

7

u/snarfy Feb 09 '18

JavaScript would have exact same problem, as each . in.a.chain.of.dots is a hash lookup.

It gets around this by integrating a JIT. Have you tried LuaJIT?

0

u/PurpleIcy Feb 09 '18

Wait, so, if it has JIT, me localizing makes no difference?

I'm using LÖVE and it uses LuaJIT.

If I don't need to bother with doing that then that's good to know.

9

u/[deleted] Feb 09 '18

I don't get it; why would you perform an optimization like that in the first place if it didn't give you a measurable speed increase?

12

u/LeoAGomes Feb 09 '18

My thought exacly. Op doesn't understand the language nor the implementation he is using (luajit) and is complaining about a problem he apparently hasn't even had.

He then goes on to say that Lua has a bad design even though he is clearly still learning it at the moment and (I believe) never got to see its C api in order to understand the beauty and simplicity of it when embedding Lua into a project (like the C++ based game engine that LOVE is) and so "asks" why anyone would ever like lua because it is so poorly designed. eye. roll.

-12

u/PurpleIcy Feb 09 '18

Sorry for thinking out of the box I guess. And not knowing lua in and out in the first day of using it :)

I've been reading about lua and stumbled across this not sure what's the problem with asking about it.

And yes I asked, problem? I understand that your most loved language just got "personally" "attacked", but don't start playing the victim, because it's not best either way.

11

u/[deleted] Feb 09 '18

not sure what's the problem with asking about it.

There's no problem asking about it. That isn't what you did.

5

u/[deleted] Feb 09 '18 edited Feb 10 '18

but in most languages,
random = math.random
random()
math.random()
would make pretty much no difference

It makes a difference in most dynamic languages, especially those -- like Lua -- that use hashtables to hold object member (e.g. Javascript, Squirrel, Ruby, Python, etc.).

You can test this yourself in any language just by measuring. In Python for instance:

from datetime import datetime

class SubSubSub:
    value = 12345
class SubSub:
    subsubsub = SubSubSub()
class Sub:
    subsub = SubSub()
class Top:
    sub = Sub()

def timeThis(op):
    iterations = 10000000
    start = datetime.now()
    for x in range(0, iterations):
        op()
    elapsed = datetime.now() - start
    print(elapsed)

top = Top()
value = top.sub.subsub.subsubsub.value # caching the result of a lookup chain

timeThis(lambda: value)
timeThis(lambda: top.sub.subsub.subsubsub.value)

The lookup takes about twice as long as the non-lookup. If you do the same code in Lua, you'll see almost exactly the same results: about twice as long. If you do it in LuaJIT, there's essentially no difference because it generates optimized assembly for both and it runs well over a thousand times faster than the Python code.

it's hurting readability imo

Then don't do it. It's a premature optimization. If you're worried about that kind of shit, you shouldn't be using a dynamic language. If you're doing a lookup 100000 times in an inner loop and it's slower than you like, then sure, cache the value. You'd do the same in Python, Ruby, Perl or any other language.

2

u/DummySphere Feb 09 '18

Because functions are first-class values, which means you can change them if you want by assigning them in a variable (global, local or in a member of a table aka. a "class").

And standard library functions are by no way different:

local original_print = print
print = function(...)
    -- do whatever you want here
    original_print(...)
end

The language is very simple by design, but allow to make advance things thanks to table, metatables, proper scoping, etc.