Performance of v4 vs v3: twice as slow? #713
Replies: 3 comments 3 replies
-
I digged into this and it's coming from laravel-data/src/Normalizers/ModelNormalizer.php Lines 39 to 43 in a5df67e Because it loads all of the attributes declared in the model, even if the data class doesn't need them It was added in v4 e1e491d So if you have any hungry attribute like this that you don't use in the data class public function getSpeakersAttribute(): Arrayable
{
$this->loadMissing(['events.video.speakers', 'events.speakers']);
return $this->events->flatMap(fn (SeminarEvent $event) => $event->speakers)->unique('id')->values();
} It can take up to 20 times more time to load your models into data class I made a PR #730 which fixes this issue |
Beta Was this translation helpful? Give feedback.
-
Good catch! |
Beta Was this translation helpful? Give feedback.
-
Thank you for bringing that up (and tracking down the issue). Our company also upgraded to v4 in a project with huge traffic and our PHP docker containers kept crashing. We never found the issue and reverted the changes. At that time I didn't create an issue here because I just couldn't track down the actual issue. We will give the latest version another shot and I will come back to you to report if this solved the issues or not. |
Beta Was this translation helpful? Give feedback.
-
Hi team,
I want to preface this discussion by saying that we find this package really useful. We use it extensively now for ViewModels, database model casting, validation, typescript generation. It's a huge help.
But in attempting to upgrade to v4 we ran into a few issues. One key issue that makes me reluctant to upgrade is v4 is that v4 seems to be about twice as slow as v3 in our tests.
We have some endpoints with quite complex (and large) data models. I benchmarked one such endpoint on v3 and v4 and these were the results:
laravel-data v3 (with structure caching)
laravel-data v4 (with structure caching)
The results showed that the mean time per request went up significantly post-upgrade. At this kind of performance we will likely have to not use laravel-data for that end-point and opt for raw classes. This is a worry as we may end up with multiple ways of achieving the same thing depending on performance needs. Note: Structure caching didn't seem to make much of a difference to performance, if any.
So I'm wondering: is there any on-going work to improve the performance of the v4 package? Is there anything we can do to help ie. provide xdebug profiles for our endpoint?
Beta Was this translation helpful? Give feedback.
All reactions