[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua_newstate vs lua_newthread
- From: Tim Hill <drtimhill@...>
- Date: Fri, 26 Jun 2015 02:19:58 -0700
> On Jun 26, 2015, at 1:37 AM, Jinhua Luo <luajit.io@gmail.com> wrote:
>
> Sometimes we need to use the whole lua vm as work unit, e.g. dispatch
> the work unit among OS threads. However, it's a bit overhead at the
> lua_newstate and lua_close. Is it feasible to reuse the vm after
> cleanup (keep sandboxing)?
>
> See my simple idea (it's simple and far from perfect, and it doesn't
> consider registry changes via C API):
> https://gist.github.com/kingluo/ad5c40047bd41ab05d5e
>
> Any suggestion?
>
In my experience loading the Lua libraries (luaL_openlibs()) is the most time-consuming part of creating a new Lua state, so we devised a JIT mechanism to amortize this over the lifetime of the Lua VM. Closing is trickier since it involves a complete GC cycle which has unpredictable time.
Another way to handle this is to pre-load one or more states into a “ready to go” queue and pull them out when needed. This won’t decrease total CPU time, but it will speed up the instantaneous startup of each state; the same can be done at shutdown.
Trying to cleanup a state for re-use appeared tricky when I looked at it.
—Tim