WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#6464 closed defect (bug) (fixed)

convert_smilies is very slow

Reported by: Acci Owned by:
Milestone: 2.8 Priority: normal
Severity: normal Version:
Component: Optimization Keywords: has-patch, tested
Focuses: Cc:

Description

I created a testing WordPress blog with 10 posts and I found huge performance problem in convert_smilies function. This function took at average 39 ms on my high-end PC.

Currently this function uses preg_replace and it's called 40× for each part of the post.

My solution is to use very similar function preg_replace_callback with modified regular expression. This solution is 6× faster than original (5,9 ms).

Attachments (5)

6464.diff (9 bytes) - added by Acci 7 years ago.
faster-smilies2.patch (4.5 KB) - added by johanee 6 years ago.
Faster regexp and uses preg_replace_callback()
faster-smilies3.patch (4.7 KB) - added by johanee 6 years ago.
Faster regexp and uses preg_replace_callback(), tested, fixed
faster-smilies4.patch (5.1 KB) - added by johanee 6 years ago.
Faster regexp and uses preg_replace_callback(), tested, fixed, minor cleaning
test_smilies.php (5.5 KB) - added by johanee 6 years ago.
Test case for smilies, for use with Wordpress Test

Download all attachments as: .zip

Change History (14)

comment:1 @Nazgul7 years ago

  • Keywords has-patch needs-testing added
  • Milestone changed from 2.7 to 2.6

@Acci7 years ago

comment:2 @Acci7 years ago

  • Keywords has-patch needs-testing removed

Path deleted.

@johanee6 years ago

Faster regexp and uses preg_replace_callback()

comment:3 @johanee6 years ago

  • Milestone changed from 2.9 to 2.8

Patch added for this. Also, I think it should go in before 2.9.

This patch changes smilies_init and convert_smilies to make a more efficient regular expression resulting in a ~10times speedup.

This is done by:

o the search expression is built up as sub-expressions for smiley first letter (example of partial result: "8(\)\)O|O)").

o to handle the replacement preg_replace_callback() in convert_smilies.

Resulting generated search regular expression for default smilies:

"/(\s|)8(\)\)O|O)|\:(\!\:|\(|\)\(\)\?DPox\
\?|\?\:|\?\?\?\:|D|P|arrow\:|cool\:|cry\:|eek\:|evil\:|grin\:|idea\:|lol\:|mad\:|mrgreen\:|neutral\:|o|oops\:|razz\:|roll\:|sad\:|shock\:|smile\:|twisted\:|wink\:|x|\|)|;(\)\))(\s|$)/e"

Load time on the site for a post with 403 comments:
Original: 4.3s
Modified: 2.6s

comment:4 @johanee6 years ago

Sorry, slightly old regexp in comment above. There is no 'e' modifier on the end in patch.

comment:5 @johanee6 years ago

  • Keywords has-patch tested added

Tested all smilies with trunk revision 10308 and 2.6.5

(found and fixed an error in handling smilies like :???: vs :?)

@johanee6 years ago

Faster regexp and uses preg_replace_callback(), tested, fixed

@johanee6 years ago

Faster regexp and uses preg_replace_callback(), tested, fixed, minor cleaning

@johanee6 years ago

Test case for smilies, for use with Wordpress Test

comment:6 @johanee6 years ago

Added a proposed test case for smilies (use with http://codex.wordpress.org/Automated_Testing).

Also minor clean ups of patch (removed unused global).

comment:7 @johanee6 years ago

  • Cc johan.eenfeldt@… added

comment:8 @ryan6 years ago

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

(In [10322]) Faster smilies. Props johanee. fixes #6464

comment:9 @azaozz6 years ago

(In [10514]) Require smilies to be surrounded by spaces or be at the beginning or end of the line, see #6464

Note: See TracTickets for help on using tickets.