Repository navigation
Cache a class's ancestors without the class itself - #6593
Merged
ondrejmirtes merged 1 commit intoSep 25, 2026
Merged
ondrejmirtes merged 1 commit into
ondrejmirtes merged 1 commit into
Conversation
getAncestors() cached an array that contained $this, so every ClassReflection that had answered it referenced itself. PHPStan runs with gc_disable(), so a ClassReflection the reflection provider does not keep, like a withTypes() copy, then stayed in memory until the process ended, together with its template type map. The cache now leaves the class out, and getAncestors() adds it back first, so the returned array is the same. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Member
|
Thank you very much! |
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.
ClassReflection::getAncestors()cached an array that contained$this, so everyClassReflectionthat had answered it referenced itself. PHPStan runs withgc_disable(), so such a cycle is never freed. That matters for aClassReflectionthat the reflection provider does not keep, such as awithTypes()copy. It stayed in memory until the process ended, together with its template type map and variance map.The cache now leaves the class out, and
getAncestors()adds it back first, so the returned array is the same.Effect
Single process, from source. The peak is PHPStan's own, with
BytesHelperpatched locally to print MB. Agc_collect_cycles()at the end of the run separates live data from uncollected cycles:Both rounds of each arm gave the same numbers, and the JSON output is identical before and after on both projects. With 4 workers, tempest's largest worker peaks at 350/340 MB instead of 374/370 MB. CPU is unchanged: on tempest the medians of 3 interleaved rounds are 67.75 s before and 67.73 s after.
On 2.3.x the effect is larger. Bidirectional type narrowing creates more of these copies, and on the Laravel app this cycle is about 77 MB of the uncollected garbage there.
Test
ClassReflectionTest::testAncestorsDoNotKeepClassReflectionAlive()takes awithTypes()copy, callsgetAncestors(), drops it with the GC disabled and checks aWeakReferenceto it. It fails without the change.On 2.3.x,
ClassReflectionis shadowed by the turbo extension. ItsgetAncestors()inturbo-ext/src/ClassReflection.cppstores the same array, so it needs the same change after this is merged up.🤖 Generated with Claude Code