lua-users home
lua-l archive

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


On 15/01/20 21:18, Stefan wrote:
 * Fast state creation and destruction even with many C functions

I once toyed with Lua state rollback and reuse, using linear allocators. The idea was to have instant Lua states for use, and fast memory management, for short-lived scripts.

The process was like this:

1. Create a linear allocator
2. Create a Lua state using the linear allocator to manage memory (note that the linear allocator does not free memory)
3. Check and save the number of bytes used by the Lua state used after initialization, including all necessary modules (state_size)
4. Allocate state_size bytes from the allocator and save the pointer (inited_state), and memcpy the initialized Lua state to that new area
5. Check and save the number of bytes used in the linear allocator (rollback)
6. Use the Lua state to run a script
7. Memcpy inited_state over the state at the beginning of the linear allocator
8. Set the number of used bytes by the linear allocator to rollback (uncommit the unused pages)
9. Go to 6

Of course each linear allocator must have enough space reserved for your worst case, but that's not a problem in 64-bit OSes since the address space is huge.

Cheers,

Andre