Hi all!
I was just wondering if there is any way to make the C driver work with an arena allocator.
Arena allocators have many benefits regarding memory management: the most important here are the ability to reduce the amount of system calls needed to allocate memory, and an easier way to release memory instad of begin tied to the malloc
/free
pairing. (If you want to know more I suggest this read).
As example, let’s look at a simple program like this:
#include <mongoc/mongoc.h>
int main(void) {
mongoc_init();
mongoc_cleanup();
return 0;
}
If compiled and run with Valgrind, we have this output:
==6188== Memcheck, a memory error detector
==6188== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6188== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==6188== Command: exe/test-bson
==6188==
==6188==
==6188== HEAP SUMMARY:
==6188== in use at exit: 0 bytes in 0 blocks
==6188== total heap usage: 8,739 allocs, 8,739 frees, 1,376,521 bytes allocated
==6188==
==6188== All heap blocks were freed -- no leaks are possible
==6188==
==6188== For lists of detected and suppressed errors, rerun with: -s
==6188== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
By giving an appopriate sized arena, we could reduce those 8739
malloc
s to just a few (or even 1 if we could assume, for example, an arena page of 2MB for mongoc_init
).
What would be needed is a way to dynamically tell the driver which allocator to use. I was looking into the docs, and found this: bson_mem_set_vtable, but it’s not very well documented so I’m having trouble understanding if it could work with an arena allocator.
Thank you!