[Ironpython-users] LightweightScopes and scopes in general

Dino Viehland dinov at microsoft.com
Fri Feb 10 19:46:48 CET 2012

Jeff wrote:
> I'm taking a stab off the top of my head based on my limited knowledge of the
> internals, but I do know that debug code is not collectable.
> It's possible that the code itself may be holding references that keep objects
> alive longer than expected.
> I believe if you run the code in a separate AppDomain you can unload the
> AppDomain and the code will be collected, which should take everything else
> with it.
> >
> > I've also tried running in the release mode and turning on the
> > options["LightweightScopes"] = true mode, which seems to help. But I
> > cannot find any information about what this option actually does and
> > what happens with the scope variables in general. Any info would be
> appreciated.
> That one I'll have to leave to Dino.

The difference between the different types of scopes just comes down to how
we hold onto global variable and call site objects.

The most efficient way to hold onto these is to use CLR static fields to store the 
global variables.  That allows the CLR to generate a direct access to the field and 
we can quickly access the global variables.  But it comes at the cost of not being 
able to recover the static fields and leaking the values that are stored in there.

The light weight scopes store the variables in a field or an array instead of storing
them in static fields.  To do that we need to pass in the field or close over it and then
on each access we need to do the field or array access (and if it's an array access, it
needs to be bounds checked).  But the end result is that the only thing which is keeping
the global variable / call site objects alive are the delegate which implements the
ScriptCode.  Once the ScriptCode goes away all of those call sites and PythonGlobal
objects can be collected.

So lightweight scopes come at a performance cost, but they are more applicable 
Where you're actually re-compiling code regularly.

More information about the Ironpython-users mailing list