[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [idea] Add memory pool to Lua?
- From: Daurnimator <quae@...>
- Date: Sat, 4 Jun 2016 19:42:26 +1000
On 4 June 2016 at 18:56, Xavier Wang <weasley.wx@gmail.com> wrote:
> Hi list :)
>
> I have made a small library before[1], and I found a memory pool can
> keep memory footprint small, and reduce allocate count much,
> especially for object have same size.
>
> The implement is here[2], only less than 50loc, and it allocates a
> page (4K) per time, and use it as a array to avoid alignment issues.
> it use the last sizeof(void*) to make a linked list. When each page
> allocated, the objects in it will use the first sizeof(void*) bytes to
> make a free object linked list. if free objects exists, it just do a
> pointer calculates to get a new object, and if no free object exists,
> it allocate new pages. implements are quite small and clear.
>
> So, I plan to add this idea to Lua codebase, in two favours: first, I
> will make a small memory pool, say 64bit per object, if Lua tries
> alloc a memory less than 64bit, the memory will allocate from pool, it
> save memory pieces created by small Lua closures, small strings, etc.
> then, I will find objects that have same small size to make they own
> pool for them. I think UpVal, Table are fits.
>
> I share this idea to list, so may somebody could interest it and give
> some help, about:
> - Any advice or review are welcome, Thanks everybody that points my fault :)
> - I'm not sure it can improve performance, it could make less memory
> allocate count, but waste a little memory, So any test case are
> welcome.
>
>
> I will reply this mail after I make patch and have any result about this.
>
> Thanks for watching :)
>
>
> [1]: https://github.com/starwing/nui
> [2]: https://github.com/starwing/nui/blob/new/nui.h#L441
>
> --
> regards,
> Xavier Wang.
>
The main benefit of multiple memory pools is that cleanup is easy: you
free/unmap the whole pool at once, instead of freeing each and every
allocation. This saves you a substantial amount of bookkeeping.
What you're suggesting is just an optimised malloc/realloc for common
lua object sizes.
Lua *does* have a facility for this: you can pass a lua_Alloc to lua_newstate.
However, it will be hard to beat your system's malloc; lots of
research and benchmarking has (should have) gone into it.