Re: [PHP-DEV] weak references
Am 16.07.2011 13:23, schrieb Ferenc Kovacs: we have debug_zval_dump but it is hard to use correctly Well, it's not a simple problem then is it? It wouldn't be too hard if you understood the source of the problem... with weak references, you wouldn't need to worry about keeping a count of the refcount only check the valid() method. yeah...a magic bullet is cool, but you could, to quote a contributor to this list, shoot yourself in the foot;) for your last paragraph: that's totally unrelated to weak references, but you could achive what you want through memory_get_usage() + You're right. It's related to one of the usecases of weak-references though...and your proposition wouldn't works ince I'd have to check my memory-usage all the time...that's what events are for. btw. if I remember correctly, somebody requested a non-fatal callback when memory limit is reached callback functionality, I have to look that up. By then it's probably too late since I don't have any memory left to run my cleanup code. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Am 17.07.2011 17:40, schrieb Hannes Landeholm: 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. Right. That's what I am talking about...a callback on high-memory consumption and a way to check which objects I could liberate from my cache. 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). I do call myself an OOP-developer but maybe I shouldn't, because I've never heard of OOP Memory Management. Would you care to explain in private? A common argument to why programmers should not start with a garbage collected language is that it prevents them from understanding memory allocation concepts. The other argument being that if you're not using a gc-language, it might prevent you from starting at all. 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. I agree with you that it's not an ideal solution. But my solution would not introduce a completely new concept to the language, but would allow advanced PHP-users to write their own custom bred solution. = give the language a rest If you write code that depend on reference counts or memory usage - you're doing something wrong. Why?...because it's not discrete? I am only taking advantage of what is already there... 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. Exactly the case I wanted to cover. 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. I know but WeakReferences (or SoftReferences) would be a solution to this, right? It's not unrelated. 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. I am looking forward to the RFCs usecases...this sounds interesting. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Lars: Please don't break up my sentences and take them out of context before you reply to them. (Hint: If a sentence begins with so it's not a good idea to just reply to that sentence.) If you are caching stuff you would rather want to use a strong reference since the objects should be retained between usages _per definition_. You might exploit weak references + destructor resurrection to achieve some kind of on-demand, unorthodox caching mechanism but that would not be a primary use cache for weak references, nor what you suggested (deallocate objects when OOM is reached). I have also given an example of how weak references is not necessarily used for caching, therefore not directly related to it. If you want to continue to discuss on demand deallocation, I suggest that you start a separate thread. I hope this will be more clear once the RFC is complete. I will then start a separate thread for official discussion. ~Hannes
Re: [PHP-DEV] weak references
Am 18.07.2011 09:33, schrieb Hannes Landeholm: Lars: Please don't break up my sentences and take them out of context before you reply to them. (Hint: If a sentence begins with so it's not a good idea to just reply to that sentence.) Sorry for misquoting you. I intended to make it clear to which passage I was replying. If you are caching stuff you would rather want to use a strong reference since the objects should be retained between usages _per definition_. You might exploit weak references + destructor resurrection to achieve some kind of on-demand, unorthodox caching mechanism but that would not be a primary use cache for weak references, nor what you suggested (deallocate objects when OOM is reached). I have also given an example of how weak references is not necessarily used for caching, therefore not directly related to it. If you want to continue to discuss on demand deallocation, I suggest that you start a separate thread. Why unorthodox? and isn't caching mostly done on-demand? Also SoftReferences would be perfect for caching since that data would be freed if memory consumption is high...but here I am, arguing your case. I hope this will be more clear once the RFC is complete. I will then start a separate thread for official discussion. All I wanted was to point out that introducing such a thing might overly complicate things for users not familiar with the concept...as the java guy pointed out in his intro, even java-people don't know about the feature, even though it's been around for 10 years (http://weblogs.java.net/blog/2006/05/04/understanding-weak-references). I'll be happy to read your RFC. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
I hope this will be more clear once the RFC is complete. I will then start a separate thread for official discussion. All I wanted was to point out that introducing such a thing might overly complicate things for users not familiar with the concept...as the java guy pointed out in his intro, even java-people don't know about the feature, even though it's been around for 10 years (http://weblogs.java.net/blog/2006/05/04/understanding-weak-references). I think that having to know and care about refcounts and zvals are more complicated than having an Spl class, which can hold a reference for a variable what can be destroyed to free memory. and there is a chance that people are familiar with the Weak references from other languages, while the zval approach only familiar for those that knows about php internals. so I think that from the userland POV, weak references are easier to grasp. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Am 18.07.2011 10:15, schrieb Ferenc Kovacs: I think that having to know and care about refcounts and zvals are more complicated than having an Spl class, which can hold a reference for a variable what can be destroyed to free memory. and there is a chance that people are familiar with the Weak references from other languages, while the zval approach only familiar for those that knows about php internals. so I think that from the userland POV, weak references are easier to grasp. You're right for users who indeed have this problem. But I am worried about those users, which do not have the problem, but still have to know what WeakReferences are...but I should wait for the RFC. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
While I like SplWeekRef and the somewhat proposed SplWeekRefList, you’ll find attached a patch that exposes a simple function „refcount“ having this signature: int refcount(mixed value) So if you would like to play around with it, have fun. It would be interesting to see if there are any other use cases besides destroying a reference. An idea for a userland implementation of SplWeekRefList with auto cleanup: let SplWeekRefList register a ticket function which executes cleanup with a certain propability. Something along the lines of this: ?php declare(ticks=1); class WeekRefList extends SplObjectStorage { public function __construct() { register_tick_function(array($this, 'cleanup')); } public function cleanup() { if (rand(1, 10) === 1) { foreach ($this as $entry) { // First ref is in ObjectStorage, second is $entry var if (refcount($entry) === 2) { unset($this[$entry]); } } } } } With regards, Lars Am 18.07.11 10:28 schrieb Lars Schultz unter lars.schu...@toolpark.com: Am 18.07.2011 10:15, schrieb Ferenc Kovacs: I think that having to know and care about refcounts and zvals are more complicated than having an Spl class, which can hold a reference for a variable what can be destroyed to free memory. and there is a chance that people are familiar with the Weak references from other languages, while the zval approach only familiar for those that knows about php internals. so I think that from the userland POV, weak references are easier to grasp. You're right for users who indeed have this problem. But I am worried about those users, which do not have the problem, but still have to know what WeakReferences are...but I should wait for the RFC. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php Index: Zend/zend_builtin_functions.c === --- Zend/zend_builtin_functions.c (revision 313122) +++ Zend/zend_builtin_functions.c (working copy) @@ -94,6 +94,7 @@ static ZEND_FUNCTION(gc_enabled); static ZEND_FUNCTION(gc_enable); static ZEND_FUNCTION(gc_disable); +static ZEND_FUNCTION(refcount); /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO(arginfo_zend__void, 0) @@ -237,6 +238,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_extension_loaded, 0, 0, 1) ZEND_ARG_INFO(0, extension_name) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_refcount, 0, 0, 0) + ZEND_ARG_INFO(0, object) +ZEND_END_ARG_INFO() /* }}} */ static const zend_function_entry builtin_functions[] = { /* {{{ */ @@ -306,6 +311,7 @@ ZEND_FE(gc_enabled, arginfo_zend__void) ZEND_FE(gc_enable, arginfo_zend__void) ZEND_FE(gc_disable, arginfo_zend__void) + ZEND_FE(refcount, arginfo_refcount) { NULL, NULL, NULL } }; /* }}} */ @@ -385,6 +391,19 @@ } /* }}} */ +/* {{{ proto int refcount(mixed value) + Returns the refcount for the passed object */ +ZEND_FUNCTION(refcount) +{ + zval *value; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, z, value) == FAILURE) { + return; + } + + RETURN_LONG(Z_REFCOUNT_P(value) - 1); +} + /* {{{ proto int func_num_args(void) Get the number of arguments that were passed to the function */ ZEND_FUNCTION(func_num_args) -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On Sat, Jul 16, 2011 at 12:11 PM, Hannes Landeholm landeh...@gmail.com wrote: I was going to add some content to https://wiki.php.net/rfc/weakreferencesnow but the wiki tells me that the page is read only for me. I couldn't find any details on who to contact to change this. ~Hannes Hannes, now you should have karma for the rfc namespace, so you can now extend the article with your suggestions. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
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. 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On Sun, Jul 17, 2011 at 3:04 PM, Lars Schultz lars.schu...@toolpark.com 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. 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php we have debug_zval_dump but it is hard to use correctly (see the notes) and xdebug has xdebug_debug_zval which is slightly better, but it is not part of the language. with weak references, you wouldn't need to worry about keeping a count of the refcount only check the valid() method. for your last paragraph: that's totally unrelated to weak references, but you could achive what you want through memory_get_usage() + unsetting your unnedid variables and maybe calling gc_collect_cycles(). btw. if I remember correctly, somebody requested a non-fatal callback when memory limit is reached callback functionality, I have to look that up. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On 17 July 2011 12:38, Ferenc Kovacs tyr...@gmail.com 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 lars.schu...@toolpark.com 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
Re: [PHP-DEV] weak references
hi, On Sun, Jul 17, 2011 at 5:40 PM, Hannes Landeholm landeh...@gmail.com wrote: On 17 July 2011 12:38, Ferenc Kovacs tyr...@gmail.com 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? See https://wiki.php.net/rfc/voting So 1st discussing then voting ;) On 17 July 2011 15:04, Lars Schultz lars.schu...@toolpark.com 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 -- Pierre @pierrejoye | http://blog.thepimp.net | http://www.libgd.org -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Hello, I just realized you could actually utilize the __destruct method to catch the event I described in my last post. The SplWeakRef class as currently defined in the RFC would therefore suffice to build a userland class that automatically garbage collects indexes. ~Hannes
Re: [PHP-DEV] weak references
I was going to add some content to https://wiki.php.net/rfc/weakreferencesnow but the wiki tells me that the page is read only for me. I couldn't find any details on who to contact to change this. ~Hannes
Re: [PHP-DEV] weak references
On 14 July 2011 20:42, Ferenc Kovacs tyr...@gmail.com wrote: To spare the 5.4 features vote thread from the off-topic discussion, we can continue here. Hannes Landeholm brought this idea up, see http://news.php.net/php.internals/53956 and http://news.php.net/php.internals/53959 I would suggest that Hannes create an rfc, and add some examples and introduce the weak references in general and how this can be useful, what problems do they solve (it was discussed in the bugreport, but it wasn't clear as far as I can tell from the comments). Not being an expert here, so the questions may have obvious answers. How often does the gc cycle run during a script? Can preemptive cleanup actually cause more of a headache? In reading about weak references on http://en.wikipedia.org/wiki/Weak_reference, weak references look like a perfect use case for memory limited caches, but I wonder if there would be a way of synching a TTL to the weak referenced objects, so that they essentially automatically expire (or can be expired and expunged). A system controlled TTL for weak referenced objects. So, void SplWeakRef::__construct(object $ref, [int $ttl = -1]) $ttl = -1 would allow the object to be gc'd in the normal way, but any other value would be a hint to how long the object would be expected to last for. If gc happens frequently, then $ttl acts as a limiter and I think this could be very useful. -- Richard Quadling Twitter : EE : Zend : PHPDoc @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On Sat, Jul 16, 2011 at 12:14 PM, Richard Quadling rquadl...@gmail.com wrote: On 14 July 2011 20:42, Ferenc Kovacs tyr...@gmail.com wrote: To spare the 5.4 features vote thread from the off-topic discussion, we can continue here. Hannes Landeholm brought this idea up, see http://news.php.net/php.internals/53956 and http://news.php.net/php.internals/53959 I would suggest that Hannes create an rfc, and add some examples and introduce the weak references in general and how this can be useful, what problems do they solve (it was discussed in the bugreport, but it wasn't clear as far as I can tell from the comments). Not being an expert here, so the questions may have obvious answers. How often does the gc cycle run during a script? First of all, the whole reason for implementing the garbage collection mechanism is to reduce memory usage by cleaning up circular-referenced variables as soon as the prerequisites are fulfilled. In PHP's implementation, this happens as soon as the root-buffer is full, or when the function gc_collect_cycles() is called. In the graph below, we display the memory usage of the script below, in both PHP 5.2 and PHP 5.3, excluding the base memory that PHP itself uses when starting up. http://www.php.net/manual/en/features.gc.performance-considerations.php#features.gc.performance-considerations.reduced-mem Can preemptive cleanup actually cause more of a headache? In reading about weak references on http://en.wikipedia.org/wiki/Weak_reference, weak references look like a perfect use case for memory limited caches, but I wonder if there would be a way of synching a TTL to the weak referenced objects, so that they essentially automatically expire (or can be expired and expunged). A system controlled TTL for weak referenced objects. So, void SplWeakRef::__construct(object $ref, [int $ttl = -1]) $ttl = -1 would allow the object to be gc'd in the normal way, but any other value would be a hint to how long the object would be expected to last for. If gc happens frequently, then $ttl acts as a limiter and I think this could be very useful. the current implementation doesn't change the way that gc works. it only stores the reference in such a way, than the gc can free the object, if nothing else references it. of course we could improve the gc mechanism, as you suggested, but I think that would be a more complex and risky, so maybe that would be a feature-creep for now. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On Sat, Jul 16, 2011 at 12:11 PM, Hannes Landeholm landeh...@gmail.com wrote: I was going to add some content to https://wiki.php.net/rfc/weakreferencesnow but the wiki tells me that the page is read only for me. I couldn't find any details on who to contact to change this. ~Hannes I already asked bjori (Hannes Magnusson) to give you karma to the rfc namespace in the wiki. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
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: quote 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. /quote 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 SplWeakRef::__construct(object ref, SplQueue ref_queue = null) 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 = 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. ~Hannes -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
On Sat, Jul 16, 2011 at 1:13 PM, Hannes Landeholm landeh...@gmail.com wrote: 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: quote 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. /quote 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 SplWeakRef::__construct(object ref, SplQueue ref_queue = null) 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 = 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. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
I'm not familiar with the internal implementation of GC in PHP but if the current garbage collection implementation makes catching the gc event non-trivial I'd agree and vote for postponing my SplQueue ref_queue suggestion until the future. It could always be added later and there is the temporary __destruct workaround for the meantime. ~Hannes On 16 July 2011 13:23, Ferenc Kovacs tyr...@gmail.com wrote: 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. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Hi, On Thu, Jul 14, 2011 at 21:42, Ferenc Kovacs tyr...@gmail.com wrote: To spare the 5.4 features vote thread from the off-topic discussion, we can continue here. Hannes Landeholm brought this idea up, see http://news.php.net/php.internals/53956 and http://news.php.net/php.internals/53959 I would suggest that Hannes create an rfc, and add some examples and introduce the weak references in general and how this can be useful, what problems do they solve (it was discussed in the bugreport, but it wasn't clear as far as I can tell from the comments). if I understand this right, Hannes proposed adding a new Spl class(interface?), which can store and retrive a collection of variables without incrementing the refcounts, so they can be freed if they aren't used other than in this collection. Hannes please correct me if I'm wrong somewhere. Some draft here: https://wiki.php.net/rfc/weakreferences With a patch implementing some magic for a POC here http://patches.colder.ch/php-src/weakref-trunk.patch?markup. It still requires some work though. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Etienne Kneuss http://www.colder.ch -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] weak references
Hi, Ferenc: Good idea to move the discussion to a separate thread. Etienne: Awesome. I wish I had time to write the patch myself. I could probably put some hours into adding more details to the RFC though. The only problem as I see it by making it a weak reference class instead of a weak reference collection is that you would end up with a bunch of unnecessary indexes in the array that points to invalid references. The simplest solution to this problem if you still want to implement it as you have begun to is to allow catching the cleanup event in a callback so you can manually unset the index. Should I add these concerns to the RFC? Thanks a bunch, Hannes
[PHP-DEV] weak references
To spare the 5.4 features vote thread from the off-topic discussion, we can continue here. Hannes Landeholm brought this idea up, see http://news.php.net/php.internals/53956 and http://news.php.net/php.internals/53959 I would suggest that Hannes create an rfc, and add some examples and introduce the weak references in general and how this can be useful, what problems do they solve (it was discussed in the bugreport, but it wasn't clear as far as I can tell from the comments). if I understand this right, Hannes proposed adding a new Spl class(interface?), which can store and retrive a collection of variables without incrementing the refcounts, so they can be freed if they aren't used other than in this collection. Hannes please correct me if I'm wrong somewhere. -- Ferenc Kovács @Tyr43l - http://tyrael.hu -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] Weak references - object destruction callback?
Hi I try to write a a weak reference extension for php5.3 to break circular object structures, so far I have a test implementation that seems to work fine with my first tests. My current system uses a new base class that returns a new resource via a getWeakRef method. This resource can be converted back to a new zval pointing to the object, or returning false if the object already is destroyed. So far so good, basic tests seems to work correct, the zend engine doesn't get screwed. But general I would want to avoid using a base class all together, is there any way to get notified when a object gets out of scope (preferred without changing Zend internals)? I couldn't find a good answer by reading the code, changing the handler tables on the fly doesn't feel like the right solution. I am curious what is the purpose of the zend_objects_proxy apis? Converting resources directly to an object by type cast would be nice and would avoid using a function for this, could this made somehow possible? regards Oskar -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php