Skip to content

fix: bound the in-memory attachment ID cache - #1192

Merged
pirate-bot merged 2 commits into
developmentfrom
bugfix/1139
Oct 22, 2026
Merged

pirate-bot merged 2 commits into
developmentfrom
bugfix/1139

Conversation

@selul

@selul selul commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Marius · project thread

All Submissions:

Changes proposed in this Pull Request:

Part of #1139. Optml_Attachment_Cache::$cache_map keeps every distinct attachment URL looked up during a request, misses included, with no limit. On pages with many offloaded or DAM image URLs it grows for the whole request.

The map now holds at most MAX_MEMORY_ENTRIES (2000) URLs. When it's full, the older half is dropped. Dropped URLs are still served from the persistent cache (transient, or the object cache when one is used), so the only cost is one extra lookup per dropped URL. Updating a URL that's already in the map never triggers a drop.

Scope: this bounds the growth path the issue's diagnosis names. The telemetry doesn't say how many URLs those pages had, so I can't prove this map alone is what took the 128 MB. WordPress's own runtime caches of transients and notoptions also grow per URL, and this PR doesn't touch those. If the crash still shows up after this ships, the next place to look is those per-URL transient reads.

How to test the changes in this Pull Request:

  1. Enable Image Storage and render a page with several thousand distinct offloaded image URLs.
  2. Compare memory_get_peak_usage() before and after this change. The in-memory map stays at or below 2000 entries.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?
Check Result
PHPUnit full suite (WP trunk, PHP 8.3) PASS, 431 tests
New tests/test-attachment-cache.php: map bounded on cache misses, dropped entries still served from the persistent cache, updating a known URL keeps the map PASS
Mutation: remove the cap 2 of the new tests fail
Mutation: cache-miss path writes the map directly The cache-miss test fails (2001 vs 1001)
PHP 7.4 lint, phpcs PASS

Not proven: that this fixes the production memory fatal (see Scope). Codex review was skipped because the Codex CLI isn't available in this environment.


Generated by Claude Code

The request-local map in Optml_Attachment_Cache kept every distinct URL
looked up during a request, including misses, with no limit. It now keeps
at most 2000 URLs and drops the older half when full; dropped URLs are
still served from the persistent cache (transient or object cache).

Refs #1139

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MEXWtYdE6e3ALYtqtP8j2p
@selul selul self-assigned this Oct 20, 2026
@selul
selul requested a review from pirate-bot October 20, 2026 20:24

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Changes requested

The cap works, but no added test covers the cache-miss path, and the remember() value type is wrong.

Validation details
  • Files reviewed: 2/2 changed files. The "Caps and limits: Realistic volume" row stayed unchecked.
  • PHPUnit 9.6.22 on PHP 8.3.35 ran the new tests at HEAD: cd /repo; set -o pipefail; timeout 280 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Attachment_Cache::' 2>&1 … (run R5, exit 0)
  • With the cache-miss path reverted, the new tests still pass: set -o pipefail; cd /workspace/checks/C6/mutant && rm /var/www/html/wp-content/plugins/optimole-wp && (timeout 300 vendo… printed OK (3 tests, 8 assertions) (run R8)
  • A miss-only control test catches that revert: `set -o pipefail; cd /workspace/checks/C6/mutant && cat > tests/test-c6-control.php <<'EOF'
Untested areas - Caps and limits: Realistic volume. No data shows how many distinct URLs real pages hold. Issue #1139 says the telemetry lacks it. - Jest and Playwright suites were not run. The change does not touch them.
🤖 Automated review · run code-review-agent_6ad7cdfbc64532.06410597. --- **🤖 Review agent** — review posted ✅ on `f4e15492` · [changes requested · 2 findings](https://github.com//pull/1192#pullrequestreview-5591450945) · 17 min Run `code-review-agent_6ad7cdfbc64532.06410597` · [trail](https://agents.vertistudio.com/admin/trails/github-pr/Codeinwp-optimole-wp-1192)

Comment thread tests/test-attachment-cache.php Outdated
Comment thread inc/attachment_cache.php Outdated
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MEXWtYdE6e3ALYtqtP8j2p
@selul
selul requested a review from pirate-bot October 20, 2026 21:05

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Approved

The PR passes: both earlier findings are fixed, and the new commit adds no new problem.

Validation details
  • Files reviewed: 2/2 changed files. The "Caps and limits: Realistic volume" question stayed unchecked.
  • PHPUnit 9.6.22 on PHP 8.3.35 ran the cache tests at HEAD: cd /repo && set -o pipefail; timeout 280 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Attachment_Cache::' 2>&… printed OK (3 tests, 7 assertions) (run R2)
  • A copy with only the cache-miss write reverted fails the new test: P=/var/www/html/wp-content/plugins; set -o pipefail; ln -sfn /workspace/checks/C1/mutant $P/optimole-wp && cd /workspace… printed Failed asserting that actual size 2001 matches expected size 1001. (run R4)
  • Caller and cap analysis is reused from run code-review-agent_6ad7cdfbc64532.06410597 at f4e1549, not rerun. The new commit leaves that code unchanged.
Untested areas
  • Caps and limits: Realistic volume. No data shows how many distinct URLs real pages hold. Issue #1139 says the telemetry lacks it.
  • The base-tree run of the changed test failed for a sandbox reason. The base code also lacks the new constant.
  • Jest and Playwright suites were not run. The change does not touch them.

🤖 Automated review · run code-review-agent_6ad7d7a5a67722.57858839.


🤖 Review agent — review posted ✅ on cc26c370 · approved · 0 findings · 6 min

Run code-review-agent_6ad7d7a5a67722.57858839 · trail

@pirate-bot

Copy link
Copy Markdown
Collaborator

Plugin build for cc26c37 is ready 🛎️!

@pirate-bot
pirate-bot merged commit 8e4a54b into development Oct 22, 2026
22 checks passed
@pirate-bot
pirate-bot deleted the bugfix/1139 branch October 22, 2026 08:02
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