Skip to content

Performance: avoid over-allocation in wp_is_numeric_array()#12100

Draft
dmsnell wants to merge 1 commit into
WordPress:trunkfrom
dmsnell:perf/avoid-over-allocating-numeric-array-keys
Draft

Performance: avoid over-allocation in wp_is_numeric_array()#12100
dmsnell wants to merge 1 commit into
WordPress:trunkfrom
dmsnell:perf/avoid-over-allocating-numeric-array-keys

Conversation

@dmsnell
Copy link
Copy Markdown
Member

@dmsnell dmsnell commented Jun 5, 2026

When a trace of allocations revealed that wp_is_numeric_array() accounted for a significant fraction of the allocations in a page render, it was observed that the function eagerly allocates and copies array keys and then filters them when all it wants to know is whether a single key in the array meets a condition.

In this patch the array_filter( array_keys() ) invocation is replaced with early-aborting iteration to avoid the memory allocation and copying.

This patch prepared during WCEU 2026 Contributor Day.

Follow-up to [34927].
Follow-up to 374b39d.

When a trace of allocations revealed that `wp_is_numeric_array()`
accounted for a significant fraction of the allocations in a page
render, it was observed that the function eagerly allocates and copies
array keys and then filters them when all it wants to know is whether a
single key in the array meets a condition.

In this patch the `array_filter( array_keys() )` invocation is replaced
with early-aborting iteration to avoid the memory allocation and
copying.

This patch prepared during WCEU 2026 Contributor Day.

Follow-up to [34927].
Follow-up to 374b39d.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 5, 2026

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.

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.

1 participant