WeakReference: WeakMap / GC callbacks

August 1, 2019 02:57 benjamin.morel@gmail.com (Benjamin Morel)
Hi internals,

I'm following up on a message by Andrea Faulds on Feb 4, 2019

Have you considered adding a WeakMap type as well — a map of (object)
> keys to values where the keys are weakly referenced? This is a useful > construct based on weak references and something I'm sure will be > wanted. It could in theory be implemented in userland on top of your > proposal, but it would be nice to save userland having to reimplement it > (probably multiple times), and userland could be saved the problem of > manually cleaning up now-dead weak references (annoyingly O(n)) and > deciding when to do so — an internal implementation can handle it > automagically and more performantly. :)
Followed by a message by Joe Watkins the next day <https://externals.io/message/104014#104186>: I have considered maps ... since it is possible to do in userland, I don't
> consider it super urgent, and even if you do, it doesn't become urgent > until PHP 7.4 is much closer to release. > So, we have almost a year; If this flies in, it's highly likely I'll > follow it up ... but don't really want to spend any more time on it until I > know it's worthwhile.
I'm starting to play with WeakReferences, and the need for a WeakMap is coming fast. As said above, yes it can be implemented in userland, but *you cannot free up memory until you have an opportunity to do so manually, and it's expensive*. This is particularly painful and defeats some of the purpose of weak references, as you cannot reclaim all memory easily. Take the example of a data mapper, that loads objects from the DB and associates them with some metadata (snapshot of original properties, etc.). Weak references, in theory, allows the data mapper to automatically clean up the associated metadata when an object goes out of scope. With the current WeakReference implementation though, we can only perform this cleanup *the next time we interact with our data mapper*, which will be our opportunity to loop through weak references and check if they're still valid. If we don't interact with it anymore (and it stays in scope), well too bad, memory is wasted. If we do interact with it, we'll waste CPU cycles by looping through weak refs on potentially every interaction, to check if something can be cleaned up. Or worse, we can put this burden on our users, by "offering" them to call some cleanup method when they see fit.. Enough said, it would be nice to have either a native WeakMap implementation, or alternatively, the possibility to set up a callback function on the WeakReference, that would be called when the object is reclaimed by the GC. This would open the possibility of an actually useful userland implementation of WeakMap (though I do think that a native one would be much better). Unfortunately the discussion went silent for quite some time and missed the feature freeze deadline for PHP 7.4. *Still, is there any plan to add WeakMap at some point?* Is there any tiny chance that an exception can be made to the feature freeze to finish this implementation? As it stands, the WeakReference is not nearly as useful as I thought it would be. Thank you, Ben