The Implementation of Lua 5.0 Roberto Ierusalimschy Luiz Henrique de Figueiredo Waldemar Celes
the l
ge a u g an
Lua 1
M AIN G OALS
• Portability • ANSI C and C++ • avoid dark corners
• Simplicity • small size
• Efficiency 2
VALUES AND O BJECTS • Values represent all Lua values • Objects represent values that involve memory allocation • strings, tables, functions, heavy userdata, threads
• Representation of Values: tagged unions
typedef union { GCObject *gc; void *p; lua_Number n; int b; } Value;
typedef struct lua_TValue { Value value; int tt } TValue;
3
O BJECTS • Pointed by field GCObject *gc in values • Union with common head:
GCObject *next; lu_byte tt; lu_byte marked • Redundant tag used by GC • Strings are hibrid • Objects from an implementation point of view • Values from a semantics point of view 4
S TRINGS
• Represented with explicit length
• Internalized • save space • save time for comparison/hashing • more expensive when creating strings
5
I MPLEMENTATION OF TABLES • Each table may have two parts, a “hash” part and an “array” part • Example:
{n = 3; 100, 200, 300}
n
100
3
200
nil
300 nil
Header 6
TABLES : H ASH PART
• Hashing with internal lists for collision resolution
• Run a rehash when table is full:
key
value
0
val
nil
key
value
0
val
link
nil
link
nil
→ insert key 4 →
4
val
7
TABLES : H ASH PART (2)
• Avoid secondary collisions, moving old elements when inserting new ones key
value
0
val
link
key
value
0
val
nil
nil
nil
4
val
3
val
4
val
→ insert key 3 →
link
8
TABLES : A RRAY PART
• Problem: how to distribute elements among the two parts of a table? • or: what is the best size for the array?
• Sparse arrays may waste lots of space • A table with a single element at index 10,000 should not have
10,000 elements
9
TABLES : A RRAY PART (2) • How should next table behave when we try to insert index 5? a = {n = 3; 100, 200, 300}; a[5] = 500
n
100
3
200
nil 5
500
nil
300
100 n nil
nil
3
200 300 nil 500 nil
Header Header
nil nil 10
C OMPUTING THE S IZE OF A TABLE • When a table rehashes, it recomputes the size of both its parts
• The array part has size N , where N satisfies the following rules: • N is a power of 2 • the table contains at least N/2 integer keys in the interval [1, N ] • the table has at least one integer key in the interval [N/2 + 1, N ]
• Algorithm is O(n), where n is the total number of elements in the table 11
C OMPUTING THE S IZE OF A TABLE (2)
• Basic algorithm: to build an array where ai is the number of integer keys in the interval (2i−1, 2i ] • array needs only 32 entries
• Easy task, given a fast algorithm to compute ⌊log2 x⌋ • the index of the highest one bit in x
12
C OMPUTING THE S IZE OF A TABLE (3)
• Now, all we have to do is to traverse the array:
total = 0 bestsize = 0 for i=0,32 do if a[i] > 0 then total += a[i] if total >= 2^(i-1) then bestsize = i end end end
13
V IRTUAL M ACHINE
• Most virtual machines use a stack model • heritage from Pascal p-code, followed by Java, etc.
Technical assistance, capacity-building for tracing and stockpile management. 4.2 Votre pays a t-il .... g) Fournir des services de financement h) Fournir des ...
calibre (ALPC) sous tous ses aspects ? a) Nom du .... d'effectuer un contrôle physique au point de livraison ? ... h) Fournir des services d'assurance i) Fournir .... est en cours de preparation pour le marquage des armes de la Force de Defense.
3.1 Votre pays s'est-il doté de lois, règlements ou procédures administratives permettant d'exercer .... f&fromtab;=loi&tri;=dd+as+rank&sql;=dd+=+date'2014-04-.
S.K. Sinha, T. Sundararajan and V.K. Garg. A variable property analysis of alloy solidi- fication using the anisotropic porous medium approach. Int. J. Heat Mass ...
Portable wireless multimedia approaches traditionally achieve ... A coarse-grained reconfigurable computing concept for ... 1: a VLIW view and an accelerator view as an ... orthogonal interconnect-networks. ... solution, we are working on it to repla
The Microsoft . ... standards [14], and implementations for non-Windows platforms already exist [17, 18]. ..... once the Lua API was fully available to C# programs.
Nov 29, 2004 - TTM4700 Teleservices and Networks, Specialization .... APPROACH. ...... Printer is by definition from Columbia Encyclopedia [2] âa computer output device that ..... to view PDF files on the screen, and rather difficult or impossible
Nov 29, 2004 - Intelligent Printing Management (IPM) is a concept related to ...... Another change was the addition of an electrically-controlled clamp to hold the ...... the embedded information will need to be overridden because it does not make se
Statistic classifiers build their predicting models learning by examples. .... Chinese. Even with only 2.2 MB total database, the only significant change was on the full ... characters (whitespace and possibly others) to split a text into tokens (wor
collaboration in SMEs is based on mutual agreements between actors1 to work, ..... sharing and skills acquisition by experience, apprenticeship and training.
are being measured with a reasonable degree of statistical significance. The influence that global emissions have on mercury levels in wildlife must be assessed ...
that generalizes the semantics of C by introducing linguistic constructs for ... design of earlier Cilk systems, but Cilk-5 exploits the prin- ... consult the Cilk-5 manual [8]. .... a solution is found by one of the searches, one wishes to abort ...
The second chapter of the book, The Design and Implementation of the 4.4BSD Operating ... The rest of the bookexplores the concepts introduced in this chapter.
visant à prévenir, maîtriser et éliminer le commerce illicite des armes légères et de petit calibre (ALPC) sous tous ... Ministere des Forces armees/secretaire permanent COMNAT/ALPC Senegal c) Adresse : s/c Ministere ..... armées, etc.) ? - Les armes