4 ms·
Fun story! The same idea applies more generally as well with so-called "region-based" memory allocation. A sub-process allocates incrementally from a pool, the
by brandonbloom 9y ago
Fun story!
The same idea applies more generally as well with so-called "region-based" memory allocation. A sub-process allocates incrementally from a pool, then when that process ends, the entire pool is freed at once. Optionally, garbage collection or other memory freeing can happen during the process. Additionally, regions can be arranged hierarchically for processes and sub-proesses, recursively.
The easiest way to take advantage of this is, if for example, you have a short-lived command line tool. Maybe you don't even bother to free any memory at all, as the operating system is going to free it all for you when you're done anyway.
- frutiger 9y agoThe (new in c++17) polymorphic allocators allow this, as you can instantiate a monotonic_memory_resource, and use it to construct all the regular C++ containers with it for the duration of the remaining scope - this works just fine as long as no memory is needed outside of that scope.
- galacticpony 9y ago...so preoccupied with whether or not they could, they didn’t stop to think if they should.
- blt 9y agoThat's cool. Do the C++ containers now contain a pointer to the monotonic_memory_resource, or is there some magic that lets the local calls to push_back, etc., know about the allocator in that local scope? When I needed many arena-backed vectors, I wrote an ArenaVec with an API that takes a pointer to the arena in all methods that can allocate, keeping the memory size down.
- frutiger 9y agoYes they do, but since c++11, allocators were not required to be stateless. As a result, containers (or any allocator-aware objects) have needed to hold an instance of the allocator.
- Retr0spectrum 9y agoI believe certain web servers do this, since some memory only has to be kept for the duration of a request.
- zzzcpan 9y agoNginx does this and it borrowed the idea from Apache.
- mikeash 9y agoI once spent a while building a tool like that. The day I realized that I needed to convert it into something with a top-level loop was a sad one.
- panic 9y agoGame engines often use region-like allocators for data that only lasts a frame -- check out the "tagged heap" in http://twvideo01.ubm-us.net/o1/vault/gdc2015/presentations/Gyrling_Christian_Parallelizing_The_Naughty.pdf http://twvideo01.ubm-us.net/o1/vault/gdc2015/presentations/G... for example.
- yyhhsj0521 9y agoThat's what I do in ACM-ICPC when I use C++! :-)
- flukus 9y ago.Net now has a non-hierarchical version of this: http://mattwarren.org/2016/08/16/Preventing-dotNET-Garbage-Collections-with-the-TryStartNoGCRegion-API/ http://mattwarren.org/2016/08/16/Preventing-dotNET-Garbage-C...