[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5 and lua2c
- From: "Leandro Candido" <enclle@...>
- Date: Fri, 5 Dec 2003 12:23:57 -0200
Hello Luiz,
Well, as you know, I'm making a translator yet, not a jit compiler, then
I can't say about jiting (as the question from Florian Berger was about
lua2c, not jit).
But, imagine the following code:
function sum(x,y)
return x + y;
end
print( sum(10,20) ) --> 30
This code has the opcodes ( only names, if someone want to see the
original use luac -l -p in the above code):
CLOSURE, SETGLOBAL, GETGLOBAL, GETGLOBAL, LOADK, LOADK, CALL, CALL,
RETURN, for global part, and ADD, RETURN, RETURN, for function sum.
Imagine we translated the sum function, in my opinion, as it's a simple
function, with 2 working opcodes, we will not gain speed translating to C,
but I will use it to show why in my opinion big functions (with more code)
speedup if translated to C:
The interpreter, for each opcode, do:
loop to get next opcode.
switch the opcode.
test until reach the instruction.
unpack arguments from opcode.
run code for instruction.
Then, the function sum, IN THE CALL will be:
loop to get next instruction (ADD).
walk in switch until OP_ADD "case" body.
run the code for OP_ADD.
loop to get next instruction (RETURN).
walk in switch until OP_RETURN.
run the code for OP_RETURN.
In a translated C function, we add only the "run the code for OP_XX"
part, we don't need to loop, nor walk inside a switch, as when we translated
we already did this inside the translator, this will be, something like:
run the code for OP_ADD.
return OP_RETURN values. -- 1 in this case
Here I don't says about "background" things, like loading values, etc...
But in this simple example we have a gain in speed INSIDE the function,
but to call a C function in Lua (and in any other interpreted language) we
have some overhead, then IN SIMPLE functions we don't have a speed gain,
because of this.
Of course, this is MY OPINION, witch isn't the God's truth ;-). Maybe
I'm wrong, but only when I have a working code I will make real and useful
tests (Well, the code above can be translated, but after the translation I
need to touch the code with my hands, as some things I have'nt created yet).
God bless you,
Leandro.
----- Original Message -----
From: "Luiz Henrique de Figueiredo" <lhf@tecgraf.puc-rio.br>
To: <lua@bazar2.conectiva.com.br>
Sent: Thursday, December 04, 2003 5:32 PM
Subject: Re: Lua 5 and lua2c
> > Of course, native code always beat interpreted code/bytecode.
>
> This is not so clear for Lua because of its extensible semantics. In
> other words, almost every Lua VM instruction can trigger a metamethod.
> So JIT compiling for Lua is not simple.
> --lhf
>