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

From: Date: Fri, 31 Aug 2018 10:39:09 +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-216832@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:

The general idea I suggested is directly modifying the **len** value of the str:

TBC, I am not familar with PHP source code, so this code I write here can't be the exactly
formal code, but simply demostrates the general idea I meant:

PHP_FUNCTION(struncate)
{
	zend_string* str;
	zend_long    len;
	int argc = ZEND_NUM_ARGS();

	ZEND_PARSE_PARAMETERS_START(...)
		Z_PARAM_STR(str)
		Z_PARAM_LONG(len)
	ZEND_PARSE_PARAMETERS_END();

	str->len = len;
}


Previous Comments:
------------------------------------------------------------------------
[2018-08-31 10:34:01] zjz at zjz dot name

Well. Let me try to look into the C code and then gives some code example to explain what I said.

------------------------------------------------------------------------
[2018-08-31 09:56:40] zjz at zjz dot name

$str = substr($str, 0 , -1); first makes a new string by the truncated length, and then frees the
original string, which seems to me uncessary.

As to why I suggest this new function with a reference parameter, **I didn't AT ALL try to mean
that referencing itself is more effient**, but I just meant in the new function, **something magic**
can be done in C code in the new function, so that the original string can be **directly**
truncated, without making a new truncated string in another place at first, and then freeing the
original one, which is uncessary.

------------------------------------------------------------------------
[2018-08-31 09:08:10] spam2 at rhsoft dot net

you miss the point:

a new function with a reference would gain *nothing* which couldn't be done in substr() too
without clutter the function list

------------------------------------------------------------------------
[2018-08-31 09:04:39] zjz at zjz dot name

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?

------------------------------------------------------------------------
[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

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=76827


--
Edit this bug report at https://bugs.php.net/bug.php?id=76827&edit=1


Thread (15 messages)

« previous php.bugs (#216832) next »