[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Memory Allocation Hooks
- From: Spencer Schumann <gauchopuro@...>
- Date: Tue, 11 Jan 2005 22:11:16 -0700
> Having never written a single line of Lua code I briefly took a look at the
> interpreter source to see if it's possible to add malloc()/free() hooks.
Look at lmem.h and lmem.c; all allocations eventually go through
luaM_realloc, which is defined within these files. But I doubt that's
what you really want.
> What I'm looking for is the ability to allocate a Lua state from my own
> memory arena and disable Lua's garbage collection so that I can free the
> entire Lua execution context in one go. Each Lua context will be running a
> handful of extremely short scripts per instantiation.
I think you'll find that garbage collection takes surprisingly little
time. For executing small scripts, each with its own Lua state,
creation and deletion of the Lua state will probably dominate
processing time. If you do want to disable garbage collection, you
can call "collectgarbage(1000000)". I tried just such a thing early
in my Lua career, thinking that it would help to stop collecting
during CPU intensive operations. However, in my case, disabling
garbage collection actually slowed things down. With the caching and
virtual memory systems in modern computers, keeping a small memory
footprint is important.
> My priority is extremely fast startup and shutdown.
With this as your priority, you may want to use a single Lua state to
run all your scripts. If you use setfenv, you can give each script
its own global environment to avoid problems with variables leaking