InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Lua: Embeddable Scripting Engine, LuaJIT 2.0 Includes New High-Performance VM

Posted by Abel Avram on Jan 22, 2010

Sections
Development
Topics
Language ,
Scripting ,
Architecture ,
Compilers
Tags
Lua ,
Game ,
Virtual Machines

Lua is a scripting language that can be embedded in any application that can provide hooks for its C API. Version 2.0 of its JIT compiler is in Beta and includes a completely re-written VM based on a trace compiler and an interpreter written in assembler.

Lua means Moon in Portuguese, and it was released internally by a group of three professors, namely Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo, back in 1993. Since then, the language has passed through a number of versions, the latest being Lua 5.1.4, released in 2008.

Lua is a general purpose embeddable scripting language with a syntax borrowed from Modula, with some influences from SNOBOL, AWK, C++, and Lisp. Scheme has also influenced the language over time. The data-description syntax came from SOL (Portuguese for 'Sun'), a language created by Ierusalimschy and later replaced by Lua. Other characteristics of the language are: functional, object-oriented through extensions, prototype-based and garbage collected.

Lua has a C API allowing it to be easily embedded in applications written in various languages.  The API is divided in a core API and an extension library. The binary package is extremely small at 200-700 KB depending on the platform. Lua runs on any operating system that has an ANSI or ISO C compiler. Practically, it runs on all flavors of Unix/Linux, on Windows, but also on handheld devices with ARM or Rabbit microprocessors/controllers.

One important feature is the table data type, the only complex data type but one that can be extended. A table is:

a collection of key and data pairs (known also as hashed heterogeneous associative array), where the data is referenced by key. The key (index) can be of any data type except nil. An integer key of 1 is considered distinct from a string key of "1".

Lua does not have the concept of a class, but they can be created through extensions using functions and tables:

By placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented via the "metatable" mechanism, telling the object to lookup nonexistent methods and fields in parent object(s).

A simple example of Lua code looks like this:

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

Unlike other scripting languages, Lua is not interpreted directly but rather compiled into bytecode that runs on its own register-based virtual machine. Michael Pall, an open source developer from Germany, has created a JIT compiler for Lua, version 2.0 currently being in Beta 2. Pall has created the JIT for the x86 architecture, and a porting to the 64-bit architecture is in the works. Its author commented on LuaJIT:

It combines high flexibility with high performance and an unmatched low memory footprint: less than 120K for the VM plus less than 80K for the JIT compiler. …

2009 also marks the first release of the long-awaited LuaJIT 2.0. The whole VM has been rewritten from the ground up and relentlessly optimized for performance. It combines a high-speed interpreter, written in assembler, with a state-of-the-art JIT compiler.

An innovative trace compiler is integrated with advanced, SSA-based optimizations and a highly tuned code generation backend. This allows a substantial reduction of the overhead associated with dynamic language features. It's destined to break into the performance range traditionally reserved for offline, static language compilers.

While Lua can be embedded in all sorts of applications, it has found more acceptance among game creators. The main reason besides its performance is the ability for a game developer to interrupt the game and tweak various parameters then to continue the game without actually stopping it to perform a rebuild. Games like World of Warcraft, League of Legends, the Star Wars series, and dozens of others have made use of Lua during development. There are also other types of applications using Lua or instrumented to use it, with examples including Adobe Photoshop Lightroom, Apache HTTP Server (through module mod_lua), MySQL Proxy, nmap, and many others. There are also at least two web frameworks written in Lua: Kepler, and Orbit.

No comments

Watch Thread Reply

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.