Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#28623 closed enhancement (fixed)

Optimize the wptexturize() Loop

Reported by: miqrogroove's profile miqrogroove Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.0 Priority: normal
Severity: normal Version: 1.5
Component: Formatting Keywords: wptexturize has-patch
Focuses: Cc:

Description

Following on #28575, there are some more things we can do to make this core code run faster:

The ampersand replacement pattern is running inside the loop for no apparent reason. We can get an overall 10% performance boost just by moving that one pattern to after the loop.

_wptexturize_pushpop_element() may be unnecessarily running regex code that could be replaced with simple string searches, for up to 7% performance boost.

Any opportunity to remove or consolidate the $dynamic patterns will improve performance by about 4% per pattern removed.

Attachments (6)

miqro-28623.patch (4.6 KB) - added by miqrogroove 10 years ago.
First pass at optimizing ampersands and the element stack.
miqro-28623.2.patch (5.4 KB) - added by miqrogroove 10 years ago.
Second pass after testing.
miqro-28623.3.patch (5.3 KB) - added by miqrogroove 10 years ago.
Minimize the stack logic a bit more for a clean look.
miqro-28623.4.patch (5.3 KB) - added by miqrogroove 10 years ago.
Also fix ampersand pattern to match things like &# in text.
miqro-28623.5.patch (5.4 KB) - added by miqrogroove 10 years ago.
Allow various entity references.
miqro-28623.6.patch (7.4 KB) - added by miqrogroove 10 years ago.
Update unit tests.

Download all attachments as: .zip

Change History (15)

#1 @miqrogroove
10 years ago

  • Keywords wptexturize added

#2 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.0

This ticket was mentioned in IRC in #wordpress-dev by miqrogroove. View the logs.


10 years ago

#4 @miqrogroove
10 years ago

  • Version changed from 3.8 to 1.5

@miqrogroove
10 years ago

First pass at optimizing ampersands and the element stack.

@miqrogroove
10 years ago

Second pass after testing.

@miqrogroove
10 years ago

Minimize the stack logic a bit more for a clean look.

#5 @dllh
10 years ago

I ran my prior tests from #28575 against trunk and with the second draft of the patch applied, and it's a nice improvement. My test is pretty blunt and not granular, but doing 10 iterations of wptexturize() for a 1MB post, trunk does it in around 6.2 seconds, and the patch shaves about a second off that time. More granular profiling like what you did on the other ticket will of course be more useful than my test. Seems like a nice step forward, though!

@miqrogroove
10 years ago

Also fix ampersand pattern to match things like &# in text.

#6 @miqrogroove
10 years ago

  • Keywords needs-unit-tests added

Need to add tests for more things like &# and check if there is already a ticket for that.

@miqrogroove
10 years ago

Allow various entity references.

@miqrogroove
10 years ago

Update unit tests.

#7 @miqrogroove
10 years ago

  • Keywords has-patch added; needs-unit-tests removed

fwiw, I also attempted to consolidate some of the $dynamic patterns by using preg_replace_callback, but the overhead for that function is so high that it negates most of the benefit of running fewer patterns.

In miqro-28623.6.patch:

  • Take the ampersand pattern out of the loop for speed.
  • Fix old bugs in the ampersand pattern.
  • Refactor _wptexturize_pushpop_element() without PCRE for speed.
  • Update unit tests.

#8 @wonderboymusic
10 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 28831:

Optimize the wptexturize() loop:

  • Take the ampersand pattern out of the loop for speed.
  • Fix old bugs in the ampersand pattern.
  • Refactor _wptexturize_pushpop_element() without PCRE for speed.
  • Update unit tests.

Props miqrogroove.
Fixes #28623.

#9 @miqrogroove
10 years ago

dllh: See #28724 for new work. I'd like to know if you can confirm another 3x speed improvement. :)

Note: See TracTickets for help on using tickets.