3 ms·
I wonder if a hybrid approach is possible. Run GC recursively. Arrange for the segfault to run on an alternative stack (sigaltstack). If a segfault occurs due t
by kazinator 9y ago
I wonder if a hybrid approach is possible. Run GC recursively. Arrange for the segfault to run on an alternative stack (sigaltstack). If a segfault occurs due to insufficient stack space, then do the rest of the GC using the "queue draining loop" approach, within the small amount of alternative stack memory. Then longmp to some point in the original GC to bail out the original stack.
- kazinator 9y agoIn my TXR Lisp, the "alt stack" is used transparently to the programmer. So we can catch a stack segfault. This is exploited in the solution to the Rosetta Code "Find Limits of Recursion" task: https://rosettacode.org/wiki/Find_limit_of_recursion#TXR https://rosettacode.org/wiki/Find_limit_of_recursion#TXR The implementation code is here: http://www.kylheku.com/cgit/txr/tree/signal.c http://www.kylheku.com/cgit/txr/tree/signal.c Search for "setup_alt_stack" and "teardown_alt_stack". Basically, this allows a Lisp signal handler lambda to execute even though the stack has been blown. What if this is in the middle of gc? We can't run Lisp code with a heap that is the middle of gc, due to objects being marked and such. That situation is detected and a function called gc_cancel is called to simply cancel gc. Everything that has been marked is unmarked. To bring the gc machine to the most obvious safe state, all the structures related to generational gc are reset, and a full gc is scheduled. With that implemented, the above Rosetta task became reliable; the random failures it exhibited were gone. Users programs (on platforms supporting sigaltstack) have a fighting chance to do something if gc runs out of stack, like save some data to disk or whatever.
- quotemstr 9y agoWhy would you bother supporting both approaches? It's not as if the recursive approach is any more efficient --- and whether or not you start with the recursive approach, you need to keep memory reserved for a worst case queue traversal.
- kazinator 9y agoFor one thing, because the recursive approach is debugged. If we can show that the new code doesn't change what the old code is doing when that case is not triggered, then then we can be confident that a bug hasn't been introduced.
- olliej 9y agoI wrote an implementation of Haskell on .net many years ago that essentially did something similar for deeply recursive code - basically catch stack overflow, and then spawn a new thread and continue execution on that. This infinit stack space :)