Cache the decoded overlay in memory - #108
Merged
Merged
Conversation
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 testpasses 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 sanitizereports no leak.make -C test valgrindreports no module allocation was definitely lost.Breaking
None. The output is identical and no directive is added.