5 ms·
> the language should allow you to express simple memory ownership when you have it The problem is that you only think you have such a thing. If you're wrong,
by kazinator 11y ago
> the language should allow you to express simple memory ownership when you have it
The problem is that you only think you have such a thing. If you're wrong, you're screwed. :)
> The third and maybe most important optimization is to make garbage collection a per-thread operation. You simply don’t allow garbage collected heap allocations to transfer between threads.
This takes away much of the point of threads, leaving you better off with processes---and process boundaries will better enforce this model. You cannot cons up a message, pop it in a queue, and have another thread pick that object out the other end: thus you have to use some IPC based on bytes in a pipe or shared memory buffer.
In other words, you might as well fork a bunch of children that have not just their own heaps but their own address spaces to enforce the containment of those heaps. There is a lot of good in it: fault isolation, more virtual memory footprint to each little worker. There is a degree of attractiveness in multiple processes with just one thread in each one, and single-threaded garbage collection (which doesn't have to deal with hairy concurrency problems, and so is faster and more robust).
> To figure out which region a pointer refers to in this scheme, we simply loop through the different region sizes that our allocator allows, mask the appropriate number of bits from the pointer to get the start of the proposed region, read the region index in the first DWORD of this memory area (this may be garbage if we have the wrong size) use this to index into the region pointer array (taking care to avoid buffer overruns from garbage indices) and ensure that the pointer stored in that element refers right back to the proposed region. We also must ensure that the size of the region at this address actually contains the original pointer.
The problem of mapping a pointer to a region is already solved very well with existing data structures. For instanc, the Linux kernel uses a red-black tree to map a virtual memory pointer to a "struct vm_area". (These are the regions allocated by mmap). The regions don't overlap and so a binary search tree ordered by their base address works fine.
Solving the problem of "does this pointer refer to a heap, and which one" in a garbage collector is only necessary when we are doing conservative garbage collection: we are given some pointer-sized value from somewhere (like a scan of some raw memory area such as a C stack), and we have no idea whether or not the value is a pointer to an object in the GC world. An always-precisely-tracing garbage collector never has to solve this problem. It starts with root values, which are known to be objects in the GC world. If they are heap values, they point to objects whose structure is known to the collector. They are precisely traversed to reach other objects and so on.
- rbehrends 11y ago> This takes away much of the point of threads, leaving you better off with processes---and process boundaries will better enforce this model. You cannot cons up a message, pop it in a queue, and have another thread pick that object out the other end: thus you have to use some IPC based on bytes in a pipe or shared memory buffer. Not really true; a lot depends on the implementation here. In a multi-threaded setup you can often avoid context switches and kernel calls (the really expensive part) and deep copying with a bump allocator is generally cheaper than deserializing/serializing [1]. Often, you can also avoid copying if you do it right. On top of that, while you can't manipulate pointers on the heap safely under such a concurrency model, you can manipulate integers and floats across heaps (which is relevant for a large number of concurrent algorithms) if you do it right (i.e. by ensuring that you don't deallocate the objects containing the data). [1] Keep also in mind that any actual shared data that goes from one thread to another will generally go through main memory or the L3 cache at one point or another.