Skip to content

Improve cached type checks by up to 55x - #74

Merged
nunomaduro merged 2 commits into
pestphp:5.xfrom
timacdonald:cache-reads-in-memory
Oct 30, 2026
Merged

nunomaduro merged 2 commits into
pestphp:5.xfrom
timacdonald:cache-reads-in-memory

Conversation

@timacdonald

@timacdonald timacdonald commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

tl;dr; Improve caching performance by up to 55x

Problem

FileCache::has and FileCache::get both call FileCache::all, which reads the entire cache file under a lock.

A run checks every file with has AND get, so a fully cached run includes the cache file twice per analysed file.

On a project with ~4,200 files the cache file is ~800KB and each include takes ~6ms, so a fully cached run spends most of its time re-reading the cache.

This keeps the cache contents in memory after the first read. persist and flush clear them, so the next read loads the file again.

Writes still go to disk under the lock as before, so forked workers still merge their results.

Benchmarks

Laravel application with 4,179 analysed files, 11 cores, two rounds each:

Run Before After
Nothing cached 33.0s / 31.3s 33.6s / 35.6s
Fully cached 29.5s / 30.3s 0.55s / 0.54s

The JSON report was identical across every run. A file with missing types reported the same uncovered lines from a cold run, from the cache, and after being changed while every other file was cached.

@timacdonald timacdonald changed the title Keep the cache contents in memory after the first read Improve caching performance by up to 55x Oct 30, 2026
@timacdonald
timacdonald marked this pull request as ready for review October 30, 2026 04:22
@timacdonald timacdonald changed the title Improve caching performance by up to 55x Improve cached type checks by up to 55x Oct 30, 2026
@nunomaduro
nunomaduro merged commit 349a881 into pestphp:5.x Oct 30, 2026
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