Repository navigation
Conversation
Member
|
Hey, please submit an issue with the description and reproducer first, thank you. |
alfredbez
force-pushed
the
fix/literal-array-memory-2.3
branch
from
October 10, 2026 20:11
9002975 to
2023a6c
Compare
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.
When analysing files with large literal arrays, PHPStan 2.3.x consumed ~55% more peak memory than 2.2.16 (139 MB vs 77 MB on 20,000 items).
Memory consumption on reproducer (
php run.php [entries]):What caused the increase:
ScalarHandler, each scalar literal allocated anInitializerExprContext, an 800-byte closure (typeCallback), and anExpressionResult. For 20,000 items (40,000 scalar expressions), closures and context objects alone consumed over 38 MB. Precomputing constant scalar types (ConstantStringType,ConstantIntegerType,ConstantFloatType) directly avoids allocating closures and contexts.ArrayHandleraccumulated all key and value results into$itemResultsand captured them in the array'stypeCallbackclosure. Scalar item types are constant and can be created on demand in the callback, so$itemTypesonly records non-scalar items.ArrayHandlernow evicts the item's key and value fromExpressionResultStorageonce the item's callback has finished. This prevents 40,000ExpressionResultinstances from accumulating inSplObjectStoragethroughout the loop.AssignHandlerinvokedprocessArrayByRefItemson every assigned array literal, even when the array contained no by-reference elements. Guarding withhasArrayReference()avoids traversing large arrays during assignment, and lookups fall back to$scope->getType()if a stored result is absent.DuplicateKeysInLiteralArraysRuleeagerly pretty-printed all keys viaexprPrinterand stored them in$printedValues. Storing only the first node occurrence and formatting printed representations lazily when a duplicate key is actually detected avoids allocating tens of thousands of printed strings for unique keys.All 22,299 tests pass.