lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Thank you for the help

Asko Kauppi

You can of course use precompiled scripts, but even then setting up
the Lua state will take some time & code.
[BLH] I tried to use precompiled scripts, it reduces about 80-90% CPU usage in LUA logic processing. I am even thinking about to mmap the precompiled scripts to memory, so that the program doesn't use fopen/fread to read the instructions from the precompiled scripts. While setting up the lua state are still require when C program start each LUA function call.

An approach I've used in Lua Lanes is to have forever-looping server
threads, who exchange data over receive/send connection with others.
In fact, Lanes has grown now to a fairly OS-kernel-like set of
features (threads ~ processes), which indeed is a part of my vision
of a "virtual" operating system.

But maybe my "answer" is more than you were asking. What you'd want
(imho) is ability to 'clone' the template lua_State after loading
'test.lua', and then be able to close those clones after usage. Maybe
you can twist 'lua_newthread()' to do this?
[BLH] Yes this is what I want,  to get a clean copy of template lua_State when C program begin the LUA function call.  I will try  'lua_newthread. I am not sure if the lua_State generated by lua_newthread will share some sort of pointer with the template lua_State, or other lua_State in different threads. If it is the case one mess up in a LUA function call may affect others.
Good luck. :)

Huang Lin kirjoitti 2.6.2007 kello 18:23:

> Hi there,
> I use lua in this way in my C program.
> int getPrompt (char* output)
> {
> lua_State *L = lua_open(); /* create state */
> if (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0))
> {
> }
> /// other lua logic to get output
> lua_close(L);
> }
> By using a local lua_State, the C program can begin the LUA logic
> from a fresh environment. This is also helpful when the C function,
> getPrompt, was run in multithread environment that doesn't require
> LUA related data to be shared/exchanged between threads.
> But it is expensive to load the LUA instructions each time when I
> just want a local and fresh LUA runtime environment.
> Is it possible to load/compile the LUA scripts once, put the result
> in the memory, and let the local lua_State(an independent lua
> universes) use it?
> Thanks,
> Huang Lin