Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54403 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62232 invoked from network); 5 Aug 2011 12:58:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Aug 2011 12:58:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=john.lesueur@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=john.lesueur@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.170 as permitted sender) X-PHP-List-Original-Sender: john.lesueur@gmail.com X-Host-Fingerprint: 209.85.220.170 mail-vx0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:37123] helo=mail-vx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6C/32-43800-ED8EB3E4 for ; Fri, 05 Aug 2011 08:58:06 -0400 Received: by vxh24 with SMTP id 24so1215282vxh.29 for ; Fri, 05 Aug 2011 05:58:03 -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=nmFh0gbwwTfyXTkr3oKi4dtlaZVqS8J/7+hi7y3CEEk=; b=g2QLc735+4ihaRH2J+sOWBH3RcnBMeJd1dvKJj8wx7owrOAiCn36JEanLWUreyCXHR 8nTYs8lSUY8w2GpMn/MRpiT043X1tGFlD8feWFRCI2mUAfSNaODeyG9+kgN0qDkPFtOR H6zw9T0+dZcmY3pINobefTarjsOIGbkuFXLj4= MIME-Version: 1.0 Received: by 10.52.69.108 with SMTP id d12mr2248519vdu.180.1312549083481; Fri, 05 Aug 2011 05:58:03 -0700 (PDT) Received: by 10.52.183.234 with HTTP; Fri, 5 Aug 2011 05:58:03 -0700 (PDT) In-Reply-To: References: <4E3898B0.40809@sugarcrm.com> <4E38EC0C.9080304@lerdorf.com> <4E38FA2E.4030605@lsces.co.uk> <4E38FC67.9090200@toolpark.com> <4E39E89F.8060605@sugarcrm.com> <4E3A3643.2070305@toolpark.com> <4E3A4793.2070209@sugarcrm.com> <4E3A91E8.1020107@toolpark.com> <4E3ACA42.8000001@sugarcrm.com> Date: Fri, 5 Aug 2011 06:58:03 -0600 Message-ID: To: Ferenc Kovacs Cc: Stas Malyshev , Lars Schultz , "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [VOTE] Weak References From: john.lesueur@gmail.com (John LeSueur) On Thu, Aug 4, 2011 at 2:53 PM, Ferenc Kovacs wrote: > > On Thu, Aug 4, 2011 at 6:35 PM, Stas Malyshev wr= ote: > > Hi! > > On 8/4/11 5:34 AM, Lars Schultz wrote: > >>> > >>> Do not keep object references, keep object IDs. This would make your > >>> code a bit more verbose and a bit slower, but weak refs would > >>> essentially do the same anyway. > >> > >> This is like saying: do not use objects at all and use the DB for > >> storage. verbosity and slowness is something I'd like to prevent. > > > > No, it's not even remotely like that. Using one intermediary function a= nd > > doing the DB call is orders of magnitude apart. You asked how you can s= olve > > the problem, I showed you how. You can claim you don't like the solutio= n, > > that's fine, everybody has his own taste. But you can't claim there's n= o > > other solution. > > maybe it's just me, but I not fully understand your solution. > > "I'm not sure I understand why you need week refs there - can't you > just always use $prodDb->getProduct(1) and when you don't need it > anymore just do $prodDb->drop(1)? Or let it drop it whenever it wants > to?" > "Do not keep object references, keep object IDs. This would make your > code a bit more verbose and a bit slower, but weak refs would > essentially do the same anyway." > > from that, my guess is that you propose that nobody should hold a > reference for the product, but always use =A0the returned object from > getProduct() on the fly. > you also suggest that ProductDatabase should also remove the cached > object when appropriate. > did I get interpreted your solution correctly? > > I have some problems with this approach: > - you cannot guarantee/force the contract that the result of the > getProduct cannot be stored. > - in a loosely coupled system, it is hard to tell when "you don't need > it anymore". > as you and Lars both mentioned we only care about freeing the cache, > because we have memory constraint. > Lars mentioned that currently he manually checks the memory usage, and > free the cache if needed: > http://www.mail-archive.com/internals@lists.php.net/msg52423.html > of course one could also write some algorithm, which removes records > from the cache either randomly, or as Jonathan suggestion doing > something like LRU/MRU. > with Weak References, you shouldn't care about that, the gc would do > it for your, albeit in a less sophisticated way: if the gc root buffer > is full, then free every item which isn't used at that time. > and of course you can combine the WeakRefs and some cache algorithm as > Jan did in his blogpost. (good article btw. and also good to see that > Benjamin Eberlei from the Doctrine project looking into the possible > use cases for them) > > so as I see you didn't really addressed the proposed use-case of the > Weak References, just stated what everybody is aware of: one can cache > and free objects "manually" without the need of Weak References. > and while that is true, I still think that this could be a great asset > for creating high quality components. > To make sure I understand the advantages of weak references paired with caching, this is what I understand: 1) While in the size-limited cache, an object would never be garbage-collec= ted. 2) If the size-limited cache decides to prune an object, and the object is still being referenced in a variable, the weak reference map will allow you to av= oid making a call to the db, and avoid having a duplicate object. 3) If the size-limited cache decides to prune an object, and the object is = not referenced anywhere, the object may be gc'ed at any time, meaning the weak reference may not be valid(). However, there may be some time between garbage collection that the object is still available if the code requests a new instance of the object. This behavior could not be relied on, but might avo= id some small amount of traffic to the db. So the advantage is mostly in case 2, where we avoid the duplication of an object. For example, in code like this(which is obviously silly, but given a more complex application might happen): function foo() { $bar =3D Bar::get(1); $bar->baz =3D 2 anotherFoo(); $bar->save(); } function anotherFoo() { $bar =3D Bar::get(1); $bar->baz =3D 4; $bar->save(); } If the cache pruned the Bar object with id 1 before anotherFoo() was called, without weak references, we would have two different $bar objects. With weak references, they still refer to the same object. Are there other advantages I'm missing? If not, I see some advantage to Weak References, but I'm glad to be playing with them in PECL to see exactly how much real advantage they would give. Thanks, John