[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [idea] Add memory pool to Lua?
- From: Xavier Wang <weasley.wx@...>
- Date: Sat, 4 Jun 2016 16:56:19 +0800
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.