Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#56784 new defect (bug)

Optimization in wp_staticize_emoji function

Reported by: kac1per's profile kac1per Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0.2
Component: Emoji Keywords: needs-patch
Focuses: performance Cc:

Description

In the wp_staticize_emoji function there is a piece of code which seems to be there to optimize the process, but in fact does the opposite.

The piece i'm writing about:

	// Quickly narrow down the list of emoji that might be in the text and need replacing.
	$possible_emoji = array();
	foreach ( $emoji as $emojum ) {
		if ( false !== strpos( $text, $emojum ) ) {
			$possible_emoji[ $emojum ] = html_entity_decode( $emojum );
		}
	}

Feeding all of 3575 emoji enitities into strpos function generates much more overhead than passing all existing emojis for further processing. This can be easily observed by entirely skipping the false !== strpos( $text, $emojum ) check and adding all entities to $possible_emoji array – the whole execution of the wp_staticize_emoji function becomes nearly 10x faster. This effect happened to me every time I tested it no matter the content or length of the passed text.

Change History (3)

#1 @desrosj
2 years ago

  • Keywords needs-patch added

Thanks for this one, @kac1per.

It seems that the code block you're referencing is there to ensure both the encoded and decoded versions of the emoji are available further down. So only skipping the condition would not be the full solution.

Could you prepare a patch or pull request demonstrating your suggested changes? This will help make it more clear to everyone viewing this ticket and allow the automated tests to run.

#2 @desrosj
2 years ago

After some digging, it seems that these lines of code were added in [41701] as a part of #35293. There is some very detailed performance testing that took place there.

This would need to receive a similar level of attention to detail to prevent any regressions.

#3 @geekofshire
3 months ago

I ran some tests to compare the old function and the new suggested function, but the improvement wasn’t significant. However, I’m not entirely confident in my testing process, so I believe it would be beneficial to conduct standard and more rigorous testing to move ahead with this issue.

Note: See TracTickets for help on using tickets.