Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106373 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50874 invoked from network); 1 Aug 2019 05:32:43 -0000 Received: from unknown (HELO mail-io1-f46.google.com) (209.85.166.46) by pb1.pair.com with SMTP; 1 Aug 2019 05:32:43 -0000 Received: by mail-io1-f46.google.com with SMTP id h6so15838033iom.7 for ; Wed, 31 Jul 2019 19:58:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=R6qKdcUpPpx4Odqu8oRH/YmUQpDtHgMKO8JCc1i6Cb4=; b=WmT94B7KbyvKAhg8fYYEPGPcaSDapmze4nU7SLQ0SmxAUubPjbVnimy/34YIlI7/O7 y/lJrR97luivK/rxAjKdF/sE9sJge8qI7vgj0FXR8xqtm/hA1kpU0a4tD8FN5Ky8BV0a hu8fSkxY5UqtLPOKlZlxVCe/HiDi3+ivXsXSlkzXVwJVopy3dZ1S67l0Xn87szc2fPcO srL9hzzi5S8QdhlxZi/NqyZHz3T8C+HmdxhVEsncWNvL/VAFR3u+X5g5sKw7SaH0Cs7a WzwqG4BUVbxuR6/3kZeS/5LajYlXOsFF2S0aj4rj2T27hhjRLrmo4fMPBMOo64q2wvxW RSeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=R6qKdcUpPpx4Odqu8oRH/YmUQpDtHgMKO8JCc1i6Cb4=; b=SUiIB/tUKoMsHSwmFL3LCJ3kOIAlb9945cVP3dFpJNmjvt6yRD3WlnXOpUGIPP/4jU LGFpF/vO1dfCmT3Jou7rFcoAqwkV7in/xuzH1RTr7HS52qg9rjcZw23DNh3Qu8n1qJOO embD/3Tx4pqbdC8UlvHcUXwaPWveUFUn/iJvYwr9nUJRboxVJecATv5tiV9jc0hOcC/V J/ITrwaQtSvJdZQzQnBehRQ4vSvX6zjtIU4kyJ4Wkhi9p9I46B61S8nnNYwO/17npFrz bAfTTWazrpoOwwUNJQ2zGp0MWB1X2q939CHpAfNnZkhT+/s8NjmREeqzDljQImMq9BhG Q/Sw== X-Gm-Message-State: APjAAAWGZTfbRYwpp7ss4fZ9pdAJRDul3Ge0qdjZT/yc5JhgQWePRtQQ fQJ4QmhTinI22QuaIbfFLqCcfYIvX1xdP2w/iaDAd9UBthQ= X-Google-Smtp-Source: APXvYqwcQgvKoQPOTLbtXo75CW2NMntqmXGr4+IDl8BcITL59SWor6PrmCgdXLiAen0k4gPB5ulru+yIAZGQ2wWQ0oE= X-Received: by 2002:a6b:d809:: with SMTP id y9mr7659592iob.301.1564628280096; Wed, 31 Jul 2019 19:58:00 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 1 Aug 2019 04:57:48 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000d73e89058f0568a8" Subject: WeakReference: WeakMap / GC callbacks From: benjamin.morel@gmail.com (Benjamin Morel) --000000000000d73e89058f0568a8 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =E2=80=94 a map of (objec= t) > 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 =E2=80=94 an internal implementation can handle it > automagically and more performantly. :) Followed by a message by Joe Watkins the next day : 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 --000000000000d73e89058f0568a8--