Skip to content

Avoid a full resource cache scan on every miss when nothing can be ev… - #1275

Open
arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:resource-cache-evict-scan
Open

arturobernalg wants to merge 1 commit into
apache:mainfrom
arturobernalg:resource-cache-evict-scan

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

When the static resource cache is full and every entry was validated within the TTL, each cache miss iterates the whole cache in evict() without being able to remove anything, then drops the new entry. Under load every miss becomes a full scan; a JFR profile of the server showed 97% of CPU in Cache.evict().

This change remembers the earliest time a skipped entry can expire and skips the scan until then, so there is at most one scan per expiry instant instead of one per miss. The eviction policy is unchanged.

I measured it on a real Tomcat built from main, with the default cache configuration. The client was a keep-alive load generator with 32 connections, with server and client pinned to separate cores. Each run had 10 s of warm-up and 30 s of measurement, Tomcat was restarted before every run, and original and patched runs alternated.

Scenario Build Requests/s p50 p99 p99.9
32,000 files, cache full original 5,566 - 5,878 4.5 ms 22 ms 35 ms
32,000 files, cache full patched 32,244 - 33,738 0.98 ms 3.1 ms 6.7 ms
4,000 files, fits in cache original 205,309 - 210,125 0.13 ms 0.51 ms 1.3 ms
4,000 files, fits in cache patched 207,489 - 213,393 0.13 ms 0.50 ms 1.3 ms
32,000 files, 1 GB cache original 122,610 0.19 ms 1.2 ms 2.7 ms
32,000 files, 1 GB cache patched 126,111 0.19 ms 1.2 ms 2.6 ms

With a full cache, throughput improves about 5.8x and p99 latency drops about 7x. When the working set fits in the cache, or the cache never fills, the results are unchanged. The remaining gap to the large cache case is the per-miss warning and reading uncached files, which this change does not touch.

The new tests in TestCache cover a full cache, eviction resuming after entries expire, staggered expiry and the directory listing path. BZ 66564 proposes a broader redesign of eviction order; this change is independent of it.

…icted

When the static resource cache is full and every entry was validated
within the TTL, each cache miss scanned the whole cache without being
able to evict anything. Remember when the first entry expires and skip
the scan until then.
@arturobernalg
arturobernalg force-pushed the resource-cache-evict-scan branch from 6be0efe to 7337c52 Compare November 27, 2026 13:12
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