jemalloc/jemalloc — pull request #153 by VerGreeneyes (2014-10-16 23:48:38+00:00)
Title: Add a limited chunk recycling mode

Hi, I've been working on getting a limited version of the VM recycling code to work in Gecko (Bugzilla bug 1073662 [1]). We can't use --disable-munmap as it currently exists - we can't let jemalloc use up all VM on 32-bit platforms as there are several other allocators in play (e.g. the GC does its own thing). Instead, I propose adding --enable-recycle, which recycles a capped amount of chunks. I'd love to get this upstreamed, so we can do a straightforward backport instead of adding more stuff to our old fork of jemalloc 2.

This adds that mode, limiting the amount of chunks to recycle to 128 for 32-bit applications and 128 * 1024 for 64-bit applications (the numbers are pretty arbitrary).

In addition, this makes several fixes for Windows.
(1) With --disable-munmap, pages_purge could attempt to reset a large region composed of several smaller regions in a single call, which isn't legal on Windows. To work around this I changed pages_purge to reset chunksized regions at a time - as jemalloc never allocates less than a chunk, this won't span multiple regions.

(2) With --enable-recycle on Windows, recording or recycling regions of various sizes is inherently impossible. Say it came time to deallocate a region, and we found that recording it would put us over the limit. Now we have to unmap the region, but what if it was part of a larger region allocated in a single call to VirtualAlloc? What if it spanned regions allocated in multiple calls to VirtualAlloc? Even if we disabled the address space coalescing and only handed out regions of the same size as the original allocation, we might end up uselessly holding on to large regions forever. So instead, this only allows recording and recycling for chunksized regions. I believe this will still give us most of the benefit, since arenas are all allocated from chunksized regions.

I tested this on Linux with and without --enable-recycle, and all the tests seem to work fine (I also confirmed that the code is actually getting used with some local printfs). Unfortunately I can't test this on Windows, as the dev branch doesn't currently compile there.

PS: I'm really new to git and this whole pull request thing, so I apologize if anything I say here comes across as clumsy :)

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1073662