Skip to content

Cache PHPStorm stubs - #4930

Closed
staabm wants to merge 7 commits into
phpstan:2.1.xfrom
staabm:cache-stubs
Closed

Cache PHPStorm stubs#4930
staabm wants to merge 7 commits into
phpstan:2.1.xfrom
staabm:cache-stubs

Conversation

@staabm

@staabm staabm commented Feb 14, 2026

Copy link
Copy Markdown
Contributor

refs phpstan/phpstan#14072 (comment)

requires ondrejmirtes/BetterReflection#40


with caching we see a ~5-6% perf improvement when result-cache is warmed. (single process with --debug)

grafik

I guess the caching is even more effective in parallel runs because it removes PHP-Parser::parse() of PHPStorm stubs on every worker-process creation.

Comment on lines +35 to +37
$stubsVersion = ComposerHelper::getPhpStormStubsVersion();
$cacheKey = sprintf('phpstorm-stubs-%s', $stubsVersion);
$variableCacheKey = sprintf('v1-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure whether this keys are correct this way.
whats the difference between $cacheKey and $variableCacheKey?

@staabm
staabm requested a review from ondrejmirtes February 14, 2026 14:55
@staabm
staabm marked this pull request as ready for review February 14, 2026 17:58
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd rather cache the same things the current locators cache. If you cache the "stubs", they are just PHP code that still needs to be parsed by the locator. So rather I'd have a caching locator around PhpInternalSourceLocator that would cache reflection objects which already have importFromCache/exportToCache.

@staabm
staabm marked this pull request as draft February 21, 2026 16:57
Comment thread src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php Outdated
continue;
}

$restored['constants'][$constantName] = ReflectionConstant::importFromCache($reflector, $cachedReflection);

@staabm staabm Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReflectionConstant/ReflectionParameter->importFromCache() is expensive.
it is dominated by PHPParser overhead

Image

looks like the parser instantiation happens multiple times

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes

Copy link
Copy Markdown
Member

For example one downside of this approach is that it loads into memory symbols it might not need in the current run.

Comment thread src/Internal/ComposerHelper.php Outdated
$installed = self::getInstalled();
$rootPackage = $installed['versions']['jetbrains/phpstorm-stubs'] ?? null;
if ($rootPackage === null) {
return self::$phpstomStubsVersion = self::UNKNOWN_VERSION;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo phpstom

}
}

$this->cache->save($this->cacheKey, $variableCacheKey, $exported);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it tries to have a single cache for everything? It could have a separate cache for each symbol.

@staabm

staabm commented Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

at the current state I cannot see any perf improvement anymore.

hopefully it gets better after ondrejmirtes/BetterReflection#41 is merged

@staabm

staabm commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

I think we have the same thing with turbo, so no need for the PR anymre

@staabm staabm closed this Aug 5, 2026
@staabm
staabm deleted the cache-stubs branch August 5, 2026 07:10
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.

4 participants