[wp-trac] [WordPress Trac] #22951: Performance enhancements for esc_url()
WordPress Trac
noreply at wordpress.org
Tue Oct 18 15:42:30 UTC 2016
#22951: Performance enhancements for esc_url()
-------------------------------------------------+-------------------------
Reporter: markjaquith | Owner: schlessera
Type: defect (bug) | Status: assigned
Priority: normal | Milestone: Future
Component: Formatting | Release
Severity: normal | Version: 2.8
Keywords: reporter-feedback has-patch 2nd- | Resolution:
opinion | Focuses:
| performance
-------------------------------------------------+-------------------------
Changes (by schlessera):
* keywords: needs-patch reporter-feedback needs-unit-tests => reporter-
feedback has-patch 2nd-opinion
Comment:
@peterwilsoncc Sorry for not getting back to you sooner.
I had some time during a contributor day and made a first attempt at
improving performance. Basically, I just add a regex in front of the bad
protocol detection to return early for http(s) URLs. I'm sure there might
be other performance enhancements at other (earlier) points in the code
execution, but these will also have more potential impact on the security
implications than what I've currently done.
With the change in place, the existing unit tests in group `kses` still
pass successfully.
The regex I've taken is the @diegoperini one (with a slight modification
to remove ftp) from the comparison here: https://mathiasbynens.be/demo
/url-regex . It is pretty long and feature-complete, but due to the way
the regex is evaluated, it is actually fast enough to provide a real
improvement.
I've done some (not 100% scientific) benchmarks to compare the current
version of `esc_url` from trunk against my modified version. For all
benchmarks, both versions run on the same machine from within the same
process, and only the relative change is stored, to get rid of
fluctuations due to allocations of server resources as good as possible.
You can see a table with the benchmark results here:
https://docs.google.com/spreadsheets/d/1XK5lgAIDMlj6lQSTrelXdD5eh6wAqJuLMS1605iM618/edit?usp=sharing
The benchmark summary: In a normal use case, this change should speed up
the `esc_url` function by 25-30%. In a best case scenario, the speed-up
can be as high as 40%, while the absolute worst case scenario (basically,
dealing with 100% bad protocol links) produces a slow-down of 2%, which I
deem is acceptable given the very low probability of this ever being the
case.
The test script I've used to run the benchmarks can be found here:
https://gist.github.com/schlessera/d4dd4db71d59dfee6ee6c5b9a0ca8a33
--
Ticket URL: <https://core.trac.wordpress.org/ticket/22951#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list