Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53983 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42124 invoked from network); 16 Jul 2011 11:23:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2011 11:23:14 -0000 Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.83.42 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 74.125.83.42 mail-gw0-f42.google.com Received: from [74.125.83.42] ([74.125.83.42:44615] helo=mail-gw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/F0-34288-1A4712E4 for ; Sat, 16 Jul 2011 07:23:13 -0400 Received: by gwb17 with SMTP id 17so914718gwb.29 for ; Sat, 16 Jul 2011 04:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=IIAwbowFBOFarnh8D/dsOCxdTgtxqGNbceWZM6yYb6U=; b=FTtwlKFijSs/bIDKAh82QEgYtM0Bx7SccmnIVZqQDVVK7s7XtrJwHL5wwOAuP40MWx 00PIiwwVUO+J67ZSFkE+kiZTW3ChZZlvqxuwF0FWGvfY/VTE8OX6yQzAZUcXZjxDz5l7 5f6XyRom40uiXCL0smBUbe1vQCiM9KM1u8kEM= MIME-Version: 1.0 Received: by 10.146.3.40 with SMTP id 40mr4084946yac.33.1310815388817; Sat, 16 Jul 2011 04:23:08 -0700 (PDT) Received: by 10.147.38.17 with HTTP; Sat, 16 Jul 2011 04:23:08 -0700 (PDT) In-Reply-To: References: Date: Sat, 16 Jul 2011 13:23:08 +0200 Message-ID: To: Hannes Landeholm Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] weak references From: tyra3l@gmail.com (Ferenc Kovacs) On Sat, Jul 16, 2011 at 1:13 PM, Hannes Landeholm wro= te: > Since Weak references is already an established concept in many OOP > languages a lot can be learned from their implementation. A lot of the > potential related problems and solutions has already been studied. > Anyone who wish to contribute to this feature should start by reading > this excellent article by Ethan Nicholas: > > http://weblogs.java.net/blog/2006/05/04/understanding-weak-references > > For example I previously stated that I wanted a callback to clean up > the weak reference object and indexes in the collection - then came to > the conclusion that __destruct method would suffice for this. Then I > read this part: > > > Reference queue > Once a WeakReference starts returning null, the object it pointed to > has become garbage and the WeakReference object is pretty much > useless. This generally means that some sort of cleanup is required; > WeakHashMap, for example, has to remove such defunct entries to avoid > holding onto an ever-increasing number of dead WeakReferences. > > The ReferenceQueue class makes it easy to keep track of dead > references. If you pass a ReferenceQueue into a weak reference's > constructor, the reference object will be automatically inserted into > the reference queue when the object to which it pointed becomes > garbage. You can then, at some regular interval, process the > ReferenceQueue and perform whatever cleanup is needed for dead > references. > > > Java utilizes a "ReferenceQueue" that tracks dead references so you > can process and clean up dead references which is an excellent design > solution. The Queue part is not that important, the references might > as well be organized in a LIFO than a FIFO but a FIFO conceptually > more fitting. Imaging dead references standing in a queue, awaiting > cleanup. > > The solution is also superior to the __destruct solution since you > would have to extend or modify the class and override the __destruct > method. It makes it impossible to separate the "how the instances is > externally stored"-design from the actual class itself, ergo: you > can't cache classes in external libraries without hacking the > libraries. > > I therefore propose changing the constructor (and implementation) to: > > void =C2=A0 SplWeakRef::__construct(object ref, SplQueue ref_queue =3D nu= ll) > > If the SplWeakRef is given a ref_queue, it will call > ref_queue->push($this) whenever it becomes invalid. If additional > cleanup is required (if you want to catch the event and do cleanup > immediately) one could simply extend SplQueue and overload > SplQueue::push(). > > Note that resurrection must be avoided so the SplWeakRef does not flip > between valid -> invalid and then turns valid again (this would be a > confusing behavior which can lead to unintentional behavior =3D bugs). > The implementation should therefore make sure the SplWeakRef does not > turn invalid before __destruct has been run for the object since > __destruct can resurrect an object. > Etienne Kneuss has more knowledge about the implementation, but AFAIK as we didn't modified the garbage collector we didn't get notified if a weakreferenced object gets gced, so we can't notify the SplQueue either. please be aware that the bigger the change that we introduce it is less likely that we can make this happen in 5.4, or at all. --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu