Skip to content

Speed up native URL rewriting#283

Merged
adamziel merged 4 commits into
trunkfrom
codex/native-url-rewrite-cache
May 19, 2026
Merged

Speed up native URL rewriting#283
adamziel merged 4 commits into
trunkfrom
codex/native-url-rewrite-cache

Conversation

@adamziel
Copy link
Copy Markdown
Collaborator

What

  • Route URLInTextProcessor through NativeURLInTextProcessor when the native extension is loaded, while preserving the public PHP API around parsing, validation, and replacement.
  • Add a bounded ring cache for parsed native URL candidates.
  • Add a bounded ring cache in wp_rewrite_urls() for final rewrite decisions keyed by mapping, token type, and raw URL.
  • Update the native extension README to describe the wrapper-based URL native default.

Why

Large imports can contain hundreds of thousands of repeated URL occurrences. The native scanner can find candidates quickly, but the old public path still paid repeated PHP WHATWG parsing and replace_base_url() costs for the same raw URLs.

The caches are capped at 4096 entries each to avoid unbounded memory growth.

Benchmarks

Fixture: 5,000 sampled post_content values from a large Studio/reprint dump, 16.9 MB decoded content, 350,000 URL occurrences, 2 unique raw URLs. Runtime: PHP.wasm 8.4.21 with wp_native_apis loaded.

Measure Before After Delta
wp_rewrite_urls() wall time 90.18s 3.57s -86.60s (-96.0%)
Output hash 44c9750c... 44c9750c... identical
Public URL processor parent PHPURLInTextProcessor NativeURLInTextProcessorWrapper native scanner active

Validation

  • vendor/bin/phpcbf -d memory_limit=1G components/DataLiberation/URL/class-nativeurlintextprocessorwrapper.php components/DataLiberation/URL/class-urlintextprocessor.php components/DataLiberation/URL/functions.php
  • vendor/bin/phpcs -d memory_limit=1G components/DataLiberation/URL/class-nativeurlintextprocessorwrapper.php components/DataLiberation/URL/class-urlintextprocessor.php components/DataLiberation/URL/functions.php
  • vendor/bin/phpunit -c phpunit.xml components/DataLiberation/Tests/URLInTextProcessorTest.php components/DataLiberation/Tests/RewriteUrlsTest.php
  • php extensions/native-apis/tests/verify-native-apis.php --allow-missing
  • native-wrapper smoke test with a fake native scanner
  • WASM benchmark with wp_native_apis loaded

Full composer test currently has one unrelated failure in WordPress\\Blueprints\\Tests\\Unit\\Steps\\DefineConstantsStepTest::testAddNewConstantsToInvalidWpConfig on PHP 8.4.5: the expected Blueprint error text is absent from the subprocess exception output.

@adamziel adamziel marked this pull request as ready for review May 19, 2026 19:53
@adamziel adamziel merged commit cb3aa25 into trunk May 19, 2026
32 checks passed
@adamziel adamziel deleted the codex/native-url-rewrite-cache branch May 19, 2026 19:53
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