micropython icon indicating copy to clipboard operation
micropython copied to clipboard

Do we want to have weakref support?

Open pfalcon opened this issue 11 years ago • 5 comments

Here's another thing to consider re: completeness of language implementation: do we want to support weak referenced (weakref module)?

On one hand, it's niche feature used rarely enough. On the other hand, it's something which would pretty much help low-memory usage: ability to cache some object in memory, but safely and transparently free it if there's memory pressure.

But efficient implementation of weakrefs is expensive - each object need to grow to contain pointer to its associated weakref. Taking into account that weakrefs are rare, less runtime-efficient, but much more memory-efficient impl of having a separate mapping from obj to its weakref can be used.

Thoughts/comments are welcome.

pfalcon avatar Jun 01 '14 15:06 pfalcon

I hit weakref usage in 3rd-party code (npyscreen lib), added dummy module to micropython-lib: https://github.com/micropython/micropython-lib/commit/450b8b63ec404d1d9dfa30c2005ab93942e3a4d3

pfalcon avatar May 08 '15 21:05 pfalcon

i think if __del__ (for user defined objects) https://github.com/micropython/micropython/issues/1878 is not implemented then weakref should be for #4993.

or at least a generic C "uweak" object with a C __del__ ready to use callback function that could be attached to user defined instances so it can react when "host" is collected eg with a callback and id(host) as parameter. it seems there's already some support to do that https://github.com/micropython/micropython/issues/245

# cpython demonstration of a what could be a µpy C object
class uweak:
    def __init__(self, host):
        self.host = id(host)

    def __del__(self):
        print(self.host,"is gone")

class user_def:
    def __init__(self):
        self.weak =uweak(self)

def test():
    u = user_def()

test()

Actual gc is out of Python control ( finalizers control - not cycle control ). determinist gc handling is nothing but a niche feature ( RPC / ffi / external c++ libraries )

pmp-p avatar Sep 11 '19 07:09 pmp-p

11 years later here I am with this requirement to let MicroPython drive remotely CPython or MicroPython on boards so that it needs to hold refs that, once collected, should notify the remote host it can free those references too ... everything is moving fine with CPython instead of MicroPython as the "driver", I am trapped by this inability to observe by any mean the collection cycle of any MicroPython object and the missing weakref namespace is also pretty painful.

Pinging @dpgeorge as he knows already I would love to have literally anything usable, even "just __del__!" to be able to replicate what I am after so apologies for bringing such old topic up again, but meanwhile many things changed and this feature, or any way to be notified about the internal gc about anything, would be very well welcomed/appreciated.

WebReflection avatar Nov 28 '25 13:11 WebReflection