Skip to content

memoize wp_normalize_path#10770

Closed
josephscott wants to merge 16 commits intoWordPress:trunkfrom
josephscott:64538/memoize-wp-normalize-path
Closed

memoize wp_normalize_path#10770
josephscott wants to merge 16 commits intoWordPress:trunkfrom
josephscott:64538/memoize-wp-normalize-path

Conversation

@josephscott
Copy link
Copy Markdown
Contributor

https://core.trac.wordpress.org/ticket/64538


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

github-actions bot commented Jan 21, 2026

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @weston.ruter@git.wordpress.org.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Props josephscott, westonruter, 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds memoization to the wp_normalize_path() function to improve performance by caching normalized path results. The implementation uses a two-tier cache system (hot and warm) to balance memory usage and cache hit rates.

Changes:

  • Added memoization logic with a two-tier cache (hot/warm) limited to approximately 200 entries maximum
  • Added test coverage for edge cases (empty strings, integers, stringable objects)
  • Added tests to verify cache consistency across multiple calls

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/wp-includes/functions.php Implements two-tier memoization cache for wp_normalize_path function with automatic rotation when cache exceeds 100 entries
tests/phpunit/tests/functions.php Adds edge case tests for empty strings and integers, plus tests for stringable object handling and cache consistency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@josephscott
Copy link
Copy Markdown
Contributor Author

I worked with Opus to address the issues brought up by Copilot.

Copy link
Copy Markdown
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your continued work on this @josephscott — it seems more-than-justified to take this approach, so I have left some comments relating to the actual implementation.

is the idea behind the hot/warm cache here mostly to get frequently-used paths cached while leaving some room for most-recently-used items to also be in the cache? a balance between long-term frequency and bursts of the same path? I would love to see a brief explanation of why this was chosen.

public function __toString() {
return '/var/www/html\\test';
}
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a more representative test might be an SplFileInfo instance, since that is actually used in Core. as discovered in #10781 we can fix that currently-improper call in another patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no particular preference there. I will update the test_wp_normalize_path_with_stringable_object() to try that out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Copy Markdown
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @josephscott for all the work you put into this!

pento pushed a commit that referenced this pull request Mar 6, 2026
`wp_normalize_path()` is called thousands of times on a given request. This patch adds memoization via a function-local static variable. This reduces the call count to the underlying `wp_is_stream()` function, and measured in testing around a 66% cache hit rate.

In testing, for a site making 4000 calls to `wp_normalize_path()`, this patch led to a reduction in runtime from 1.4 ms to 0.4 ms on the test computer. While small, this time occurs early in the hotpath of the loading WordPress.

Developed in: #10770
Discussed in: https://core.trac.wordpress.org/ticket/64538

Props dmsnell, josephscott, mreishus, westonruter.
Fixes #64538.


git-svn-id: https://develop.svn.wordpress.org/trunk@61857 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 6, 2026

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 61857
GitHub commit: 1757a20

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Mar 6, 2026
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Mar 6, 2026
`wp_normalize_path()` is called thousands of times on a given request. This patch adds memoization via a function-local static variable. This reduces the call count to the underlying `wp_is_stream()` function, and measured in testing around a 66% cache hit rate.

In testing, for a site making 4000 calls to `wp_normalize_path()`, this patch led to a reduction in runtime from 1.4 ms to 0.4 ms on the test computer. While small, this time occurs early in the hotpath of the loading WordPress.

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

Props dmsnell, josephscott, mreishus, westonruter.
Fixes #64538.

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


git-svn-id: http://core.svn.wordpress.org/trunk@61144 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.

4 participants