[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua on cell phones
- From: Mike Pall <mikelu-0711@...>
- Date: Sun, 11 Nov 2007 21:22:20 +0100
Roberto Ierusalimschy wrote:
> I do not think I am unbiased enough to present the best arguments.
I guess we need a (completely biased) Lua PR department then? :-)
(no, I'm not volunteering)
Ok, here's my list of arguments why Lua would be a good choice
for a cell phone VM:
- Lua is small: the Lua interpreter easily fits in under 100
kilobytes including the base libraries. Cell phones are often
very memory limited -- every kilobyte saved counts.
It's practically hopeless to try to strip the non-essential
parts from larger runtimes (Python, Java -- you name it)
without severely limiting their functionality (J2ME anyone?).
And you'll still not be able to match Lua's small footprint.
One really cannot afford a "batteries-included" and
all-memory-you-can-eat VM on a cell phone.
Minimal memory use is important even for smartphones with more
generous resource limits. Their CPU caches are usually small
and memory access is rather slow compared to desktop CPUs.
- Lua is fast: independent benchmarks show that Lua is often
amongst the fastest interpreted dynamic languages. The Lua
interpreter has been extensively tuned for high performance.
The call-out mechanism to C is one of the fastest, too (compare
It has been shown that Lua can be compiled to machine code with
a JIT compiler (x86 only right now) for even better performance
at minimal cost (30% larger footprint). Given how underpowered
many cell phone CPUs are, this means that Lua could be used to
supplant even more tasks usually delegated only to systems
programming languages (i.e. C).
[Disclaimer: I wrote LuaJIT.]
- Extending Lua is easy: Lua has been designed to work together
with C. The Lua VM offers a small, yet flexible C API.
Binding to C libraries is the forte of Lua. Often enough a few
lines suffice. Even beginners can write a binding in minutes
without studying huge manuals. There are plenty of automatic
binding generators for larger tasks, too.
Compared to other language's binding mechanisms, Lua's C API
effectively provides a shield between the internals of the Lua
VM and the outer world. One does not have to care about
internal structures of the Lua VM or even the garbage collector
(how many refcounting bugs do your Python bindings have?).
- Embedding Lua is easy: the Lua core uses zero static data. All
C APIs require a single opaque handle which carries along the
complete state of the VM.
Memory use is strictly contained. The Lua core does not leak
memory. Even custom memory allocation functions can be used.
It's easy to use multiple completely independent Lua
interpreters in a single process if there should be a need.
Lua needs only a handful of ANSI C library functions for
operation. And even these can be further reduced. Lua has
excellent portability to even the most restricted embedded
- Lua has an incremental GC: amongst the options for implementing
a garbage collector, an incremental GC is most suitable for
embedded devices. It has low latency, no additional memory cost
and little implementation complexity.
A traditional mark-and-sweep GC may cause lengthy pauses which
are incompatible with semi-real-time requirements (imagine:
"Please hold the line while we're cleaning up -- beep").
Generational GC has a much higher memory overhead and its
heuristic approach to reclamation may hold on to important
resources much longer than tolerable in such an environment.
[It has been debated at length whether GC is an appropriate
mechanism for managing memory in resource constrained devices.
But manual memory management is tedious and error-prone. The
benefits of GC, coupled with modern languages by far outweigh
its disadvantages wrt. average memory use IMHO.]
- Lua has coroutines: multitasking is desirable, even on small
devices. Too often one encounters annoying limits on cell
phones, e.g. one cannot open the calendar or lookup a number in
the phonebook while on the phone with someone. But fully
preemptive multitasking is either not supported or is
considered too costly for these environments.
Lua's coroutines provide a fast and memory efficient way for
non-preemptive multitasking. Lua's coroutines are built-in and
are independent of the capabilities of the underlying OS. Lua
even happily runs on devices without an MMU.
- Lua can be sandboxed: it's easy to restrict access to library
functions on a function-by-function basis. Memory use as well
as CPU usage can be restricted, too.
This is very important for a cell phone environment. You really
want to prevent any random application you've downloaded from
blocking the GUI forever or silently making expensive phone
calls on your behalf.
Removing certain dangerous library functions or (better yet)
just offering a handful of "safe" functions for use by
user-written applications is easy. It's even possible to manage
multiple protection domains with different privilege levels in
the same Lua VM.
Other VMs provide either no support for sandboxing (short of
completely ripping out huge parts of their libraries) or offer
only complicated and inflexible protection mechanisms. Lua can
be sandboxed _in_ Lua and with standard Lua calls, providing
for maximum flexibility at minimum cost.
- Lua is malleable: Lua has just enough syntactic sugar and
meta-mechanisms to be easily repurposed for domain specific
It's nice for (readable!) configuration files or (say) a GUI
layout language. Tasks where you'd otherwise have to design,
implement and teach extra languages (hand-written XML anyone?).
With Lua you have to put in the effort only once.
- Lua is simple, but not simplistic: Lua has a simple, yet
powerful syntax which fits on a single page. The semantics are
consistent and intuitive.
Beginners can start to program right away. Teaching Lua is
easy, excellent programming books are available. No huge and
expensive IDEs and SDKs are needed, any text editor will do.
Lua also has advanced features like closures, coroutines,
meta-mechanism and lots of finely tuned details. All of these
make it attractive for professional developers, too. But none
of these features get in the way of beginners.
The WoW Lua add-ons are a good example of the power of an open
and collaborative environment. It nicely shows what a huge
driving factor user contributed (active) content can be for the
success of a project.
If you want to quickly gain a huge library of gadgets and
add-ons for your cell phone, then Lua is the way to go. You
just have to make it easy for them ...
Ok, this list is by no means exhaustive. But it's a start.