#55680 closed enhancement (fixed)
Replace `phpversion()` function calls with PHP_VERSION
Reported by: | ayeshrajans | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 6.0 |
Component: | General | Keywords: | has-patch has-unit-tests |
Focuses: | performance | Cc: |
Description
We use several phpversion()
functions in WordPress core, which I propose to be replaced with PHP_VERSION
constants.
phpversion()
return value and PHP_VERSION
constant value are identical, but the former is about 3 times slower because it is a function call compared to a direct constant value lookup.
Simple benchmark:
<?php $start = microtime(true); for ($i = 0; $i < 1000000; $i++) {phpversion();} echo microtime(true) - $start; echo "\r\n"; $start = microtime(true); for ($i = 0; $i < 1000000; $i++) {PHP_VERSION;} echo microtime(true) - $start;
Change History (11)
This ticket was mentioned in PR #2674 on WordPress/wordpress-develop by Ayesh.
4 weeks ago
- Keywords has-unit-tests added
#3
@
4 weeks ago
Ayesh commented on PR #2674:
@costdev you are right, it absolutely makes sense to change the function_exists('phpversion
) do the
defined`, which I have now done.
Apart from this PR, I think we could use a good cleanup of these function_exists
calls we have accumulated over the years, which are now either bundled into core, is past the version test.
#4
@
4 weeks ago
jrfnl commented on PR #2674:
@costdev you are right, it absolutely makes sense to change the
function_exists('phpversion
)do the
defined`, which I have now done.
Actually.... I think that check can probably be removed...
If I remember correctly, those kind of checks will have been put in place for silly hosts which think adding phpversion
to the `disabled_functions` ini directive increases security (it does not).
I'm trying to think, but I can't remember any way (for hosts) to prevent PHP from declaring the PHP_VERSION
constant.
Unless someone else comes up with a way that could be done, that function_exists()
/defined()
check is no longer needed.
Apart from this PR, I think we could use a good cleanup of these
function_exists
calls we have accumulated over the years, which are now either bundled into core, is past the version test. Makes the code cleaner, and a happy path for OPCache.
➕ Agreed. I remember doing such a check for WP 5.3 (after the PHP 5.2 version drop), but I doubt it's been done since.
#7
@
2 weeks ago
SergeyBiryukov commented on PR #2674:
phpversion()
return value andPHP_VERSION
constant value are identical, but the former is about 3 times slower because it is a function call compared to a direct constant value lookup.
Simple benchmark:
$start = microtime(true); for ($i = 0; $i < 1000000; $i++) {phpversion();} echo microtime(true) - $start; echo "\r\n"; $start = microtime(true); for ($i = 0; $i < 1000000; $i++) {PHP_VERSION;} echo microtime(true) - $start;
I was curious about the results of this benchmark. On my install, after a few runs, PHP_VERSION
on average appears to be 5 times faster than phpversion()
, making the performance improvement even more noticeable:
phpversion(): 0.0367081165314 PHP_VERSION: 0.00715589523315
Should be good to go.
#8
@
2 weeks ago
SergeyBiryukov commented on PR #2674:
On my install, after a few runs,
PHP_VERSION
on overage appears to be 5 times faster thanphpversion()
, making the performance improvement even more noticeable
For reference, this was on PHP 8.0 and 8.1. On PHP 7.4, the difference is indeed closer to 3 times.
#10
@
2 weeks ago
SergeyBiryukov commented on PR #2674:
Thanks for the PR! Merged in https://core.trac.wordpress.org/changeset/53426.
Trac ticket: https://core.trac.wordpress.org/ticket/55680#ticket
We use several
phpversion()
function calls in WordPress core, which I propose to be replaced withPHP_VERSION
constants.phpversion()
return value andPHP_VERSION
constant value are identical, but the former is about 3 times slower because it is a function call compared to a direct constant value lookup.Simple benchmark:
{{{php
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {phpversion();}
echo microtime(true) - $start;
echo "\r\n";
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {PHP_VERSION;}
echo microtime(true) - $start;
}}}