Juan Manuel Alvarez | 29 Sep 18:57

Executing a script multiple times

Hi everyone! I am new to LUA and I have a little question.

I am trying to execute a script multiple times.
I first called luaL_loadbuffer one time and lua_pcall two times. The
second time calling lua_pcall I received an error message. I start
looking and I found something in the documentation about pcall: "Then
the program calls lua_pcall, which pops the chunk from the stack and
runs it in protected mode", so I finally discovered what my error was
=o)

Now I have some questions: How much does luaL_loadbuffer affect
performance? Is there any way to precompile a script from the C API
and call it multiple times? (since I only found a way to precompile it
from command-line).

Thanks in advance!

Juan M.

James Dennett | 29 Sep 19:06

Re: Executing a script multiple times

On Mon, Sep 29, 2008 at 9:57 AM, Juan Manuel Alvarez <naicigam <at> gmail.com> wrote:
> Hi everyone! I am new to LUA and I have a little question.
>
> I am trying to execute a script multiple times.
> I first called luaL_loadbuffer one time and lua_pcall two times. The
> second time calling lua_pcall I received an error message. I start
> looking and I found something in the documentation about pcall: "Then
> the program calls lua_pcall, which pops the chunk from the stack and
> runs it in protected mode", so I finally discovered what my error was
> =o)
>
> Now I have some questions: How much does luaL_loadbuffer affect
> performance? Is there any way to precompile a script from the C API
> and call it multiple times? (since I only found a way to precompile it
> from command-line).

Store the loaded chunked in a variable (i.e., make it a named
function) and then call that function repeatedly.

-- James

Thomas Harning | 29 Sep 19:07
Gravatar

Re: Executing a script multiple times

On Sep 29, 2008, at 12:57 PM, Juan Manuel Alvarez wrote:
> ....
> looking and I found something in the documentation about pcall: "Then
> the program calls lua_pcall, which pops the chunk from the stack and
> runs it in protected mode", so I finally discovered what my error was
> =o)
>
> Now I have some questions: How much does luaL_loadbuffer affect
> performance? Is there any way to precompile a script from the C API
> and call it multiple times? (since I only found a way to precompile it
> from command-line).

luaL_loadbuffer is relatively expensive to the act of calling it's  
data, particularly when it is over uncompiled code since it must  
reparse the text each time.

You could do a lua_pushvalue to copy the chunk before you pcall it...

Ex:

luaL_loadbuffer(your chunk data)
for each time you want to call up your chunk
	lua_pushvalue(L, -1)  // copy it
	for each arg
		lua_push*
	lua_pcall(...)
	// handle values...
lua_pop(1)  // remove your chunk from the stack

(Continue reading)

Re: Executing a script multiple times

>I found something in the documentation about pcall: "Then
>the program calls lua_pcall, which pops the chunk from the stack and
>runs it in protected mode", so I finally discovered what my error was
>=o)

The best way to do this would probably be to not lose it after lua_pcall. Do you really want to recompile every
time? If not, you could do something like this (semi-pseudocode):

luaL_loadbuffer(...)

/* Check for error conditions */
/*...*/

/* Make another reference to the function on the stack */
/* for x = 1 to n ... */
lua_pushvalue(-1); 
lua_pcall(...);

This should allow you to call lua_pcall as many times as you want without having to recompile it.

That being said, recompilation is only as expensive if it needs to be. Code first, profile and optimize
after. I wouldn't recommend it, but recompiling is certainly a valid option if it's not too expensive for
your needs.

Regards,
-- Matthew P. Del Buono

Alex Davies | 29 Sep 20:15
Favicon

Re: Executing a script multiple times

Matthew Paul Del Buono wrote:
> That being said, recompilation is only as expensive if it needs to be. 
> Code first, profile and optimize after. I wouldn't recommend it, but 
> recompiling is certainly a valid option if it's not too expensive for your 
> needs.

I respectfully disagree on this note. lua_pushvalue isn't an optimization in 
this case, but moreso correct practice. Imo, to recompile it each time 
violates that least astonishment law, along with wasting time and memory.

So I believe the only-optimize-bottlenecks doesn't apply. Still, probably 
debateable. :)

- Alex 

Stefan Sandberg | 29 Sep 20:56

Re: Executing a script multiple times

Hum? I think the logic implied was to amortize your 'waste of time and 
memory', with 'I don't really care' and get a win from less effort spent 
over time (which often means money, or time to spend on something else)

Don't really see how least astonishment principle fits in here, that 
basically says 'don't do stupid stuff that can confuse people'..

Whether ignoring a time consuming optimization or not is deemed 'correct 
practice' or not isn't really anything to discuss here, since it will 
never end, but I wouldn't call recompiling 'wrong' in any way..

Alex Davies wrote:
> Matthew Paul Del Buono wrote:
>> That being said, recompilation is only as expensive if it needs to 
>> be. Code first, profile and optimize after. I wouldn't recommend it, 
>> but recompiling is certainly a valid option if it's not too expensive 
>> for your needs.
>
> I respectfully disagree on this note. lua_pushvalue isn't an 
> optimization in this case, but moreso correct practice. Imo, to 
> recompile it each time violates that least astonishment law, along 
> with wasting time and memory.
>
> So I believe the only-optimize-bottlenecks doesn't apply. Still, 
> probably debateable. :)
>
> - Alex

Juan Manuel Alvarez | 29 Sep 21:26

Re: Executing a script multiple times

Thanks for all the quick replies!

I could make it work using lua_pushvalue(-1), but I am still having
problems with parameters.
I am doing the operations in the following order:

luaL_loadbuffer

lua_pushvalue(-1)
lua_pcall // to load the global table

lua_getglobal
lua_pushnumber // parameter 1
lua_pushnumber // parameter 2

lua_pushvalue(-1)
lua_pcall

The last pcall, gives me the error "attempt to call a number value".

The same sequence without the parameters works fine for me. Can you
please point me in the right direction?

Thanks in advance!

Juan M.

On Mon, Sep 29, 2008 at 2:11 PM, Matthew Paul Del Buono
<delbu9c1 <at> erau.edu> wrote:
>>I found something in the documentation about pcall: "Then
(Continue reading)

Thomas Harning | 29 Sep 21:56
Gravatar

Re: Executing a script multiple times

On Sep 29, 2008, at 3:26 PM, Juan Manuel Alvarez wrote:

> Thanks for all the quick replies!
>
> I could make it work using lua_pushvalue(-1), but I am still having
> problems with parameters.
> I am doing the operations in the following order:
>
> luaL_loadbuffer
>
> lua_pushvalue(-1)
> lua_pcall // to load the global table
>
ADJUSTED:
lua_pushvalue(-1)
> lua_getglobal
> lua_pushnumber // parameter 1
> lua_pushnumber // parameter 2
>
// lua_pushvalue(-1)
> lua_pcall
>
> The last pcall, gives me the error "attempt to call a number value".
>
> The same sequence without the parameters works fine for me. Can you
> please point me in the right direction?
For parameters, you need to make sure you do the push_value(-1)  
beforehand... otherwise your function itself becomes a parameter,  
throwing off the stack number.

(Continue reading)

Juan Manuel Alvarez | 29 Sep 22:34

Re: Executing a script multiple times

That worked just as expected!
Thank you very much to everyone!

Juan M.

On Mon, Sep 29, 2008 at 4:56 PM, Thomas Harning <harningt <at> gmail.com> wrote:
> On Sep 29, 2008, at 3:26 PM, Juan Manuel Alvarez wrote:
>
>> Thanks for all the quick replies!
>>
>> I could make it work using lua_pushvalue(-1), but I am still having
>> problems with parameters.
>> I am doing the operations in the following order:
>>
>> luaL_loadbuffer
>>
>> lua_pushvalue(-1)
>> lua_pcall // to load the global table
>>
> ADJUSTED:
> lua_pushvalue(-1)
>>
>> lua_getglobal
>> lua_pushnumber // parameter 1
>> lua_pushnumber // parameter 2
>>
> // lua_pushvalue(-1)
>>
>> lua_pcall
>>
(Continue reading)


Gmane