Make WordPress Core

Opened 8 weeks ago

Closed 5 weeks ago

#65533 closed enhancement (fixed)

Editor: prepend_to_selector: optimized with str_replace()

Reported by: josephscott Owned by:
Priority: normal Milestone: 7.1
Component: Editor Version:
Severity: normal Keywords: has-patch has-unit-tests gutenberg-merge close
Cc: Focuses:

Description

This ticket tracks the backport of PHP files for the following Gutenberg update:

https://github.com/WordPress/gutenberg/pull/76556/

prepend_to_selector() can get call hundreds or thousands of times, which can add up. For supported conditions this small optimization can dramatically reduce the time spent in prepend_to_selector().

Change History (12)

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


8 weeks ago
#1

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/65533

This PR syncs the Gutenberg PR: https://github.com/WordPress/gutenberg/pull/76556

## Use of AI Tools

AI assistance: Yes
Tools: Claude
Model: Opus 4.6
Used for: digging into php-spx results and generating optimization ideas

#2 @wildworks
7 weeks ago

  • Keywords gutenberg-merge added
  • Milestone Awaiting Review7.1

This ticket appears to be targeting 7.1, so I will update the milestone.

@dmsnell commented on PR #12306:


7 weeks ago
#3

@josephscott I’ve added the full set of start characters here to remove the bug this introduces, and I’m going to add the expanded split_selector_list() update to #11857 to remove the bugs it introduces.

@dmsnell commented on PR #12306:


7 weeks ago
#4

@josephscott mind giving this a look before I merge it? the changes that I made are:

  • updated the fast-path condition so that it doesn’t break selectors with non-comma-token commas. this is a more restrictive check and will abort the fast-path even when it wouldn’t have to. it does this to keep the pre-check condition simple and fast.
  • the same optimization is added to append_to_selector()

@dmsnell commented on PR #12306:


6 weeks ago
#5

Rebased in preparation for merge: previous HEAD was a41df2d

@josephscott commented on PR #12306:


6 weeks ago
#6

@dmsnell my apologies for not getting to this sooner. I have no problem with these changes in the effort to avoid breaking things. What I'm not sure about is using preg_replace() instead of str_replace(). I haven't testing this specific case, but regular expressions are usually heavier.

@dmsnell commented on PR #12306:


6 weeks ago
#7

thanks @josephscott. I did anticipate that we’d lose some benefit in using preg_replace(), but I think the burden likely comes from the splitting, allocating, and reassembling more than the string operation, so I was thinking it’d still be worth it.

the reason for the change was consistency with the interface and a desire to reduce the risk of downstream code making assumptions about what this function produces. initially I left in the optimization you had, but then the behavior became inconsistent with respect to whitespace and the test suites demonstrated that.

happy to update this if we find regressions, at which point we should re-examine again the goal of split_selector_list(). it’s not in a release yet, so we have opportunity still.

#8 @dmsnell
6 weeks ago

In 62650:

Editor: Skip parsing in prepend_to_selector() for simple selectors.

Splitting a CSS selector involves unnecessary computation and allocation when the selector could not possibly involve more than one selector. It was discovered in profiling that this function was being called 4000+ times in a single request, and adding a pre-condition before splitting saved meaningful time on a page render.

This patch adds the pre-condition to verify that any commas present in a selector string can only be “comma tokens,” which split selectors at the top level, making it safe to fall back to simpler parsing that’s more efficient than general separator splitting.

This commit synchronizes work from the following Gutenberg PRs:

Developed in: https://github.com/WordPress/wordpress-develop/pull/12306
Discussed in: https://core.trac.wordpress.org/ticket/65533

Follow-up to [62607].

Props dmsnell, josephscott.
See #65533.

#10 @wildworks
5 weeks ago

@dmsnell, is there anything else we should address with this ticket? Should we close it?

#11 @wildworks
5 weeks ago

  • Keywords close added

#12 @wildworks
5 weeks ago

  • Resolutionfixed
  • Status newclosed

As the release of 7.1 Beta1 is approaching, I would like to close this issue. If there is anything else that needs to be addressed, please reopen this. However, since this ticket is an enhancement, I believe it will be necessary to change the milestone to 7.2 if you decide to keep the ticket open.

Note: See TracTickets for help on using tickets.