| 1 |
= Regions |
| 2 |
|
| 3 |
In region-based memory management, the heap is divided into a |
| 4 |
collection of regions into which objects are allocated. At compile |
| 5 |
time, either in the source program or through automatic inference, |
| 6 |
allocation points are annotated with the region in which the |
| 7 |
allocation will occur. Typically, although not always, the regions |
| 8 |
are allocated and deallocated according to a stack discipline. |
| 9 |
|
| 10 |
MLton does not use region-based memory management; it uses traditional |
| 11 |
<<GarbageCollection#>>. We have considered integrating regions with |
| 12 |
MLton, but in our opinion it is far from clear that regions would |
| 13 |
provide MLton with improved performance, while they would certainly |
| 14 |
add a lot of complexity to the compiler and complicate reasoning about |
| 15 |
and achieving <<SpaceSafety#>>. Region-based memory management and |
| 16 |
garbage collection have different strengths and weaknesses; it's |
| 17 |
pretty easy to come up with programs that do significantly better |
| 18 |
under regions than under GC, and vice versa. We believe that it is |
| 19 |
the case that common SML idioms tend to work better under GC than |
| 20 |
under regions. |
| 21 |
|
| 22 |
One common argument for regions is that the region operations can all |
| 23 |
be done in (approximately) constant time; therefore, you eliminate GC |
| 24 |
pause times, leading to a real-time GC. However, because of space |
| 25 |
safety concerns (see below), we believe that region-based memory |
| 26 |
management for SML must also include a traditional garbage collector. |
| 27 |
Hence, to achieve real-time memory management for MLton/SML, we |
| 28 |
believe that it would be both easier and more efficient to implement a |
| 29 |
traditional real-time garbage collector than it would be to implement |
| 30 |
a region system. |
| 31 |
|
| 32 |
== Regions, the ML Kit, and space safety |
| 33 |
|
| 34 |
The <<MLKit#,ML Kit>> pioneered the use of regions for compiling |
| 35 |
Standard ML. The ML Kit maintains a stack of regions at run time. At |
| 36 |
compile time, it uses region inference to decide when data can be |
| 37 |
allocated in a stack-like manner, assigning it to an appropriate |
| 38 |
region. The ML Kit has put a lot of effort into improving the |
| 39 |
supporting analyses and representations of regions, which are all |
| 40 |
necessary to improve the performance. |
| 41 |
|
| 42 |
Unfortunately, under a pure stack-based region system, space leaks are |
| 43 |
inevitable in theory, and costly in practice. Data for which region |
| 44 |
inference can not determine the lifetime is moved into the "global |
| 45 |
region" whose lifetime is the entire program. There are two ways in |
| 46 |
which region inference will place an object to the global region. |
| 47 |
|
| 48 |
* When the inference is too conservative, that is, when the data is |
| 49 |
used in a stack-like manner but the region inference can't figure it |
| 50 |
out. |
| 51 |
|
| 52 |
* When data is not used in a stack-like manner. In this case, |
| 53 |
correctness requires region inference to place the object |
| 54 |
|
| 55 |
This global region is a source of space leaks. No matter what region |
| 56 |
system you use, there are some programs such that the global region |
| 57 |
must exist, and its size will grow to an unbounded multiple of the |
| 58 |
live data size. For these programs one must have a GC to achieve |
| 59 |
space safety. |
| 60 |
|
| 61 |
To solve this problem, the ML Kit has undergone work to combine |
| 62 |
garbage collection with region-based memory management. |
| 63 |
<<References#HallenbergEtAl02,HallenbergEtAl02>> and <<References#Elsman03,Elsman03>> describe the addition |
| 64 |
of a garbage collector to the ML Kit's region-based system. These |
| 65 |
papers provide convincing evidence for space leaks in the global |
| 66 |
region. They show a number of benchmarks where the memory usage of |
| 67 |
the program running with just regions is a large multiple (2, 10, 50, |
| 68 |
even 150) of the program running with regions plus GC. |
| 69 |
|
| 70 |
These papers also give some numbers to show the ML Kit with just |
| 71 |
regions does better than either a system with just GC or a combined |
| 72 |
system. Unfortunately, a pure region system isn't practical because |
| 73 |
of the lack of space safety. And the other performance numbers are |
| 74 |
not so convincing, because they compare to an old version of SML/NJ |
| 75 |
and not at all with MLton. It would be interesting to see a |
| 76 |
comparison with a more serious collector. |
| 77 |
|
| 78 |
== Regions, Garbage Collection, and Cyclone |
| 79 |
|
| 80 |
One possibility is to take Cyclone's approach, and provide both |
| 81 |
region-based memory management and garbage collection, but at the |
| 82 |
programmer's option (<<References#GrossmanEtAl02,GrossmanEtAl02>>, <<References#HicksEtAl03,HicksEtAl03>>). |
| 83 |
|
| 84 |
One might ask whether we might do the same thing -- i.e., provide a |
| 85 |
`MLton.Regions` structure with explicit region based memory |
| 86 |
management operations, so that the programmer could use them when |
| 87 |
appropriate. <<MatthewFluet#>> has thought about this question |
| 88 |
|
| 89 |
* http://www.cs.cornell.edu/People/fluet/rgn-monad/index.html |
| 90 |
|
| 91 |
Unfortunately, his conclusion is that the SML type system is too weak |
| 92 |
to support this option, although there might be a "poor-man's" version |
| 93 |
with dynamic checks. |