Skip to content

Editor: prepend_to_selector: optimized with str_replace()#12306

Closed
josephscott wants to merge 1 commit into
WordPress:trunkfrom
josephscott:editor/optimize-prepend-to-selector
Closed

Editor: prepend_to_selector: optimized with str_replace()#12306
josephscott wants to merge 1 commit into
WordPress:trunkfrom
josephscott:editor/optimize-prepend-to-selector

Conversation

@josephscott

@josephscott josephscott commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

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

This PR syncs the Gutenberg PRs:

Use of AI Tools

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

  • Codex ported the prepend_to_selector() tests to append_to_selector() tests.

⚠️ This PR depends on #11857 going in first and fixing parsing issues for selector lists.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props josephscott, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@dmsnell

dmsnell commented Jun 29, 2026

Copy link
Copy Markdown
Member

@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

dmsnell commented Jun 30, 2026

Copy link
Copy Markdown
Member

@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()

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

This PR syncs the foloowing Gutenberg PRs:
 - WordPress/gutenberg#76556
 - WordPress/gutenberg#79499

Opus 4.6 used in the initial patch creation in Gutenberg.

Co-Authored-By: Dennis Snell <dmsnell@git.wordpress.org>
@dmsnell dmsnell force-pushed the editor/optimize-prepend-to-selector branch from a41df2d to 7a34811 Compare July 6, 2026 21:20
@dmsnell

dmsnell commented Jul 6, 2026

Copy link
Copy Markdown
Member

Rebased in preparation for merge: previous HEAD was a41df2d

@josephscott

Copy link
Copy Markdown
Contributor Author

@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

dmsnell commented Jul 6, 2026

Copy link
Copy Markdown
Member

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.

pento pushed a commit that referenced this pull request Jul 7, 2026
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:
 - WordPress/gutenberg#76556
 - WordPress/gutenberg#79499

Developed in: #12306
Discussed in: https://core.trac.wordpress.org/ticket/65533

Follow-up to [62607].

Props dmsnell, josephscott.
See #65533.


git-svn-id: https://develop.svn.wordpress.org/trunk@62650 602fd350-edb4-49c9-b593-d223f7449a82
@dmsnell

dmsnell commented Jul 7, 2026

Copy link
Copy Markdown
Member

Merged in 62650
608993d

@dmsnell dmsnell closed this Jul 7, 2026
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Jul 7, 2026
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:
 - WordPress/gutenberg#76556
 - WordPress/gutenberg#79499

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

Follow-up to [62607].

Props dmsnell, josephscott.
See #65533.

Built from https://develop.svn.wordpress.org/trunk@62650


git-svn-id: http://core.svn.wordpress.org/trunk@61935 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants