#50784 closed defect (bug) (fixed)
Stop using `preg_match()` in `wp_opcache_invalidate()`
Reported by: | kirasong | Owned by: | kirasong |
---|---|---|---|
Milestone: | 5.5 | Priority: | normal |
Severity: | normal | Version: | 5.5 |
Component: | Upgrade/Install | Keywords: | has-patch |
Focuses: | performance | Cc: |
Description
While writing up the devnote for wp_opcache_invalidate()
, @aaroncampbell pointed out, rightly, that it might make sense to use something else to check for a PHP extension rather preg_match()
for performance reasons.
As timeout failures are common during upgrades and we're only checking for one extension for PHP files at this point, I agree.
Patches coming shortly.
See: #36455
Attachments (3)
Change History (12)
#2
@
4 years ago
50784.substr.diff would be my preference here, that seems the most consistent with other similar checks in core.
This ticket was mentioned in Slack in #core by david.baumwald. View the logs.
4 years ago
#5
@
4 years ago
Of the three approaches, I think I too prefer 50784.substr.diff.
At first, another option that I thought of was using strpos()
or stripos()
. In my testing, this was the most performant approach. However, negative offsets are only supported in PHP >= 7.1
That would look like this:
if ( false !== strpos( $filepath, '.php', -4 )
Another option, though I'm not sure it's a readability improvement, is using in_array()
and substr()
.
if ( ! in_array( substr( $filepath, -4 ), array( '.php', '.PHP' ), true ) )
#6
@
4 years ago
Thanks so much both!
@desrosj Nice! I wish we could use the method that is 7.1+. Maybe when WordPress starts requiring it.
The in_array()
pass you wrote is a bit faster in my testing. I think it's easier to read than the substr_compare()
approaches. It doesn't catch *quite* as many files, although I'm guessing not very many are named .Php
.
Since it seems we all liked 50784.substr.diff and it lines up with some existing methods in core (thanks, @SergeyBiryukov!), I think it makes sense to go ahead and do that for now.
If we see issues with performance, we could come back to the in_array()
approach, perhaps?
As we're near RC, I'll get that taken care of for now, and we can iterate if y'all would like.
All of these are faster than what is there now.
50784.substr_compare.diff is the fastest here, but I noticed that in the PHP docs, a warning gets thrown if the string is too short, so 50784.substr_compare_warning.diff works around that.
Since that comes out pretty terse, I like the approach @jnylen0 used in a previous patch, in 50784.substr.diff quite a bit. This is slightly slower, but much more readable.
Would appreciate any opinions.