zhanjh / minify

Automatically exported from code.google.com/p/minify
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Minify_HTML performance improvements (patch) #192

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Minify version: 2.1.3
PHP version: 5.2.13 (not really relevant)

What steps will reproduce the problem?
1. Measure minification time of (for example) this page on Minify 2.1.3: 
http://www.salir.com/antigua -- any large-ish "normal" page will do.
3. Apply the attached patch (again, on Minify 2.1.3)
4. Repeat the measurement

You'll observe a saving of 70-80% of time -- mostly CPU time.

Expected output:
n/a

Actual output:
n/a

Did any unit tests FAIL? (Please do not post the full list)

No. All tests pass before and after the patch. The patch contains some changes 
in expected result, but you'll see that those new expected results are just as 
good as the previous ones.

Description of the proposed changes:

The change in "remove ws outside of all elements" replaces a 
preg_match_callback call with a preg_replace call, which is much faster.

All other changes use the fact that starting capturing during regular 
expression matching is an expensive operation (see, e.g. man perlre, which 
states: "you [...] pay a price for each pattern that contains capturing 
parentheses.") This can be avoided by delaying the start of capture until the 
regular expression has confirmed that there will be something to 
capture/replace.

It should be possible to improve the minification of <script> tags by somehow 
getting rid of the "(\\s*)" at the start of the regexp, but we've encountered 
difficulties doing so and decided that 4 times faster is enough for us.

Original issue reported on code.google.com by jordi.sa...@salir.com on 27 Sep 2010 at 4:01

Attachments:

GoogleCodeExporter commented 9 years ago
(How do I change the issue Type to "Enhancement"?)

Original comment by jordi.sa...@salir.com on 27 Sep 2010 at 4:03

GoogleCodeExporter commented 9 years ago
Awesome, can't wait to look into these.

Original comment by mrclay....@gmail.com on 27 Sep 2010 at 9:13

GoogleCodeExporter commented 9 years ago
Committed in r423

Original comment by mrclay....@gmail.com on 30 Sep 2010 at 4:34