#59450 closed enhancement (fixed)
Initialize $url variable later in get_bloginfo()
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.5 | Priority: | normal |
Severity: | minor | Version: | 2.3 |
Component: | General | Keywords: | has-patch commit |
Focuses: | performance | Cc: |
Description
get_bloginfo()
is a popular function. Alas, it performs checks for an often unused parameter: $url
.
Moreover, the ! str_contains()
chain can be exchanged for a simpler preg_match()
.
See, for performance: https://3v4l.org/dqBgA.
The attached patch will address both issues.
Note that the performance impact is mostly due to the use of NOT and writing to a variable.
Here's the performance benchmark of that: https://3v4l.org/8hSiW.
When we fix that and then compare it to preg_match()
, it becomes a matter of preference: https://3v4l.org/3AXAY.
Still, str_contains()
relies on a polyfill for many sites.
It's been like this since WP 2.3, so it might be worthwhile to write a sniff for this as it went unnoticed for 16 years.
Attachments (1)
Change History (9)
#1
@
18 months ago
- Milestone changed from Awaiting Review to 6.5
- Severity changed from normal to minor
#2
@
18 months ago
- Summary changed from Fix get_bloginfo to Initialize $url variable later in get_bloginfo()
#3
@
18 months ago
The 1e4
iterations are not part of the benchmark but the warmup. I found that the website shows inconsistent numbers, probably due to the way hrtime()
interacts with the server or how it lazy loads PCRE -- I cannot tell. If you flip the warmup tests, the second will always be slower during -- everything thereafter is indicative of real-world performance. I should exclude those from the output in the future to alleviate confusion about this quirk.
The point I'm making about not writing to variables is that the entire codebase is trying to write variables before reading them. This often happens needlessly, also in apply_filters()
, albeit obscure: https://wordpress.slack.com/archives/C02RQBWTW/p1695251506451989. This is the aftermath of supporting PHP 5.2 for far too long, accumulating code debt that should be undone if we wish to make WordPress load in under 0.01 seconds; I'm certain that number is attainable.
#4
@
18 months ago
The 1e4 iterations are not part of the benchmark but the warmup.
You used 1e3 for warmup and ie5 for the benchmark. In my testing I reduced the 1e5 to 1e4. That's what my comment was referring to.
But yes, I agree, overall the numbers are inconsistent, which is why I don't see the need in changing those str_contains
calls to preg_match
.
Anyhow, as we have already established here, it makes sense to initialize the variable later.
#5
@
16 months ago
- Keywords commit added
- Owner set to flixos90
- Status changed from new to reviewing
- Type changed from defect (bug) to enhancement
Since the str_contains()
vs preg_match()
comparison doesn't show a clear winner in performance, it's likely insignificant regardless. So given the existing code uses str_contains()
, we should probably just stick to that.
So what I'm leaning towards is modifying general-template.php.patch to use the following in the if
clause (instead of preg_match()
):
str_contains( $show, 'url' ) || str_contains( $show, 'directory' ) || str_contains( $show, 'home' )
This ticket was mentioned in PR #5739 on WordPress/wordpress-develop by @flixos90.
16 months ago
#6
@flixos90 commented on PR #5739:
16 months ago
#8
Committed in https://core.trac.wordpress.org/changeset/57170
Initializing the variable only when it's needed is reasonable.
I doubt the performance impact is that high though in a real world scenario, as the function is never used 100k times on a single page load :)
In fact, your benchmark shows that
preg_match
is slower even with 1e4 iterations.