[Stackless] Question on Hard Switching and Soft Switching

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Sep 29 23:06:39 CEST 2009

Hello Andrew.
I've been exporing the bowels of Stackless a bit in the last weeks and am somewhat familiar with the topic.
"soft switching" refers to the mechanism where we switch between tasklets without manipulating the C stack.  This is possible simply by jugling the 'frame' chains around that are used in the python code evaluation functions.
'hard switching' occurs when it becomes necessary to save the C stack, and manipulate the stack pointer.  Usually this is because some C code has been involved in the tasklet, so that its state cannot be fully represented by just a linked chain of python frames.  For example, if python code calls some C function which then recursively calls into python.
Stackless will attempt to use 'soft switching' by default when possible, if stackless.enable_softswitch is true.  It then contains mechanism to fall back to hard switching if necessary.
Stackless will also attempt to use its 'soft switching' machinery to optimize execution of regular code, by using tail recursion of method invocation, rather than recursion through the py_eval_code function.  I am less knowledgable about this last feature and I may be mistaken about is purpose and I don't know what, if any, performance impact it may have.  At any rate, this behaviour can be disabled with the stackless.enable_softswitch flag (or whatever it may actually be called.)

If stackless.enable_softswitch is set to false, then stackless will always use C - magic to do its stack switching, even in a pure python (i.e. no C extensions or recursions) environment.

I have not done any performance measurements to evaluate the merits of soft switching.  But I must confess that the presence of soft-switching makes stackless much more difficult to understand for me.  And a lot of the diff between trunk python and stackless python is because of the boilerplate code required to get soft switching to work.

I hope this clears things up.



> -----Original Message-----
> From: stackless-bounces at stackless.com [mailto:stackless-
> bounces at stackless.com] On Behalf Of Andrew Francis
> Sent: 29. september 2009 16:34
> To: stackless at stackless.com
> Cc: pypy-dev at codespeak.net
> Subject: [Stackless] Question on Hard Switching and Soft Switching
> Hi Folks:
> As a part of an up-coming talk, I have been reading more about that
> happens under the hood. For instance "The Essentials of Stackless
> Python."
> The terms 'hard switching' and 'soft switching' constantly appear.
> 'Soft' switching is defined as cooperating switching. I will assume
> this is associated with a 'stackless.schedule()' or a blocked on
> channel.
> Hard switching is defined as brute-force. However what is brute-force?
> Is this when Stackless is in pre-emptive mode and one is relying on the
> ticks?
> Cheers,
> Andrew
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless

More information about the Stackless mailing list