Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54409 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98541 invoked from network); 5 Aug 2011 16:29:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Aug 2011 16:29:48 -0000 Authentication-Results: pb1.pair.com header.from=landeholm@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=landeholm@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.172 as permitted sender) X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.210.172 mail-iy0-f172.google.com Received: from [209.85.210.172] ([209.85.210.172:48040] helo=mail-iy0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/12-21514-B7A1C3E4 for ; Fri, 05 Aug 2011 12:29:48 -0400 Received: by iye7 with SMTP id 7so3201045iye.31 for ; Fri, 05 Aug 2011 09:29:45 -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 :content-type; bh=T7q/oiSJkepdSyiC8UTDsQoy7u6QGrkc1kHr3d6Wb0s=; b=Rr2zstXoRAR8uPILfNkg1DJ40TMVqj8WAoNHYs9LJrQKKNCYHc7fw0HGMWifL6byei 3X32bd0WD6TYV6DhWPXyrG8YbpBfa613SOeZ0I4tqtsxAUBMoJW9aThtS20dTwzc2tXp ugIZO3Do49hHEK6QOZGkZf0mlsHGf8Tcpc6TM= MIME-Version: 1.0 Received: by 10.231.69.136 with SMTP id z8mr1936952ibi.21.1312561785105; Fri, 05 Aug 2011 09:29:45 -0700 (PDT) Received: by 10.231.37.129 with HTTP; Fri, 5 Aug 2011 09:29:44 -0700 (PDT) In-Reply-To: <4E3BF930.9030701@toolpark.com> 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> <4E3BF930.9030701@toolpark.com> Date: Fri, 5 Aug 2011 18:29:44 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=0015176f1aaef26c4604a9c49a60 Subject: Re: [PHP-DEV] [VOTE] Weak References From: landeholm@gmail.com (Hannes Landeholm) --0015176f1aaef26c4604a9c49a60 Content-Type: text/plain; charset=ISO-8859-1 Soft references could theoretically be implemented if PHP only implemented weak references even though the implementation would consist of a very ugly and "coincidental" hack. Lets say you had an object A that you wouldn't want to be collected until approximately when PHP needs more memory. E.g. we want to implement some sort of caching behavior. Note the difference towards weak references which allows objects to be collected directly when they are not strongly referenced anymore. In the caching scenario we don't know weather the object might be used again so we want it to be retained in memory as long as it's not a problem - e.g. as long as PHP doesn't use "too much" memory. Now after object A is created we create a weak reference for it in our cache. Then we create another object B which is just a StdClass with a single field "ptr" which is a strong reference to A. Then we create a strong reference from A to B and so a cyclic reference memory structure is born. A (and B) will now be unable to be garbage collected until gc_collect_cycles runs which happens when the "root buffer is full" - piggybacking on PHP's internal approximation of when enough memory has been allocated that the probability is high enough for garbage cycle collection to be worth it. This also assumes that you can add an arbitrary property to the A object without disrupting it's behavior. Adding arbitrary properties to other objects violates encapsulation and is generally not good design. Also since this implementation of a soft reference depends on internal PHP implementation I would avoid such design though unless careful testing is made and the behavior is disabled if an incremented PHP version is detected where there is a possibility that the PHP GC implementation might have changed. It's interesting to think about though. Also I would not rely on "ticks" for anything really except debugging. It's an unreliable performance killer. ~Hannes --0015176f1aaef26c4604a9c49a60--