hadron-sclang / hadron

drop-in jit replacement for sclang
https://solitarybees.us
39 stars 2 forks source link

Consider using jemalloc as a replacement for the system allocator #36

Closed lnihlen closed 3 years ago

lnihlen commented 3 years ago

The jemalloc memory allocator, introduced into the system in #35, can also be used as a replacement for the system memory allocator. This can offer a performance benefit in some applications, sometimes a substantial one, and is worth some data-driven experimentation. It might also mean that jemalloc's usage analytics could be available program-wide.

UPDATE: further reading into jemalloc shows that by default it might be overriding itself on MacOS as the default allocator using the zone allocation hooks. And it certainly also provides a C++ integration. So overriding the system allocator may be as simple as removing the --disable-cxx and --disable-zone-allocator flags as documented in the jemalloc INSTALL file.

However, replacing the system allocator is non-trivial and platform-specific. It will come at a fairly high complexity cost and therefore may only be worthwhile if the performance benefits are nontrivial. What follows are some notes taken when doing the initial research on this.

One important note is that jemalloc at configure time can build with either a user-supplied prefix for all functions (the default is je_) or not. If no prefix is provided the jemalloc functions will have the same signature as the system C functions, meaning for example je_malloc() becomes just malloc(). The unprefixed option is not available on MacOS, as the compiler complains about redefinition of the system allocator functions.

Three overall approaches to jemalloc usage are outlined in the Getting Started documentation:

If the goal is to replace the system allocator, and to work across all three operating systems, only the first seems like a viable approach. Recording some notes here about different examples of this approach I found while researching system allocator replacements:

lnihlen commented 3 years ago

We're moving to use the garbage collection system for JIT memory allocation in #54, thus deprecating jemalloc. Closing this for now, as it is a far-future possible optimization.