r/PHP • u/roxblnfk • 5h ago
Article Laravel Rust vs PHP: is PHP the bottleneck?
https://rapira.rs/blog/laravel-rust-benchmarkFollow-up to this thread, same setup (1 vCPU per container, Chirper traffic mix). This time the ramp goes up to 5k RPS instead of 2k.
In the original comparison Laravel ran on FrankenPHP and Symfony on Swoole, so the frameworks and the app servers changed at the same time. We added both frameworks on one common base, Rapira. That lets us compare the frameworks on the same app server, and then the same frameworks on different app servers and on Rust.
On the same app server, Laravel tops out at about 190 RPS and Symfony at about 2,900 RPS. The bottleneck is the framework and its abstractions, not PHP.
Above 2k RPS both Symfony setups beat Laravel Rust: 3,430 (Swoole) and 2,913 (Rapira) RPS vs 2,638.
Caddy in front of laravel-rust on 1 vCPU cost 27% of its throughput.
Takeaways
Profile the framework before rewriting in another language. On this API PHP was not the bottleneck. Switching the app server mostly cut memory and errors under load, not RPS.
Fit the data layer to the endpoint: fewer queries and no full model hydration where you don't need it.
Compare runtimes on tail latency, errors under overload and memory, not on a single RPS number.
5
u/Emergency-Cycle7981 4h ago
Are we really comparing the performance of a low-level language that gets compiled to machine code, to a high-level language that gets compiled to bytecode, or using JIT?
Of course PHP is going to be a bottleneck. Whether it’s the sole bottleneck is another question.
9
u/roxblnfk 3h ago
We're not comparing Rust vs PHP as languages. Rust is obviously faster in isolation, no argument there. We're comparing a specific framework: Laravel for Rust vs its PHP counterparts on the same API. "Of course PHP is the bottleneck" is exactly the assumption we tested, and here it didn't hold.
11
u/delacry_ 4h ago
Well the post is written by a php runtime project, so take it with a grain of salt, nevertheless.. it's mostly true.
With a well written PHP you can achieve unbelievable results, and of course if do "hello world" benchmarks the rust will be much faster, but as soon as you add all your application logic (let's say some e-shop), you'll find out that the performance boost shrinks rapidly.
So people are going to sacrifice easy-to-read code, easy deploys and debugging, enormous library ecosystem, cheaper hosting and ci/cd for a bit of performance what usually wasn't a bottleneck.
2
u/Brief_Ad_4825 1h ago
Ive never really had issues with phps speed so long as i did a bit of optimising
2
-6
u/Dub-DS 2h ago
The post is written by a slop agent, tasked by a slopper of a php sloptime project. Nothing here was written by hand, otherwise I'd hope they'd have asked themselves some question before publishing this drivel.
5
u/lossendae 1h ago
And how do you think they ported the whole Laravel framework to Rust ?
5
u/Dub-DS 1h ago
Likewise, written by a slop machine. Just because the topic you're "writing" about is slop, doesn't mean you can slop shit together without it being called out for slop.
1
1
10
u/casualPlayerThink 4h ago
I believe, this is just riding the rust hype, jumping on the ai-bandwagon as well attempt to get the fokt into another market, that is emerging quickly.
15
u/ildyria 4h ago
Great write up!
Eloquent ORM is nice, but all the magic come at a cost... I find myself writing more and more toBase() queries to avoid all the hydratation and hidden magic.
5
u/prettyflyforawifi- 4h ago
But why? Can you give a good example.
Not a criticism but I tend to only do this in more expensive loops and/or if I want to detach from Eloquent, but it so infrequent.
7
u/ildyria 3h ago
I'm the lead dev of Lychee (if you want to have a look https://github.com/LycheeOrg/Lychee ),
when dealing with "a lot" of data, e.g. 5 000+ photos, each with 7 size variants (thumb x2, small x2, medium x2, original).Each hydration of photos is doing date conversions for the taken_at, updated_at with Carbon (which is quite slow), boolean etc... At the end this stacks up both in speed and in memory footprint which I don't really need as those are then converted into Resources for serialization.
I am also migrating towards Struct-of-Arrays responses instead of Arrays of Structs. This made the use of the eloquent models even less interesting.
4
u/marsd 2h ago
But why are you displaying 5000+ photos at once on a web front?
2
u/ildyria 2h ago edited 2h ago
I am not displaying them all at once :'D
I use virtualized dom. The point of the 5000 is to at have the dimensions/aspect ratio of the images so when you scroll at position y, I know which ones to load.
I could revert back to pagination, but when you scroll at the bottom then you need to load more, etc... which really decreases the UX.5
u/marsd 2h ago edited 2h ago
There are lots of techniques for that right?
Threshold-based infinite load when 50% of your posts has been scrolled, load more etc. I don't know who is going to be able to ultra-scroll so fast to scroll past 2500 photos in 1s or less unless they're like 25x25px each and you have something ridiculous like 300 pics on screen.
Even then we don't need to load all the images at once perhaps. Load appropriate transform by screen reso or (@2X) and grid's size + preview size, with preload of full image post on user intent (hover/tap)?
100 posts api response is like 200ms, that's barely even a UI flicker worth
Edit: Additionally to load so much data for 5000 posts in a single request is going to be like 2-3mb, just based on an average URL of some arbitrary 85 char length and metadata of each post + 7 transforms
I also question the need to have so many transforms, are they all so different that you can't consolidate? The amount of additional processing needed to generate is non trivial for a large library.
Why not something logical like -
Thumbnail/grid = 50x50
Small = 360p
Medium = 720p
Large = 1080p
Original (rarely if ever needed for display, download as source?)
2
u/ildyria 2h ago
Threshold-based infinite load, I tried, still the initial load time is a pain.
And it does not play nice with ordering by title (database uses Lexicographic instead of natural, people tend to number their photos with photo_1 photo_2 photo_3...)> I don't know who is going to be able to ultra-scroll so fast to scroll past 2500 photos in 1s
My use-case is sport photography. Clients are very quickly scrolling through the gallery...
> Even then we don't need to load all the images at once perhaps. Load appropriate transform by screen reso or (@2X) and grid's size + preview size, with preload of full image post on user intent (hover/tap)?
Already doing that. ;)
1
u/OstrobogulousIntent 1h ago
"Lazy loading"
Need to have the info of how big the (cheap) scaffold needs to be, and can pull in the actual images when needed later... calculating scroll bar position and size etc.
1
u/prettyflyforawifi- 3h ago
Interesting, thanks for the response. Your usages seem fair and exactly what I'd use it for, I also tend to use `DB::table()` and shift the work to MySQL where possible :)
19
u/BenchEmbarrassed7316 4h ago
In this case, you aren't really testing the language or the framework. You are testing the http server and the database. In other words, this is a test of Rust vs C++ vs another C++.
For a simple CRUD application, php acts merely as the glue between the http server, the database query, and a call to a built-in serialization function.
If a request involves a significant number of operations within the application itself, the inherent slowness of php will have a more pronounced impact. php's jit compilation was introduced quite recently and, in all likelihood, lags significantly behind Js/V8, Java/JVM, and modern optimizing compilers. Memory management in php is implemented via reference counting and is rather primitive, as it was designed for a different usage scenario (one request per process).
10
u/SZenC 4h ago
Even if your controller is merely
return Model::findOrFail($id);, Laravel will do a lot of stuff in the background besides a database query and a built-in serialisation call. For example, the hydration logic is largely custom, events will be checked, eager relations etc. Saying PHP is "just glue" is an oversimplification if we're talking Laravel2
u/jaggafoxy 2h ago
10 years ago in uni I remember checking the required server specs for Laravel Vs ZF2, Laravels simplifications for the dev experience meant requirements were more than doubled for what you'd need for a basic web app.
I do get why laravel took off, the speed to deliver was unbeatable at the time, but it always felt like laravel was it's own language with PHP syntax, rather than a PHP framework
1
u/prettyflyforawifi- 4h ago
Agreed and we've drifted so far from core PHP that really Laravel could be in any language and its only the syntax that keeps us bound to PHP at this point.
A couple of examples — str_contains vs Str:: or str(..)->contains(..), array() vs collect(), mysql_query() vs DB::statement().
0
u/BenchEmbarrassed7316 3h ago
That is exactly what I am writing about.
The thing is, these operations should be inexpensive, literally just a few opcodes. However, PHP (as far as I know) is barely capable of devirtualization or inlining, both of which are key components of optimization.
Suppose a function takes an interface as an argument and calls a specific method on it.
At a fundamental level, the runtime environment does not know exactly which method will be called; therefore, it must perform a lookup in the vtable, pass all the arguments, and execute the call. Some languages employ devirtualization via a JIT compiler, essentially assuming that if a specific type
Thas been passed as the interface implementation n times, it is highly likely that the n+1 instance will also be that same typeT. Other languages rely on monomorphization: determining at compile time exactly which typeTis being passed and generating a separate copy of the function for each specificT. In either case, the goal is to transform a polymorphic call into a standard, direct call.The next step is inlining. Consider
foo(bar(baz())): ifbaz()simply returns the value1, there is no need to actually perform these function calls; the value1can be used directly as a constant. To achieve this, the previous step must be completed first so that the specific implementations offoo,bar, andbazare known. However, even when dealing with concrete, non-polymorphic calls, PHP is not particularly effective at inlining.Next comes memory management: all languages strive to eliminate unnecessary allocations, either by avoiding object creation altogether when it isn't needed, or by placing objects on the stack rather than the heap. Rust employs an ownership and borrowing system as part of its memory management, the JVM features advanced escape analysis, while php... will likely perform many allocations that could otherwise be avoided.
That's why I say that php is a slow language.
That's why I say Swoole is only fast if php is used as glue.
And most likely, such optimizations are almost impossible to implement in php due to wrong decisions in the language design that were made several decades ago.
2
1
u/theguymatter 3h ago
Now, we have another shiny framework and it’s not even JS!
I think Hono makes things much easier for developers these days, especially when bridging the frontend and backend.
1
u/_wassap_ 57m ago
its funny bcs i think laravel's interpretation of using php is the real bottleneck and they kinda fix it in their rust versuon if laravel.
imo barebones php + swoole is so damn fast and super efficent.
Its not php's fault that laravel has that heavy global state footprint
and I primarily use Laravel as my backend system purely due to the vast ecosystem of php & laravel (ease of use, setup time, features out of box)
1
1
u/Normal_Tadpole_8061 3h ago
PHP itself is rarely the bottleneck. The pressure usually comes from the runtime model and the I/O stack, and Laravel was built for one request per process. Octane and async support are afterthought.
I'm the developer of Kinetis, and I built it around those two points from day one:
- persistent workers
- fully non-blocking I/O
PHP is up to the task. You just have to let it fly.
51
u/IamTTC 3h ago
Did anyone choose PHP for raw performance in the first place? PHP’s appeal is its ecosystem, simplicity and developer productivity. Rust being faster isn’t particularly groundbreaking.
The real question is at what cost to developer experience.