arximboldi / immer

Postmodern immutable and persistent data structures for C++ — value semantics at scale
https://sinusoid.es/immer
Boost Software License 1.0
2.48k stars 177 forks source link

jemalloc memory allocation strategy #15

Open thedrow opened 7 years ago

thedrow commented 7 years ago

Folly, a library developed by Facebook provides containers with specific memory allocation strategies using jemalloc's private non-standard APIs. I wonder if we could do the same for immer. Since persistent/immutable data structures are used in concurrent applications using jemalloc will improve the performance of the containers we provide.

arximboldi commented 7 years ago

Interesting! I am aware of the "normal" jemalloc api, which is more or less trivial to integrate by defining a heap using immer/heap/malloc_heap.hpp as an example. But I was not aware that there were special private apis you mention. @thedrow can you pass me a link to some specific class where these private apis are used, or some document describing the advantages of this private api? Thanks!

thedrow commented 7 years ago

This is one allocator using the non-standard API https://github.com/facebook/folly/blob/d4aacd244f21e76dce685365acc281a9015897c1/folly/experimental/JemallocNodumpAllocator.h Here's an IOBuffer that does an inplace allocation using xcallox https://github.com/facebook/folly/blob/d4aacd244f21e76dce685365acc281a9015897c1/folly/io/IOBuf.cpp#L825 Here's a vector that does inplace allocation using the same trick above https://github.com/facebook/folly/blob/d4aacd244f21e76dce685365acc281a9015897c1/folly/FBVector.h#L1014 You can find more throughout the code base.

You can see the description of all the methods jemalloc exposes here See http://jemalloc.net/jemalloc.3.html#idm45353369694928

I was using the word "private" when I should have used non-standard API. This is now edited in the original issue.

arximboldi commented 7 years ago

Aha! Thanks for the details @thedrow!

It seems there are different potential ways these things could be used. The reallocation functions at the moment don't have a space in Immer but they will have soon---there are plans to improve the transient push back to use less memory where this could be useful.

The regions/arena thing seems useful, but it requires allocators with local data which are not supported now (issue #14 also mentions this). I will probably not get to that until after implementing hash maps.

If you are using Immer in a commercial project where these jemalloc features are important and need high priority I can suggest buying consulting/development time. In this way I can also look further into your particular use-case to make sure we make the most efficient use of Immer. Drop me a line at juanpe@sinusoid.es if you are interested!

thedrow commented 7 years ago

Wish I had the money to pay you to do this. :) It's just an idea and might be useful for me if I end up bringing these containers to OSQuery.

arximboldi commented 7 years ago

No prob 😄 I'll leave the issue open in case I or someone else has time eventually to work on this.