From 44dcafa53d13381a1fa252c46465f7e64eeb5457 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 13 Nov 2026 13:35:54 +0000 Subject: [PATCH 1/3] Do not look up the extra offset-accessible classes an object type does not extend ObjectType::isOffsetAccessible() asked ClassReflection::is() about each of EXTRA_OFFSET_CLASSES - DOMNodeList, ResourceBundle, PDORow, FFI\CData and others - for every object type that is not an ArrayAccess. That looked each class up, and every lookup is a dependency of the analysed file now: a class found was expanded like a referenced one, resolving the PHPDoc of the built-in class from its PhpStorm stub file and keeping it in memory, and a class missing, like Dom\NodeList, became a value dependency of nearly every file. The answer depends only on the ancestors of the class, which are tracked already, so it is found among them instead, with the same result as is(). Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6 --- src/Type/ObjectType.php | 8 +++++++- turbo-ext/src/ObjectType.cpp | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 0115cea2a85..4b96125e2d7 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -1505,7 +1505,13 @@ private function isExtraOffsetAccessibleClass(): TrinaryLogic } foreach (self::EXTRA_OFFSET_CLASSES as $extraOffsetClass) { - if ($classReflection->is($extraOffsetClass)) { + // like $classReflection->is($extraOffsetClass), without looking up the class: the answer + // depends only on the ancestors, and a lookup is recorded as a dependency of the analysed file + $ancestor = $classReflection->getAncestorWithClassName($extraOffsetClass); + if ($ancestor === null) { + continue; + } + if ($ancestor === $classReflection || $classReflection->isSubclassOfClass($ancestor)) { return TrinaryLogic::createYes(); } } diff --git a/turbo-ext/src/ObjectType.cpp b/turbo-ext/src/ObjectType.cpp index 1e81b93bad3..10f66302d3a 100644 --- a/turbo-ext/src/ObjectType.cpp +++ b/turbo-ext/src/ObjectType.cpp @@ -1888,9 +1888,17 @@ class ObjectType zend_object *reflection = Z_OBJ_P(classReflection.raw()); for (const pt_superglobal_name &extraOffsetClass : pt_ot_extra_offset_classes) { - bool is; - if (UNEXPECTED(!reflectionIs(reflection, extraOffsetClass.name, extraOffsetClass.len, is))) return -1; - if (is) return PT_TRI_YES; + /* like $classReflection->is(), without looking up the class: the + * answer depends only on the ancestors, and a lookup is recorded + * as a dependency of the analysed file */ + zv::Val name = zv::Val::string(extraOffsetClass.name, extraOffsetClass.len); + zv::Val ancestor = pt_type_call(reflection, PT_LC("getancestorwithclassname"), 1, name.raw()); + if (UNEXPECTED(ancestor.isUndef())) return -1; + if (ancestor.isNull()) continue; + if (Z_OBJ_P(ancestor.raw()) == reflection) return PT_TRI_YES; + bool subclass; + if (UNEXPECTED(!pt_type_call_bool(reflection, PT_LC("issubclassofclass"), 1, ancestor.raw(), subclass))) return -1; + if (subclass) return PT_TRI_YES; } bool isInterface; From 0933b6251c4b093f4c9c8b968a5de2f190353bcd Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 13 Nov 2026 13:35:54 +0000 Subject: [PATCH 2/3] Check the directory locators' own symbol maps instead of a merged copy OptimizedDirectorySourceLocatorGroup built a map of the symbols of all its locators, a second copy of every class, function and constant name for the rest of the process. It checks the maps of the locators themselves now, shared instead of copied, and still asks only the locators declaring the name. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6 --- .../OptimizedDirectorySourceLocator.php | 11 ++-- .../OptimizedDirectorySourceLocatorGroup.php | 53 ++++++++----------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php index a7ad252f288..f05cef1bff0 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php @@ -77,18 +77,19 @@ public function fillScanned(array $classToFile, array $functionToFiles, array $c } /** - * The lowercase class names, the lowercase function names and the normalized constant names - * it declares - see OptimizedDirectorySourceLocatorGroup. + * The maps of the symbols it declares, keyed by the lowercase class names, the lowercase + * function names and the normalized constant names - see OptimizedDirectorySourceLocatorGroup. + * They are not changed after the scan, so they are shared, not copied. * - * @return array{list, list, list} + * @return array{array, array, array} */ - public function getDeclaredSymbolNames(): array + public function getSymbolMaps(): array { if ($this->awaitingScan) { throw new ShouldNotHappenException('Symbols were looked up in a directory whose batch has not been scanned yet.'); } - return [array_keys($this->classToFile), array_keys($this->functionToFiles), array_keys($this->constantToFile)]; + return [$this->classToFile, $this->functionToFiles, $this->constantToFile]; } /** diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php index 6702410fb93..fdcd9d4cb7d 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php @@ -11,21 +11,22 @@ use PHPStan\Reflection\ConstantNameHelper; use function array_merge; use function count; -use function is_int; use function strtolower; /** * Asks OptimizedDirectorySourceLocators as if they were asked in turn, the way - * AggregateSourceLocator would: the first one locating the symbol answers. Which of them declare - * it is looked up once, in a map of all their symbols built when the group is first asked, instead - * of in every one of them - a project with many Composer packages has a locator for each classmap - * directory, and most names asked for are declared in none of them. + * AggregateSourceLocator would: the first one locating the symbol answers. Only the ones whose + * symbol map has the name are asked, and the maps are checked directly instead of through each + * locator - a project with many Composer packages has a locator for each classmap directory, and + * most names asked for are declared in none of them. The maps are the locators' own: one map of + * all their symbols would answer in one lookup, but it would keep a second copy of every name for + * the rest of the process. */ final class OptimizedDirectorySourceLocatorGroup implements SourceLocator { - /** @var array{array>, array>, array>}|null class, function and constant name => the locators declaring it */ - private ?array $declaringLocators = null; + /** @var array{list>, list>, list>}|null the class, function and constant maps of each locator, by its index */ + private ?array $symbolMaps = null; /** * @param list $locators @@ -62,13 +63,12 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): return null; } - $this->declaringLocators ??= $this->buildDeclaringLocators(); - $indexes = $this->declaringLocators[$kind][$name] ?? null; - if ($indexes === null) { - return null; - } + $this->symbolMaps ??= $this->collectSymbolMaps(); + foreach ($this->symbolMaps[$kind] as $index => $symbols) { + if (!isset($symbols[$name])) { + continue; + } - foreach (is_int($indexes) ? [$indexes] : $indexes as $index) { $reflection = $this->locators[$index]->locateIdentifier($reflector, $identifier); if ($reflection !== null) { return $reflection; @@ -79,28 +79,19 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): } /** - * @return array{array>, array>, array>} + * @return array{list>, list>, list>} */ - private function buildDeclaringLocators(): array + private function collectSymbolMaps(): array { - $declaringLocators = [[], [], []]; - foreach ($this->locators as $index => $locator) { - foreach ($locator->getDeclaredSymbolNames() as $kind => $names) { - foreach ($names as $name) { - $declaring = $declaringLocators[$kind][$name] ?? null; - if ($declaring === null) { - $declaringLocators[$kind][$name] = $index; - continue; - } - - $declaring = is_int($declaring) ? [$declaring] : $declaring; - $declaring[] = $index; - $declaringLocators[$kind][$name] = $declaring; - } - } + $symbolMaps = [[], [], []]; + foreach ($this->locators as $locator) { + [$classes, $functions, $constants] = $locator->getSymbolMaps(); + $symbolMaps[0][] = $classes; + $symbolMaps[1][] = $functions; + $symbolMaps[2][] = $constants; } - return $declaringLocators; + return $symbolMaps; } #[Override] From 1cda34406300d7756ef4526df901456dc49a7eff Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Fri, 13 Nov 2026 13:35:54 +0000 Subject: [PATCH 3/3] Do not keep a lowercased copy of all functions in the PhpStorm stubs Nearly all of them are lowercase in PhpStormStubsMap already and are looked up there directly; only the others are kept lowercased. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6 --- .../CachedPhpInternalSourceLocator.php | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php index e5a0fa8b99d..bca6a852036 100644 --- a/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php +++ b/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php @@ -18,8 +18,8 @@ use PHPStan\Internal\ComposerHelper; use PHPStan\Php\PhpVersion; use PHPStan\Reflection\BetterReflection\SourceStubber\ExtensionVersionProvider; -use function array_change_key_case; use function array_key_exists; +use function array_keys; use function is_array; use function is_file; use function is_string; @@ -55,8 +55,8 @@ final class CachedPhpInternalSourceLocator implements SourceLocator private const RELATIVE_FILENAME_PREFIX = 'phpstorm-stubs:'; - /** @var array|null lowercase name => stub file, of every function in the stubs of any extension version */ - private static ?array $stubFunctions = null; + /** @var array|null lowercase names of the functions in the stubs not listed in lowercase in PhpStormStubsMap::FUNCTIONS, see isStubFunction() */ + private static ?array $otherStubFunctions = null; private ?string $variableCacheKey = null; @@ -178,29 +178,46 @@ private function resolveStubFilename(array $cached): ?array return $cached; } - /** - * @return list - */ /** * Whether the stubber could have a function: the stubber has no cheap way to tell, its own * answer parses the stub file. The functions of the extension versions it selects are added * to the default ones, so they are all included. + * + * Nearly all names in the map are lowercase already and are looked up in it directly: a + * lowercased copy of the whole map would stay in memory for the rest of the process. */ private static function isStubFunction(string $lowercaseName): bool { - if (self::$stubFunctions === null) { - $functions = array_change_key_case(PhpStormStubsMap::FUNCTIONS); + if (array_key_exists($lowercaseName, PhpStormStubsMap::FUNCTIONS)) { + return true; + } + + if (self::$otherStubFunctions === null) { + $functions = []; + foreach (array_keys(PhpStormStubsMap::FUNCTIONS) as $name) { + $lowercaseFunctionName = strtolower($name); + if ($lowercaseFunctionName === $name) { + continue; + } + + $functions[$lowercaseFunctionName] = true; + } foreach (PhpStormStubsMap::EXTENSION_VERSIONS as $versionMaps) { foreach ($versionMaps as $versionMap) { - $functions += array_change_key_case($versionMap['functions']); + foreach (array_keys($versionMap['functions']) as $name) { + $functions[strtolower($name)] = true; + } } } - self::$stubFunctions = $functions; + self::$otherStubFunctions = $functions; } - return array_key_exists($lowercaseName, self::$stubFunctions); + return array_key_exists($lowercaseName, self::$otherStubFunctions); } + /** + * @return list + */ #[Override] public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array {