Skip to content

HTML API: Hoist strlen() out of the class_list() loop condition - #12964

Draft
mukeshpanchal27 wants to merge 1 commit into
WordPress:trunkfrom
mukeshpanchal27:perf/hoist-strlen-class-list
Draft

HTML API: Hoist strlen() out of the class_list() loop condition#12964
mukeshpanchal27 wants to merge 1 commit into
WordPress:trunkfrom
mukeshpanchal27:perf/hoist-strlen-class-list

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Aug 11, 2026

Copy link
Copy Markdown
Member

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5
Used for: Initial code skeleton; final implementation was reviewed and edited by me.


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.

`$class` is a local string that is never reassigned inside the loop, yet
`strlen()` was called twice per iteration: once to evaluate the `while`
condition and once more to check whether skipping boundary characters ran
past the end. Compute the length once before the loop instead, matching
the idiom already used elsewhere in core.

Follow-up to [63171].

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@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.

@westonruter

Copy link
Copy Markdown
Member

I recall this coming up before, that strlen() may be a different case than count() with an array. PHP may store an internal string length more readily than it does for arrays.

@sirreal

sirreal commented Aug 11, 2026

Copy link
Copy Markdown
Member

I've also heard that from @dmsnell several times and decided to verify it myself, or at least have Copilot verify it.

It says:

[strlen()] is a built-in internal function, and when the optimizer can prove the argument is already a string, it can even fold it to a direct string-length read at compile time.

Why it’s cheap:

  • PHP strings store their length alongside the string buffer, so length retrieval is O(1).
  • For non-constant cases, the runtime still has to perform a function/opcode dispatch, parameter handling, and type checks.
  • So the length access itself is extremely cheap…
    Referencing:

Previously, we'd relied on this extensively across the HTML API instead of using $strlen variables, although that's changed and there are plenty examples of storing the string length in a variable now. I don't think there's anything harmful or wrong with this change although the performance difference is likely negligible.

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.

3 participants