"weak" reference to python object?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
MrSurly
Posts: 7
Joined: Wed Apr 19, 2017 6:08 pm

"weak" reference to python object?

Post by MrSurly » Wed Apr 19, 2017 10:17 pm

I have an implemented class that provides "make_new" to create a related object. That related object has a UUID, and the object is a "singleton" per UUID; that is for UUID "5", there is only one object.

To implement this, I was going to keep a table of existing created objects, and if the constructor is called for an existing UUID, then it would simply return the existing object. I was going to use a µPy object to hold these, internally (maybe a set?). In any case, if there aren't any _external_ references (in user python code), then I'd like to discard the objects.

Questions:

If I have an "internal" mp_obj_t (say, made with mp_obj_new_list), will that be safe from the GC?

If I store objects inside this "internal" structure, how can I tell if they're referenced "outside"?

If they're not referenced outside (thus not needed internally, either), how can I mark them for reaping by the GC?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: "weak" reference to python object?

Post by dhylands » Thu Apr 20, 2017 1:04 am

MrSurly wrote:I have an implemented class that provides "make_new" to create a related object. That related object has a UUID, and the object is a "singleton" per UUID; that is for UUID "5", there is only one object.

To implement this, I was going to keep a table of existing created objects, and if the constructor is called for an existing UUID, then it would simply return the existing object. I was going to use a µPy object to hold these, internally (maybe a set?). In any case, if there aren't any _external_ references (in user python code), then I'd like to discard the objects.
you probably want a dict rather than a set. The key would be the uuid and the value would be the singleton object for that uuid.
Questions:

If I have an "internal" mp_obj_t (say, made with mp_obj_new_list), will that be safe from the GC?
As long as the internal object is referred to by some object on the heap, or otherwise through a root pointer.
If I store objects inside this "internal" structure, how can I tell if they're referenced "outside"?
You can't (at least not directly). The GC uses a mark and sweep algorithim. It clears all of the "marks" for each object in the heap, then starts with the root pointers and marks every referenced object as "in-use". When its done, any objects which are not marked as "in-use" can be freed.
If they're not referenced outside (thus not needed internally, either), how can I mark them for reaping by the GC?
You don't need to do anything. When nothing else references them, then the GC will reap them then next time collect is called.

MrSurly
Posts: 7
Joined: Wed Apr 19, 2017 6:08 pm

Re: "weak" reference to python object?

Post by MrSurly » Thu Apr 20, 2017 3:52 pm

Does that mean that there's no weakref concept for µPy objects?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: "weak" reference to python object?

Post by dhylands » Thu Apr 20, 2017 5:26 pm

Not that I'm aware of. If there is a reference to an object then it doesn't get freed.

The issue with weakrefs (that I see) is that the GC would need to zero out the reference and the GC doesn't really know if a reference is a pointer or an integer that just happens to have an unfortunate value.

I suppose if the GC knew for sure that an object was a weakref object then it could work with it, but it feels like quite a bit of complexity.

pmp-p
Posts: 13
Joined: Mon Apr 24, 2017 12:45 am

Re: "weak" reference to python object?

Post by pmp-p » Wed Sep 11, 2019 8:02 am

it seems finalizers support is implemented in VM. but no way provided to use them from Python.

https://github.com/micropython/micropython/issues/245
related:
__del__ special method not implemented for user-defined classes https://github.com/micropython/micropyt ... ssues/1878

i would be interested for discussion around the idea of an "uweak" object :
Do we want to have weakref support? https://github.com/micropython/micropyt ... -530262406

Post Reply