[wp-trac] [WordPress Trac] #58069: Performance of _wp_normalize_relative_css_links() can be increased >2x
WordPress Trac
noreply at wordpress.org
Wed Apr 5 13:59:35 UTC 2023
#58069: Performance of _wp_normalize_relative_css_links() can be increased >2x
--------------------------------------+-------------------------------
Reporter: westonruter | Owner: westonruter
Type: defect (bug) | Status: assigned
Priority: normal | Milestone: 6.2.1
Component: Script Loader | Version: 5.9
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses: css, performance
--------------------------------------+-------------------------------
Description changed by westonruter:
Old description:
> The `_wp_normalize_relative_css_links()` function is not currently
> implemented in an optimal way. It is doing a regex match for all
> instances of `url(...)` in a given CSS, and then for each match it checks
> if it is a relative URL. If so, it then makes the URL absolute and then
> does ''another'' global search & replace in the CSS to replace the
> relative path with the absolute path. This means for every search there
> is a secondary search. This secondary `str_replace()` can be eliminated
> by replacing the unitial `preg_match_all()` with a
> `preg_replace_callback()`. This makes the function run >2x faster when
> [https://gist.github.com/westonruter/7f916bee3f45ef9307235daefb603e7c
> benchmarked] being run across each CSS file in Gutenberg and the built-in
> themes.
>
> Essentially the function can be turned from O(n²) to O(n).
>
> {{{
> $ composer run-script test -- --iterations=10 --revs=500
> > phpbench run Bench.php --report=default '--iterations=10' '--revs=500'
> PHPBench (1.2.10) running benchmarks... #standwithukraine
> with PHP version 7.4.33, xdebug , opcache
>
> \Bench
>
> benchTrunk..............................I9 - Mo7.734ms (±2.96%)
> benchPatch..............................I9 - Mo3.527ms (±6.74%)
>
> Subjects: 2, Assertions: 0, Failures: 0, Errors: 0
> +------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
> | iter | benchmark | subject | set | revs | mem_peak | time_avg |
> comp_z_value | comp_deviation |
> +------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
> | 0 | Bench | benchTrunk | | 500 | 4,242,960b | 8,066.348μs |
> +0.76σ | +2.24% |
> | 1 | Bench | benchTrunk | | 500 | 4,242,960b | 8,006.150μs |
> +0.50σ | +1.48% |
> | 2 | Bench | benchTrunk | | 500 | 4,242,960b | 7,815.732μs |
> -0.32σ | -0.93% |
> | 3 | Bench | benchTrunk | | 500 | 4,242,960b | 7,706.406μs |
> -0.78σ | -2.32% |
> | 4 | Bench | benchTrunk | | 500 | 4,242,960b | 7,702.188μs |
> -0.80σ | -2.37% |
> | 5 | Bench | benchTrunk | | 500 | 4,242,960b | 7,649.464μs |
> -1.03σ | -3.04% |
> | 6 | Bench | benchTrunk | | 500 | 4,242,960b | 7,628.240μs |
> -1.12σ | -3.31% |
> | 7 | Bench | benchTrunk | | 500 | 4,242,960b | 7,785.394μs |
> -0.45σ | -1.32% |
> | 8 | Bench | benchTrunk | | 500 | 4,242,960b | 8,314.294μs |
> +1.82σ | +5.39% |
> | 9 | Bench | benchTrunk | | 500 | 4,242,960b | 8,219.126μs |
> +1.41σ | +4.18% |
> | 0 | Bench | benchPatch | | 500 | 4,242,960b | 3,492.050μs |
> -0.80σ | -5.42% |
> | 1 | Bench | benchPatch | | 500 | 4,242,960b | 3,440.668μs |
> -1.01σ | -6.81% |
> | 2 | Bench | benchPatch | | 500 | 4,242,960b | 3,524.826μs |
> -0.67σ | -4.53% |
> | 3 | Bench | benchPatch | | 500 | 4,242,960b | 4,035.964μs |
> +1.38σ | +9.32% |
> | 4 | Bench | benchPatch | | 500 | 4,242,960b | 4,178.894μs |
> +1.96σ | +13.19% |
> | 5 | Bench | benchPatch | | 500 | 4,242,960b | 3,769.186μs |
> +0.31σ | +2.09% |
> | 6 | Bench | benchPatch | | 500 | 4,242,960b | 3,527.606μs |
> -0.66σ | -4.45% |
> | 7 | Bench | benchPatch | | 500 | 4,242,960b | 3,900.464μs |
> +0.84σ | +5.65% |
> | 8 | Bench | benchPatch | | 500 | 4,242,960b | 3,542.204μs |
> -0.60σ | -4.06% |
> | 9 | Bench | benchPatch | | 500 | 4,242,960b | 3,508.126μs |
> -0.74σ | -4.98% |
> +------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
> }}}
New description:
The `_wp_normalize_relative_css_links()` function is not currently
implemented in an optimal way. It is doing a regex match for all instances
of `url(...)` in a given CSS, and then for each match it checks if it is a
relative URL. If so, it then makes the URL absolute and then does
''another'' global search & replace in the CSS to replace the relative
path with the absolute path. This means for every search there is a
secondary search. This secondary `str_replace()` can be eliminated by
replacing the unitial `preg_match_all()` with a `preg_replace_callback()`.
This makes the function run >2x faster when
[https://gist.github.com/westonruter/7f916bee3f45ef9307235daefb603e7c
benchmarked] being run across each CSS file in Gutenberg and the built-in
themes.
{{{
$ composer run-script test -- --iterations=10 --revs=500
> phpbench run Bench.php --report=default '--iterations=10' '--revs=500'
PHPBench (1.2.10) running benchmarks... #standwithukraine
with PHP version 7.4.33, xdebug , opcache
\Bench
benchTrunk..............................I9 - Mo7.734ms (±2.96%)
benchPatch..............................I9 - Mo3.527ms (±6.74%)
Subjects: 2, Assertions: 0, Failures: 0, Errors: 0
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
| iter | benchmark | subject | set | revs | mem_peak | time_avg |
comp_z_value | comp_deviation |
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
| 0 | Bench | benchTrunk | | 500 | 4,242,960b | 8,066.348μs |
+0.76σ | +2.24% |
| 1 | Bench | benchTrunk | | 500 | 4,242,960b | 8,006.150μs |
+0.50σ | +1.48% |
| 2 | Bench | benchTrunk | | 500 | 4,242,960b | 7,815.732μs |
-0.32σ | -0.93% |
| 3 | Bench | benchTrunk | | 500 | 4,242,960b | 7,706.406μs |
-0.78σ | -2.32% |
| 4 | Bench | benchTrunk | | 500 | 4,242,960b | 7,702.188μs |
-0.80σ | -2.37% |
| 5 | Bench | benchTrunk | | 500 | 4,242,960b | 7,649.464μs |
-1.03σ | -3.04% |
| 6 | Bench | benchTrunk | | 500 | 4,242,960b | 7,628.240μs |
-1.12σ | -3.31% |
| 7 | Bench | benchTrunk | | 500 | 4,242,960b | 7,785.394μs |
-0.45σ | -1.32% |
| 8 | Bench | benchTrunk | | 500 | 4,242,960b | 8,314.294μs |
+1.82σ | +5.39% |
| 9 | Bench | benchTrunk | | 500 | 4,242,960b | 8,219.126μs |
+1.41σ | +4.18% |
| 0 | Bench | benchPatch | | 500 | 4,242,960b | 3,492.050μs |
-0.80σ | -5.42% |
| 1 | Bench | benchPatch | | 500 | 4,242,960b | 3,440.668μs |
-1.01σ | -6.81% |
| 2 | Bench | benchPatch | | 500 | 4,242,960b | 3,524.826μs |
-0.67σ | -4.53% |
| 3 | Bench | benchPatch | | 500 | 4,242,960b | 4,035.964μs |
+1.38σ | +9.32% |
| 4 | Bench | benchPatch | | 500 | 4,242,960b | 4,178.894μs |
+1.96σ | +13.19% |
| 5 | Bench | benchPatch | | 500 | 4,242,960b | 3,769.186μs |
+0.31σ | +2.09% |
| 6 | Bench | benchPatch | | 500 | 4,242,960b | 3,527.606μs |
-0.66σ | -4.45% |
| 7 | Bench | benchPatch | | 500 | 4,242,960b | 3,900.464μs |
+0.84σ | +5.65% |
| 8 | Bench | benchPatch | | 500 | 4,242,960b | 3,542.204μs |
-0.60σ | -4.06% |
| 9 | Bench | benchPatch | | 500 | 4,242,960b | 3,508.126μs |
-0.74σ | -4.98% |
+------+-----------+------------+-----+------+------------+-------------+--------------+----------------+
}}}
--
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58069#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list