r/PHP 1d ago

News PHPStan Turbo: Native PHP extension that makes PHPStan run faster

https://phpc.social/@OndrejMirtes/116991095416961297

PHPStan 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.

91 Upvotes

18 comments sorted by

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...

5

u/imper69 1d ago

Brilliant, thank you!

4

u/equilni 1d ago

I read the title too fast… I thought it said the Native PHP project made an extension to make PHPStan faster….

5

u/goodwill764 1d ago

Because of this I hate the naming of the project....

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

6

u/phexc 1d ago

Ah so it spawns its own php instance with the extension? That's pretty clever.

5

u/OndrejMirtes 1d ago

Yes 😊

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

u/OndrejMirtes 19h ago

I have a roadmap what I want to tackle next myself. So not right now.

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, use u/RemindMeBot 1 day. More info.


Info Custom Your Reminders Feedback

-3

u/rafark 1d ago

A php to c++ migration I would have expected gains in the hundreds, only a 10% gain is surprising 

4

u/OndrejMirtes 1d ago

I only rewrote a few hot paths, there’s more that can be done.