lua-users home
lua-l archive

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


Hello again,

Is it worth implementing a nice caching mechanism for doing allocations inside lua_Alloc function?

As far as I see, lua_Alloc is quite stressed with a lot of calls. I was thinking, instead of doing a lot of realloc/free, to allocate the necessary memory in logarithmic increments and never de-allocate the memory. Of course, cleanup the cache at the exit of the application. 

For example:
nsize: 1 - 128 bytes,  allocate 128 bytes
nsize: 129 - 384 bytes,  allocate 384 bytes
nsize: 385 - 896 bytes, allocate 896 bytes
nsize: 897 - 1920 bytes, allocate 1920 bytes
nsize: 1921 - 3968 bytes, allocate 3968 bytes
....

Instead of de-allocating the memory (nsize==0 && ptr!=NULL), keep that memory pointers in a binary-tree-like structure and just pick them up next time.

I've done some synthetic tests, and it looks like there is a lot of improvements especially when working with android on a phone. IMHO, thrashing the allocator with a lot of realloc/free is not a good thing. 

This is only a scheme example. It can be enhanced with mem limits (do standard free/alloc after a certain threshold), tweaking the logarithmic scale, pre-allocating, etc.

What do you say?

Thank you

------
Eugen-Andrei Gavriloaie
Web: http://www.rtmpd.com