Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53994 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6998 invoked from network); 17 Jul 2011 15:40:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jul 2011 15:40:38 -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.170 as permitted sender) X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:33917] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EA/13-02120-572032E4 for ; Sun, 17 Jul 2011 11:40:38 -0400 Received: by iym1 with SMTP id 1so2636752iym.29 for ; Sun, 17 Jul 2011 08:40:34 -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=vu+aQTUqtMJTUZ9K0xb3qoQo2Kmebaoe2mTMzG2d7jY=; b=aZL2uz8YqZ7j4ArN/pBysQDC8YJFHUi8hSanC8wLBuz8bpJCk5FhfAUYL8LiER571/ PLAugQ1ne6yz4bu/Knnnf5P/3tXSOJLX2G4w/uLA46HPURKVMDoKVzSrWNURiYCZQVYJ 5kQUuSL3x6u07iVkoOrU37MJPDbijWcfoWiW8= MIME-Version: 1.0 Received: by 10.231.115.146 with SMTP id i18mr5000016ibq.22.1310917234880; Sun, 17 Jul 2011 08:40:34 -0700 (PDT) Received: by 10.231.37.11 with HTTP; Sun, 17 Jul 2011 08:40:34 -0700 (PDT) In-Reply-To: References: <4E22DDD0.20105@toolpark.com> Date: Sun, 17 Jul 2011 17:40:34 +0200 Message-ID: To: internals@lists.php.net, Ferenc Kovacs , Lars Schultz Content-Type: multipart/alternative; boundary=005045015a661d708a04a845b455 Subject: Re: [PHP-DEV] weak references From: landeholm@gmail.com (Hannes Landeholm) --005045015a661d708a04a845b455 Content-Type: text/plain; charset=ISO-8859-1 On 17 July 2011 12:38, Ferenc Kovacs wrote: > > Hannes, now you should have karma for the rfc namespace, so you can > now extend the article with your suggestions. Great, I'll start contributing ASAP. What's the next step after the RFC is complete? Testing? Voting? On 17 July 2011 15:04, Lars Schultz wrote: > I too would welcome a solution to this problem, I've run into it several > times already and always had to use a semi-satisfactory solution. I hadn't > heard about weak-references before, and I generally like the concept. What I > am not so sure is wether this makes everything alot more complicated for > users who do not know/care about the problem or the solution. Of course the > impact on those users depends on the actual solution. > If you are writing code that caches objects/relations and that caching has a significant impact on memory usage, you need to care about memory allocation/management per definition. So then you have to learn how OOP memory managment works (which should be obligatory anyway if you want to call yourself a OOP-developer IMO). A common argument to why programmers should not start with a garbage collected language is that it prevents them from understanding memory allocation concepts. > I came at the problem from a different angle and came to a different > solution: If I could get the refcount on a certain object, and kept the > object stored centrally in one list, I'd know that if the refcount is down > to 1 (or some other constant) that the object ist not in use anymore. I > believe that this would be quite a simple PHP addition...a simple function > called: get_ref_count() or something and as I remember, that value is always > stored in the zval... > > The only other thing that would help would be a callback (or callable) > which is triggered by PHP reaching a certain memory-limit, absolute or > relative. Then I could clean up memory according to my own business-logic... > > This way, PHP would not feature something totally new, but one could still > solve the problem with some work. > > Do I make any sense at all? Am I missing the point? Anyway, this is an > interesting problem. > > Cheers. > Lars It would probably be simple to implement such functionality. However this would be bad design since it would violate responsibility isolation. PHP should expose as little internal stuff to the userland as possible - unless there are a clear use case for it, and even then it might be a bad idea because it could cause BC breaks in future updates. If you write code that depend on reference counts or memory usage - you're doing something wrong. You either need data or you don't. If you need data you reference it. If you don't reference it the GC will deallocate it. A fundamental part of OOP is designing your program so you never reference stuff you don't need. Weak references solve the special case where you need the reference for as long as you need the data it references (as long as there are strong references for it). You should have a clear reference graph in your application - there is no magic solution to good object oriented design. The only scenario I could imagine where you could "free memory" spontaneously on demand (when the arbitrary OOM limit is reached) - is if that would be cached data. Caching is a very complicated subject that is not directly related to weak references. Start a separate thread about it and present clear use cases if you have anything special in mind. For example, I need weak references to store a table of "back references" between objects for later reference (depends on how you use the objects). If A references B i also need a weak reference from B to A so if B should be replaced with a C object I need the weak reference from B to A to know that A should be updated to point to C instead (if A still exists). This is not related to caching. ~Hannes --005045015a661d708a04a845b455--