Re: concatenation operator
On Thu, 2012-06-07 at 12:53 -0700, Adi Mutu wrote:
> Ok Johannes, thanks for the answer. I'll try to look deeper.
> I basically just wanted to know what happens when you concatenate two
> strings? what emalloc/efree happens.
This depends. As always. As said what has to be done is one allocation
for the result value ... and then the zval magic, which depends on
refcount, references, ...
> Also can you tell me if possible how to put a breakpoint to
> emalloc/efree which are executed only after all core functions are
> registered? because it takes like a million years like this and a
> million F8 presses...
Depends on your debugger. Most allow conditional breakpoints or have a
breakpoint and while holding at some place add a few more ...
For such a question my preference is using DTrace (on Solaris, Mac or
BSD), something like this session:
$ cat test.d
#!/sbin/dtrace
pid$target::concat_function:entry {
self->in_concat = 1;
}
pid$target::execute:return {
self->in_concat = 0;
}
pid$target::_emalloc:entry
/ self->in_concat /
{
trace(arg0);
ustack();
}
pid$target::_erealloc:entry
/ self->in_concat /
{
trace(arg0);
trace(arg1);
ustack();
}
$ cat test1.php
<?php
$a = "foo"; $b = "bar"; $a.$b;
$ dtrace -s test.d -c 'php test1.php'
dtrace: script 'test.d' matched 4 probes
dtrace: pid 16406 has exited
CPU ID FUNCTION:NAME
3 100372 _emalloc:entry 7
php`_emalloc
php`concat_function+0x270
php`ZEND_CONCAT_SPEC_CV_CV_HANDLER+0xcd
php`execute+0x3d9
php`dtrace_execute+0xe7
php`zend_execute_scripts+0xf5
php`php_execute_script+0x2e8
php`do_cli+0x864
php`main+0x6e2
php`_start+0x83
$ cat test2.php
<?php
$a = 23; $b = "bar"; $a.$b;
$ dtrace -s test.d -c 'php test2.php'
dtrace: script 'test.d' matched 4 probes
dtrace: pid 16425 has exited
CPU ID FUNCTION:NAME
1 100373 _erealloc:entry 0 79
php`_erealloc
php`xbuf_format_converter+0x11ee
php`vspprintf+0x34
php`zend_spprintf+0x2f
php`_convert_to_string+0x174
php`zend_make_printable_zval+0x5ec
php`concat_function+0x3c
php`ZEND_CONCAT_SPEC_CV_CV_HANDLER+0xcd
php`execute+0x3d9
php`dtrace_execute+0xe7
php`zend_execute_scripts+0xf5
php`php_execute_script+0x2e8
php`do_cli+0x864
php`main+0x6e2
php`_start+0x83
1 100372 _emalloc:entry 6
php`_emalloc
php`concat_function+0x270
php`ZEND_CONCAT_SPEC_CV_CV_HANDLER+0xcd
php`execute+0x3d9
php`dtrace_execute+0xe7
php`zend_execute_scripts+0xf5
php`php_execute_script+0x2e8
php`do_cli+0x864
php`main+0x6e2
php`_start+0x83
So, when having two constant strings there's a single malloc, in this
case allocating 7 bytes (strlen("foo")+strlen("bar")+1), if you have a
different type it has to be converted first ...
johannes
Thread (14 messages)