"weak" reference to python object?

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

"weak" reference to python object?

Postby 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: 2470
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: "weak" reference to python object?

Postby 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: 5
Joined: Wed Apr 19, 2017 6:08 pm

Re: "weak" reference to python object?

Postby MrSurly » Thu Apr 20, 2017 3:52 pm

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

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

Re: "weak" reference to python object?

Postby 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.


Who is online

Users browsing this forum: No registered users and 2 guests