Req #76827 [Com]: Introduce a new function struncate or some such

From: Date: Fri, 31 Aug 2018 09:04:39 +0000
Subject: Req #76827 [Com]: Introduce a new function struncate or some such
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216826@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76827&edit=1 ID: 76827 Comment by: zjz at zjz dot name Reported by: zjz at zjz dot name Summary: Introduce a new function struncate or some such Status: Open Type: Feature/Change Request Package: Performance problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I think I understand the copy-on-write nature, I am just not sure it can avoid copying **if the substr can optimise out** the copying when the variable that receives the returned value is as the same one as the passed parameter. I haven't right now looked into the C code, but from what I guess, substr doesn't know before hand if the variable that receives the returned value is as the same one as the passed parameter, so the process I guess is like the following: Step 1. Copying the string by the truncated length. (in the process of calling substr) Step 2. Do the assignment(in the process of $str = substr()), now the original string is dereferenced and its reference count is zero, so it's freed in this step. So I want to repeat what I said in my previous comment: is PHP smart in that if the variable that receives the returned value is as the same as the one passed as the parameter, it can avoid copying? Previous Comments: ------------------------------------------------------------------------ [2018-08-31 08:46:19] spam2 at rhsoft dot net why the hell should it copy the param and why do you think a reference is helpful? http://www.phpinternalsbook.com/zvals/memory_management.html http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html ------------------------------------------------------------------------ [2018-08-31 08:42:25] zjz at zjz dot name Thanks for the reply. So if substr doesn't do copying, then what if I use a different variable(say $str1) to receive the truncated string from $str. $str1 = substr($str, 0, -1); Doesn't it do copying? Or did you mean that PHP is smart in that if the variable that receives the returned value is as the same as the one passed as the parameter, it can avoid copying? ------------------------------------------------------------------------ [2018-08-31 08:31:41] spam2 at rhsoft dot net $str = substr($str, 0, -1); why do you think that does copy the whole string? function struncate(&$string, $length) and why do you think that would gain anything? for me it looks you don't understand the copy-on-write nature of the zendengine and that references in PHP in most cases don't do what you think and hence are often bad ------------------------------------------------------------------------ [2018-08-31 08:30:00] zjz at zjz dot name Please consider introducing a new function |struncate| or some such to resolve the performance problem of truncating a very long string to a slightly shorter one. For example, suppose the user wants to truncate a 50000 byte string to a 49999 byte string(i.e. to delete the last character), and **the untruncated one is no longer in use**. Right now the user has to write code as such: $str = substr($str, 0, -1); However, this is silly, it is rather bad in performance when the string copying is not necessary as the untruncated one is no longer in use as mentioned above. So I'd like to suggest adding a new function |struncate|, which receives two parameters, one is a reference of a string to be truncated, the other is the truncated length: function struncate(&$string, $length) ------------------------------------------------------------------------ [2018-08-31 08:27:37] zjz at zjz dot name Description: ------------ Please consider introducing a new function |struncate| or some such to resolve the performance problem of truncate a very long string to a slightly shorter one. For example, suppose the user wants to truncate a 50000 byte string to a 49999 byte string(i.e. to delete the last character), and **the untruncated one is no longer in use**. Right now the user has to write code as such: $str = substr($str, 0, -1); However, this is silly, it is rather bad in performance when the string copying is not necessary as the untruncated one is no longer in use as mentioned above. So I'd like to suggest adding a new function |struncate|, which receives two parameters, one is a reference of a string to be truncated, the other is the truncated length: function struncate(&$string, $length) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76827&edit=1

« previous php.bugs (#216826) next »