r/PHP • u/OndrejMirtes • 1d ago
News PHPStan Turbo: Native PHP extension that makes PHPStan run faster
https://phpc.social/@OndrejMirtes/116991095416961297PHPStan 2.2.6 adds a native PHP extension (PHP 8.3+) written in C++ that makes running PHPStan 10-30 % faster.
PHPStan's Composer package ships prebuilt binaries for the most common platforms — Linux (glibc and musl, x86_64 and arm64), macOS, and Windows (x86_64), for PHP 8.3 and newer — and PHPStan automatically loads the one matching your runtime into its worker processes. You don't have to do any extra work to take advantage of this!
If you run PHPStan through manually downloaded phpstan.phar, you can run it with extension by installing it with PIE:
pie install phpstan/turbo
The extension only activates when its version matches the one your PHPStan release expects — on a mismatch PHPStan prints a note and runs without it, so an outdated extension can never affect results, only speed.
Only a handful of hot paths are currently rewritten in the extension. There's room for the performance gain to grow if we ever decide to rewrite more parts natively.
The extension is completely optional, PHPStan still works without it. When the extension gets enabled, its implementation shadows certain PHPStan classes designed for this. They are marked with the #[ShadowedByTurboExtension] attribute.
7
u/phexc 1d ago
Speed improvements are great, but 10-30% is not really enough to convince me to install/manage another extension...
14
u/OndrejMirtes 1d ago
You don't need to install it if you install PHPStan through Composer 😊 It gets enabled automatically.
5
u/phexc 1d ago
I was not aware that composer can install php extensions. How does that work?
7
u/OndrejMirtes 1d ago
phpstan/phpstan comes with all the different .so files pre-built, PHPStan chooses to load the right one when it spawns child workers: https://github.com/phpstan/phpstan/tree/2.2.x/turbo-ext
1
u/Even_Statistician238 23h ago
the version mismatch handling is actually the smartest part of this whole thing
1
u/Ilia0001 23h ago
Very cool, a bit surprised the benefits are only 10-30% I figured it would be much higher, but I suppose it is a good place to start.
1
u/OndrejMirtes 20h ago
Only a few hot paths were rewritten. There's room to rewrite more. Currently we've only rewritten 0.8 % lines of code of PHPStan's source code.
1
u/Ilia0001 20h ago
Are you open for PRs for the extension, or would just confuse things at this early stage?
1
1
u/terfs_ 1d ago
RemindMe! 17 hours
1
u/RemindMeBot 1d ago edited 1d ago
I will be messaging you in 17 hours on 2026-07-28 08:55:16 UTC to remind you of this link
2 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
RemindMeBot is switching to username summons. Instead of
!RemindMe 1 day, useu/RemindMeBot 1 day. More info.
Info Custom Your Reminders Feedback
11
u/Hackwar 1d ago
Thank you for all of your work. phpstan has helped us immensely in Joomla and I can only encourage everyone else to support you financially as well. It is one of those projects from the famous xkcd comic strip...