Make WordPress Core

Opened 6 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#62317 closed enhancement (fixed)

Optimize "WP_Style_Engine::parse_block_styles()" by reducing unnecessary "array_merge" calls

Reported by: mukesh27's profile mukesh27 Owned by: mukesh27's profile mukesh27
Milestone: 6.8 Priority: normal
Severity: normal Version: 6.1
Component: Editor Keywords: has-patch commit
Focuses: performance Cc:

Description

While benchmarking WordPress 6.7, a significant number of array_merge calls were observed, particularly within the WP_Style_Engine::parse_block_styles() function in TT4 theme. The repeated, unnecessary calls to array_merge impacted performance

This update optimizes WP_Style_Engine::parse_block_styles() by conditionally merging classnames and CSS declarations only when they are not empty. This removes several unwanted calls to array_merge and helps improve performance, especially for themes with extensive styles.

Change History (10)

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


6 weeks ago
#1

  • Keywords has-patch added; needs-patch removed

@mukesh27 commented on PR #7670:


6 weeks ago
#2

### TT4 theme:

https://github.com/user-attachments/assets/d5bd9d78-639f-4603-9cbf-349e08329a19

@ramonopoly commented on PR #7670:


5 weeks ago
#3

Thanks for testing!

One very minor question on the code here, is it worth type checking the return values from get_css_declarations etc?
From memory, that can in turn call other functions from the style declaration. So I was wondering if we could benefit from future proofing this a little

So is_array or something? The value functions are all internal for now, and they all return array() consistently.

But if we ever open that up for extension, it would be a good idea.

@aaronrobertshaw commented on PR #7670:


5 weeks ago
#4

So is_array or something? The value functions are all internal for now, and they all return array() consistently.

Yeah, that's what I meant.

In addition to the scenario where the style engine was made external, I was thinking for people less familiar with the style engine that came to add style declarations but missed that array return value requirement.

No need to address it now. I was mostly just thinking out loud.

@mukesh27 commented on PR #7670:


5 weeks ago
#5

Thanks for the review.

One very minor question on the code here, is it worth type checking the return values from get_css_declarations etc?

@aaronrobertshaw @ramonjd I believe we should open a Trac ticket to facilitate further discussion, allowing us to implement this in the future if needed.

@joemcgill @felixarntz, could you please take a look when you have a moment? This way, we can proceed with committing it for 6.8.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


5 weeks ago

#7 @mukesh27
3 weeks ago

  • Keywords commit added

The PR got enough approval and GB backport PR already merged.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


3 weeks ago

#9 @joemcgill
3 weeks ago

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

In 59442:

Editor: Avoid unnecessary array_merge in WP_Style_Engine::parse_block_styles().

This adds an ! empty() check for classnames and declarations to avoid calling array_merge() with an empty value.

Props mukesh27, ramonopoly, aaronrobertshaw.
Fixes #62317.

Note: See TracTickets for help on using tickets.