[php-src] Issue #15262: Why internal function is slower than my own doing the same?
| From: | 6562680 | Date: | Tue, 06 Aug 2024 21:21:06 +0000 |
| Subject: | [php-src] Issue #15262: Why internal function is slower than my own doing the same? | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247449@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15262
Author: 6562680
### Description
The following code:
```php
<?php
function parse_string_one($value) : ?string
{
static $fn;
$fn = $fn ?? (static function (string $value) {
return $value;
});
if (is_string($value)) {
return $value;
}
try {
$_value = $fn($value);
}
catch ( \Throwable $e ) {
return null;
}
return $_value;
}
function parse_string_two($value) : ?string
{
if (is_string($value)) {
return $value;
}
if (
null === $value
|| is_array($value)
|| is_resource($value)
) {
return null;
}
if (is_object($value)) {
if (method_exists($value, '__toString')) {
$_value = (string) $value;
return $_value;
}
return null;
}
$_value = $value;
$status = @settype($_value, 'string');
if ($status) {
return $_value;
}
return null;
}
```
The
parse_string_one like 2x slower than parse_string_two.
Also:
```
@settype($_value, 'string');
```
affects objects that has no method __toString() converts any object to word
'Object'
in that case the boolean should be converted to 'TRUE/FALSE', and NULL should be converted
to 'NULL'
Or this is bug.
### PHP Version
PHP 8.0
### Operating System
_No response_