DEV Community

loading...

PHP 8 performance 🐘🐘🐘🐘🐘🐘🐘🐘🚀

dakujem profile image Andrej Rypo Updated on ・1 min read

Is anyone else seeing a big performance boost on PHP 8?

A couple of tests in my small library dealing with reflection and a bit of regexps are running 30-50% faster 💪!
It is great, but the isolated unit tests are by no means representative.

Has anyone successfully benchmarked a more complex solution already?

As the third party library support for the new PHP version is far from sufficient, I was not yet able to run more complex tests.

This is the hobby lib that's seeing the boost.

GitHub logo dakujem / wire-genie

Autowiring Tool & Dependency Provider.

Wire Genie 🧞

PHP req. Build Status Coverage Status

Autowiring Tool & Dependency Provider for PSR-11 service containers Wire with genie powers.

💿 composer require dakujem/wire-genie

What?

A superpowered call_user_func? Yup! And more.

Wire Genie uses your PSR-11 service container to "magically" provide arguments (dependencies).

Allows you to:

  • invoke any callables
  • construct any objects

... with high level of control over the arguments. 💪

Usage

$container = new Any\Psr11\Container([
    Thing::class => new Thing()
    MyService::class => new MyService()
])
$callable = function (MyService $service, Thing $thing){ ... };

class Something {
    public function __construct(MyService $service, Thing $thing) { ... }
}

$g = new Dakujem\Wire\Genie($container);

// Magic! The dependencies are resolved from the container.
$value  = $g->invoke
Enter fullscreen mode Exit fullscreen mode

And since the attributes can now be used instead of doc-comment annotation parsing (which requires regexp parsing most of the time), the upcoming version will be much faster by design.

Discussion

pic
Editor guide
Collapse
yellow1912 profile image
yellow1912

I don't expect to see much improvement on the frontend side, jit is meant for long running scripts. I think I may get some nice improvement in the cli, perhaps this will make php based server a viable option as well?

Collapse
yoursunny profile image
Junxiao Shi

PHP used to be Personal Homepage Preprocessor and I'll continue to treat it as such. My main website uses PHP but it's only for rendering pages.

I would not make long running scripts out of PHP. It's what Node.js is for.

Collapse
yellow1912 profile image
yellow1912

PHP actually has better type checking, better support for OOP, and similar if not faster performance compared to Node. I use both of them at the same time for their purposes.

Thread Thread
yoursunny profile image
Junxiao Shi

I don't know about PHP8, but PHP7 indeed has good OOP.
As for type checking: you are not supposed to write JavaScript directly. Write TypeScript, compile and run in Node.

Performance depends on use case. Node would perform better on I/O intensive workloads; PHP would perform better if you don't need much I/O.
Neither would beat C++, but you would spend 3x coding time for C++.

Thread Thread
yellow1912 profile image
yellow1912

If you like the performance of C++, try the Swoole extension for PHP, it brings the Go Coroutine syntax and power to PHP :). You can start writing async code with PHP as well and get the same I/O performance you have with Node if not more.

In any case, the point is not to say which language is better, PHP can be used for serious work, it's not just meant for personal toy project anymore. Even if you do not want to give that a try, I hope that you should not spread out-dated information that may discourage new beginners to try it out. Both PHP and Node are fantastic languages and they can live happily together :).

Thread Thread
dakujem profile image
Andrej Rypo Author

How widespread is the hosting/platform support for Swoole? Do you run it in docker, kubernetes or something like that? I have yet to experiment with it. Though i lived the coroutines in Go. Still, Go is a pain in the ass to write. 😆

Thread Thread
yellow1912 profile image
yellow1912

If you can use pecl or run phpize to compile php then you can install swoole. It's no different than install other php extensions IMHO. I think you should have absolutely no issue on Amazon,, Google, etc...

Collapse
_garybell profile image
Gary Bell

Just checked my CI job for a small project (and I mean tiny) but the time to run the unit tests on PHP 8 was cut in half compared to 7.4.

Collapse
dakujem profile image
Andrej Rypo Author

That's what I'm seeing. Maybe Travis deals better with php 8 🙂

Collapse
leob profile image
leob

Interesting ... I'd like to benchmark PHP 8 vs PHP 7 with a Laravel app and then measure how the TPS (transactions/requests per second) is affected, because processing an HTTP request in Laravel involves executing a LOT of PHP code - I can imagine that a faster 'engine' executes all of that code a lot more quickly & efficiently, meaning that more requests can be handled using the same percentage of CPU ...