Skip to content

Cache the decoded overlay in memory - #108

Merged
beetlebugorg merged 1 commit into
mainfrom
perf/overlay-decode-cache
Aug 28, 2026
Merged

Cache the decoded overlay in memory#108
beetlebugorg merged 1 commit into
mainfrom
perf/overlay-decode-cache

Conversation

@beetlebugorg

@beetlebugorg beetlebugorg commented Aug 28, 2026

Copy link
Copy Markdown
Owner

What

The watermark operation keeps a process cache of decoded overlays, keyed by the overlay URL. A repeat request clones the cached overlay and skips the read and decode. The disk cache still holds the overlay bytes.

Why

Every watermark request read the cached overlay bytes and decoded them again with MagickReadImage. The decode is the slow step, and it repeats for every request that uses the same overlay.

The cache is safe for the worker threads. It clones the overlay under a lock, because one ImageMagick wand is not safe for two threads at once. It holds a fixed, small number of overlays, because a decoded overlay is large. It expires an entry after DimsOverlayCacheMaxAge, the same age the disk cache uses. The allowlist check runs before the cache lookup, so a memory hit obeys the same rule as a disk hit.

Verify

make -C test test passes all 271 cases, including the watermark golden cases, which now hit the cache on the second identical overlay and match the golden bytes. make -C test sanitize reports no leak. make -C test valgrind reports no module allocation was definitely lost.

Breaking

None. The output is identical and no directive is added.

- Add a process cache of decoded overlays, keyed by the overlay URL.
- Clone the overlay under a lock, so one wand never serves two threads.
- Bound the cache by a fixed count and expire an entry after DimsOverlayCacheMaxAge.
@beetlebugorg
beetlebugorg merged commit 5767116 into main Aug 28, 2026
6 checks passed
@beetlebugorg
beetlebugorg deleted the perf/overlay-decode-cache branch August 28, 2026 20:48
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