Make WordPress Core

Opened 8 months ago

Closed 8 months ago

Last modified 8 months ago

#59178 closed enhancement (fixed)

Performance optimization of wp_get_block_css_selector()

Reported by: mukesh27's profile mukesh27 Owned by: davidbaumwald's profile davidbaumwald
Milestone: 6.4 Priority: normal
Severity: normal Version: 6.3
Component: Editor Keywords: needs-patch has-screenshots good-first-bug
Focuses: performance Cc:

Description

On the TT3 home page, a single post with content (including an image and text) is causing calls to the wp_get_block_css_selector function during testing. This function call occurs 567 times, which is significantly impacting performance. A deeper analysis reveals that the array_merge function is being called 372 times within the wp_get_block_css_selector function, leading to unnecessary overhead.

According to measurements taken using the XHProf tool, these activities are consuming approximately 2% of the total load time for the page. Addressing this issue has the potential to not only enhance performance but also contribute to improved memory management, as demonstrated in the attached screenshot.

Additional props to @joemcgill for helping to identify the bug!

Attachments (1)

XHProf.png (442.6 KB) - added by mukesh27 8 months ago.

Download all attachments as: .zip

Change History (7)

@mukesh27
8 months ago

This ticket was mentioned in PR #5072 on WordPress/wordpress-develop by @daxelrod.


8 months ago
#1

  • Keywords has-patch added; needs-patch removed

Performance optimization of wp_get_block_css_selector() by removing array_merge() with an inline array. The current() function only works because must only have one element to within the block.

@daxelrod commented on PR #5072:


8 months ago
#2

Unless there is a noticeable performance benefit, I'm not sure this optimization is worth it. This is my attempt to remove array_merge() as simply as possible, but a larger refactor could be more readable and performant.

#3 @tabrisrp
8 months ago

  • Keywords needs-patch added; has-patch removed

I was looking into this and wondering what was the original idea of using array_merge() to add a single element to the array? This could help decide the best approach to improve the performance of the function.

#4 @davidbaumwald
8 months ago

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

In 56457:

Editor: Optimize wp_get_block_css_selector to remove array_merge calls for better performance.

Some block themes like TT3 use wp_get_block_css_selector to determine a CSS selector based on block type and other parameters. However, recent performance profiling indicated a bottleneck in wp_get_block_css_selector, particularly sections that utilize array_merge. By slightly refactoring these sections, array_merge calls can be avoided which increases performance.

Props mukesh27, joemcgill, daxelrod, tabrisrp.
Fixes #59178.

#5 @mukesh27
8 months ago

  • Milestone changed from Awaiting Review to 6.4

Thanks @davidbaumwald for committing it.

Moving this ticket to 6.4 milestone.

Note: See TracTickets for help on using tickets.