Next: , Previous: , Up: Grade modifiers   [Contents][Index]


3.4.6 Grade modifiers for flexible stack sizes

In MLDS grades, the code generated by the Mercury compiler uses the native call stack of the target language. In LLDS grades, on the other hand, the generated code manages its own two stacks: the det stack, which stores the data of procedures that cannot succeed more than once, and the nondet stack, which stores the data of procedures that can succeed more than once. (The two stacks are separate because for predicates that can succeed more than once, we cannot throw away the values of their input arguments and local variables when they succeed, while for predicates that cannot succeed more than once, we can throw them away, and for good performance, we must do so.)

By default, Mercury reserves a fixed size block of memory for each stack, and never increases their size during execution. To make this work even for programs that use relatively deep recursion, the default size of the det stack is 16 megabytes on 32-bit machines, and 32 megabytes on 64-bit machines. These default sizes can be overridden using runtime options (see Environment variables affecting the Mercury runtime system), but whatever pair of sizes one picks, for some programs, they will be wasteful overkill, while for some other programs, hopefully rarely, they nevertheless will not be enough. If the platform allows it, we make the top page of each stack inaccessible, so that a stack overflow, instead of accessing and overwriting memory effectively randomly, causes the operating system to send a signal to the program. If not caught and handled, this signal will abort the program, minimizing the damage.

We therefore have a grade modifier that makes the sizes of the two stacks dynamic, and able to respond to the actual requirements of the program being executed.

.stseg

Compiling programs in a grade that includes the .stseg grade modifier generates executables that use stack segments, small memory areas whose size is 16 or 32 kilobytes on 32- and 64-bit systems respectively. In .stseg grades, the det and nondet stacks both consist of a linked list of one or more stack segments. Each stack grows by adding a new segment at a time on demand, and shrinks by putting segments back on a free list once they are no longer needed.

Using stack segments does have a small performance penalty, because it requires code for checking

However, for many applications, this is a more than acceptable price to pay for not imposing any limit on stack sizes other than the amount of available memory.

This grade modifier is relevant only for LLDS base grades, and is compatible only with them.


Next: , Previous: , Up: Grade modifiers   [Contents][Index]