Performance optimization for laravel-permission projects

Source: Internet
Author: User
This article mainly introduced about the Laravel-permission project performance optimization, has a certain reference value, now share to everyone, the need for friends can refer to

I recently researched the performance of the projects created above Swis. Surprisingly, one of the most cost-performance methods is the excellent spatie/laravel-permission package.

After more information and research, we found a performance problem that could be significantly improved. Now that the solution is clearly articulated, it is easy to write code improvements and submit requests.

Now that the solution has been consolidated and released, here is an analysis of this performance problem and how to avoid such problems in your own project.

TL;DR: Jump to the conclusion section.

Performance bottlenecks

If we look at it abstractly, spatie/laravel-permission it mainly does two things:

    1. Keep a list of permissions that belong to a model.

    2. Checks whether a model has permissions.

The 1th point is that performance bottlenecks are a bit farfetched. The permission data is stored in the database and will be read when needed. This process is a bit slow but also only executes once. The results are cached and subsequent requests can be used directly.

The 2nd is indeed a bottleneck in the view of performance bottlenecks. This bottleneck depends on the nature of the permissions and the size of the project, because permissions are frequently checked. Any slowness in this check will become a performance bottleneck for the entire project.

Filtering collection Classes

The method of filtering permission sets is considered to be the cause of low performance. It does the following things:

$permission = $permissions    ->where (' id ', $id)    ->where (' Guard_name ', $guardName)    ->first ();

After modification:

$permission = $permissions    ->filter (function ($permission) use ($id, $guardName) {        return $permission id = = $id && $permission->guard_name = = = $guardName;    })    ->first ();

Both code snippets implement the same thing, but the second one is faster.

Performance testing

There are about 150 different permissions in the app I'm developing. In a normal request, there are about 50 permissions that need to be hasPermissionTo checked in this way, and of course some pages might need to check about 200 permissions.

Here are some of the settings used to do performance testing.

$users = Factory (User::class,->make), $searchForTheseUsers = $users->shuffle ()->take (50); # method 1: Whereforeach ($searchForTheseUsers as $user) {    $result = $users->where (' id ', ' = ', $user->id)->first ();} # Method 2: Filter, pass a model as callback foreach ($searchForTheseUsers as $searchUser) {    $result = $users->filter (function ($user) use ($searchUser) {        return $user->id = = = $searchUser->id;    }) ->first ();} # Method 3: Filter, pass property as callback foreach ($searchForTheseUsers as $user) {    $searchId = $user->id;    $result = $users->filter (function ($user) use ($searchId) {        return $user->id = = = = $searchId;    }) ->first ();}

The above three methods will be used to test filter 1 properties, 2 properties, 3 properties, so, using method 1 to filter three properties will be:

foreach ($searchForTheseUsers as $user) {    $result = $users        ->where (' id ', ' = ', $user->id)        ->where ( ' FirstName ', ' = ', $user->firstname)        ->where (' lastname ', ' = ', $user->lastname)->first ();}

Results


Method #1 Method #2 Method #3
1 Properties 0.190 0.139 (-27%) 0.072 (-62%)
2 Properties 0.499 0.372 (-25%) 0.196 (-61%)
3 Properties 0.488 0.603 (+25%) 0.198 (-59%)

Conclusion

We can conclude that for a project, repeated filtering of a large set can lead to severe performance bottlenecks.

Multi-attribute filtering significantly increases the computational cost.

Using Collection::filter() substitution Collection::where() can improve performance by 60%.

Warning: Passing a full model to a filter callback is expensive, preferably passing a separate property.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.