[Ironpython-users] LightweightScopes and scopes in general

Dino Viehland dinov at microsoft.com
Fri Feb 10 22:40:58 CET 2012


We won’t call IDispose on them.  Usually they’ll follow the pattern of implementing IDisposable + having a finalizer so they will eventually release their resources.  Also they can be used with the “with” statement so that their Dispose methods are eagerly called.

From: Igor Brejc [mailto:igor.brejc at gmail.com]
Sent: Friday, February 10, 2012 10:51 AM
To: Dino Viehland
Cc: Jeff Hardy; Ironpython-users at python.org
Subject: Re: [Ironpython-users] LightweightScopes and scopes in general


On Fri, Feb 10, 2012 at 7:46 PM, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:


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.


Thanks for that explanation, Dino.

Another question that pops up: what happens with Disposable objects (like files, GDI+ bitmaps etc.) created within the Py scope (if they are not referenced from the outside)?

Igor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120210/8372cf42/attachment.html>


More information about the Ironpython-users mailing list