This Stack goes to Infinity

In the official CPython implementation, the Python interpreter mixes the state of Python's stack with the state of the underlying C code's stack. Under normal operation, this design choice has little impact, but if one looks more closely there are some major drawbacks.

One such drawback is that Python's stack depth is now limited by the maximum stack depth supported by the underlying CPython process. This might not seem so bad until you consider that one function call in Python may cause many C functions to be pushed onto the stack.

A solution to this problem is to separate Python's stack from C's stack. This is exactly what Stackless Python does. Now, Python's stack size is only limited by the amount of memory in the machine.

I've implemented the Sieve of Eratosthenes example from last time using the tasklets and channels that Stackless provides. I haven't really written any Stackless code until now, but I feel this is a decent translation.



Running this results in decent performance:



I tried running the same example to find the millionth prime, but after hours of execution time I killed the process. While the memory consumption was reasonable, the execution time suffered from, what I believe to be, much overhead from object creation and switching between tasklets. Still, it's encouraging that this version didn't fail due to stack depth problems.

Stackless Python is an interesting experiment, and I wish the features implemented here were part of the official Python release. If you haven't heard of Stackless before, I encourage you to read about what it offers to Python and why this can be useful beyond the toy example I have here.