Live data from Hacker News

Why Lua?

blog.datamules.com

51–60 of 142 posts

Re: Why Lua?

#51

The more I learn about Lua's design and implementation, the more impressed I am. It's very rare to see software that does so much with so little code. The design is extremely clean and the code is extremely fast. The C API is easy to use and gives good performance, and yet encapsulates enough of the VM's implementation that C modules are source and binary compatible with both Lua and LuaJIT, two very different implem…

Lua has great C integration, but LuaJIT's is even better. You can natively interface with C code without a recompile, just by defining the C function prototype in Lua, and using it like you would any normal lua function. Better still, LuaJIT can frequently inline the calls to C code, so there's no cross-language marshaling penalty. Check out http://luajit.org/ext_ffi_tutorial.html

FFI is extremely cool and impressive, no doubt, but a major downside of it is that you give up memory safety. Once you import it your Lua program can SEGV the interpreter and read/write arbitrary memory in your process. Freedom from memory errors is a major motivation for using high-level languages, so this should not be given up lightly. Also, from a security standpoint it means your Lua is less sandboxed.

Re: Why Lua?

#52
Does anyone know how good is Lua's support for async IO? Especially the handling of large amount of connections and the memory footprint for each connection?

Re: Why Lua?

#53

Not criicizing lua, but worth comparing to guile. > Integration with C (and C++ for that matter) Guile does it better. You can use shared memory threads in guile without any penalty. Atmost you have to allow for the garbage collector to run when inside FFI functions. But that is a small price to pay in case you need to use multiple parallel-concurrent threads with a single heap. Guile was built with FFI in mind and h…

Not sure if I'd agree that Guile's FFI bindings are better than LuaJIT's FFI. It's certainly easier to declare a function in LuaJIT - you can take the definition directly from the header file, rather than transcoding it into s-exps.

Compare:

  (define memcpy
	       (let ((this (dynamic-link)))
	         (pointer->procedure '*
	                             (dynamic-func "memcpy" this)
	                             (list '* '* size_t))))
vs.

  ffi.cdef [[
	void * memcpy ( void * destination, const void * source, size_t num );
  ]]
You also get the ability to add metamethods and finalizers to returned C objects (so you can use Lua's built-in GC to clean up after, for example, your FILE*).

As for the speed, there's no reason that a lot of dynamic languages couldn't be as fast as LuaJIT. But none of them are even close. I wish LuaJIT was still up in the computer language shootout. The LuaJIT interpreter (with JIT turned off) is 3x faster than V8 on the PC, and faster than Java on Android. And that's the interpreter - the JITed code is way faster.

Of course, macros are awesome, and a huge point in Guile's favor. On the plus side, Lua is very easy to understand, especially if you're coming from Javascript.

Re: Why Lua?

#54
post #47
post #6

Why isn't Lua more widely used? One reason is a consequence of it being an embedded language. Lua has had 5 major versions which are very incompatible with each other. You're just supposed to stick with the previous version until you upgrade your code. I read all the Lua papers, and they are quite fond of "getting it right" (which is refreshing). They will break things to get it right, whereas other languages stick w…

Why isn't Lua more widely used? I think the major, overwhelming reason is because it has no standard library, much less a package-ecosystem such as RubyGems or Python eggs.

You do know about LuaRocks, right? It's not as robust or vibrant as the Ruby or Python equivalent, but it certainly does exist.

http://luarocks.org/repositories/rocks/

Re: Why Lua?

#55

Earlier quoted context omitted.

Lua has great C integration, but LuaJIT's is even better. You can natively interface with C code without a recompile, just by defining the C function prototype in Lua, and using it like you would any normal lua function. Better still, LuaJIT can frequently inline the calls to C code, so there's no cross-language marshaling penalty. Check out http://luajit.org/ext_ffi_tutorial.html

FFI is extremely cool and impressive, no doubt, but a major downside of it is that you give up memory safety. Once you import it your Lua program can SEGV the interpreter and read/write arbitrary memory in your process. Freedom from memory errors is a major motivation for using high-level languages, so this should not be given up lightly. Also, from a security standpoint it means your Lua is less sandboxed.

Yes, but you use it to interface with native code. Normally, you'd write Lua_Cfunctions to achieve the same behavior - and those can crash your app just as easily, since they're native code. Perhaps more so, since it's easy to screw up the stack manipulation.

Re: Why Lua?

#56
post #47
post #6

Why isn't Lua more widely used? One reason is a consequence of it being an embedded language. Lua has had 5 major versions which are very incompatible with each other. You're just supposed to stick with the previous version until you upgrade your code. I read all the Lua papers, and they are quite fond of "getting it right" (which is refreshing). They will break things to get it right, whereas other languages stick w…

Why isn't Lua more widely used? I think the major, overwhelming reason is because it has no standard library, much less a package-ecosystem such as RubyGems or Python eggs.

The part about the package ecosystem is not true. Check out luarocks [1]. It's not as well developed but it's certainly there.

1: http://luarocks.org/

Re: Why Lua?

#57

Earlier quoted context omitted.

__index and __newindex are the key ones - effectively equivalent to method_missing in Ruby. __index gets called if you try to read a non-existent key, and __newindex gets called if you try to write to a non-existing key. You can use these methods to implement inheritance, proxies, getters/setters, all sorts of stuff.

This sounds very much like Python's __getattribute__ [0] method. Is there a difference I'm not understanding? [0] http://docs.python.org/reference/datamodel.html#object.__get...

I'm not that familiar with Python, but can you use __getattribute__ to provide methods as well? Using __index is the way you typically implement inheritance in Lua.

Re: Why Lua?

#58
post #47

Earlier quoted context omitted.

Why isn't Lua more widely used? I think the major, overwhelming reason is because it has no standard library, much less a package-ecosystem such as RubyGems or Python eggs.

You do know about LuaRocks, right? It's not as robust or vibrant as the Ruby or Python equivalent, but it certainly does exist. http://luarocks.org/repositories/rocks/

Ah, I did in fact not know about it, thanks for the pointer!

Maybe there's still hope! ;-)

Re: Why Lua?

#59

Not criicizing lua, but worth comparing to guile. > Integration with C (and C++ for that matter) Guile does it better. You can use shared memory threads in guile without any penalty. Atmost you have to allow for the garbage collector to run when inside FFI functions. But that is a small price to pay in case you need to use multiple parallel-concurrent threads with a single heap. Guile was built with FFI in mind and h…

> And guile offers all of this while supporting native threading with a single heap.

What is Guile's synchronization model for shared-state multithreading?

> Guile is available on n900. So there.

This appears to be a Linux-based phone, so that's not very surprising or impressive. Lua is straight ANSI C and can compile on much more limited systems than a Linux-based smartphone.

One other data point: Guile is 5x the size of Lua, in both source and binary forms.

> To continue, guile has continuations (delimited or otherwise), and macros (hygienic or otherwise), both of which are effectively missing in lua.

One of the authors of Lua made an interesting point in his slides describing Lua 5.2: if you think "goto" is evil, continuations are much worse. And yet it's considered "cool" to support continuations. http://www.inf.puc-rio.br/~roberto/talks/novelties-5.2.pdf

Re: Why Lua?

#60
post #21

Earlier quoted context omitted.

Tcl is an interesting little language as well, and the niche it was created for is similar to the one Lua was created for. The biggest advantages that Lua has over Tcl, off the top of my head, are speed and lambdas. Tcl's semantics make it very hard to optimize because it is stringly typed (with some bytecode and value specialization in the background, but with conversions between strings and other values as needed).…

Tcl has lambdas (anonymous functions): http://wiki.tcl.tk/4884 . As of 8.6 (currently in beta), it also has coroutines, and has a non-recursive engine (stackless) among other niceties.

Shows how long it's been since I last used Tcl!

Still, Tcl's anonymous functions aren't quite what many people consider to be lambdas:

    % set inc {x {expr $x + 1}}
    % apply $inc 1
    2
    % apply $inc 1
    2
    % $inc 1
    invalid command name "x {expr $x + 1}"
    % inc 1
    1
This works fine with `apply`, but if you treat it like a normal function, it does some strange things. Conversely, functions that are defined with `proc` aren't compatible with `apply` unless you wrap them up.

I'm not saying it's inadequate, just that the anonymous functions don't quite work how you expect lambdas to work.

Post reply on HN