Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Optimize Filter() for small lists #4282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
Merged

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Sep 30, 2022

What this does

Calling Prepare for authorization has significant overhead costs. This will especially be hit in all of our unit tests. It's cheaper for small lists to just call ByRoleName on each element in a short list.

I ran some benchmarks and saw preparing as a user with 0 roles is the fastest at 12803317ns. Running a ByRoleName is slowest with many roles at 968460ns. This means if you took the best Prepare time and worse ByRoleName time, you'd get the performance cutoff at ~13 items in a list (assuming the time after Prepare()is negligible).

Putting the cutoff at 10 is a safe magic number such that calls below 10 will always be faster to do without Prepare().

Why do this

If the cost is 12ms in the best case, this time is added to many of our unit tests. Doing this optimization will not only benefit the FE, but also will benefit our test time in the aggregate.

Data used to decide magic number

BenchmarkRBACFilter/PrepareNoRoles-8                  99          12803317 ns/op         2552352 B/op      74203 allocs/op
BenchmarkRBACFilter/PrepareAdmin-8                    93          14968142 ns/op         2411699 B/op      73109 allocs/op
BenchmarkRBACFilter/PrepareOrgAdmin-8                 69          17115370 ns/op         3222047 B/op      93369 allocs/op
BenchmarkRBACFilter/PrepareOrgMember-8                84          17906357 ns/op         3093862 B/op      89507 allocs/op
BenchmarkRBACFilter/PrepareManyRoles-8                56          20082477 ns/op         3707931 B/op     106583 allocs/op
BenchmarkRBACFilter/PrepareAdminWithScope-8           96          13191942 ns/op         2307629 B/op      69411 allocs/op

BenchmarkRBACFilter/OnceNoRoles-8                   4204            304850 ns/op           74356 B/op       1444 allocs/op
BenchmarkRBACFilter/OnceAdmin-8                     1443            697019 ns/op          170803 B/op       3645 allocs/op
BenchmarkRBACFilter/OnceOrgAdmin-8                  1921            677430 ns/op          163995 B/op       3434 allocs/op
BenchmarkRBACFilter/OnceOrgMember-8                 1622            755283 ns/op          179122 B/op       3711 allocs/op
BenchmarkRBACFilter/OnceManyRoles-8                 1130            968460 ns/op          227128 B/op       4902 allocs/op
BenchmarkRBACFilter/OnceAdminWithScope-8            1405            805665 ns/op          167309 B/op       3560 allocs/op

@Emyrk Emyrk requested review from kylecarbs and sreya September 30, 2022 19:47
@Emyrk Emyrk merged commit d8008de into main Sep 30, 2022
@Emyrk Emyrk deleted the stevenmasley/short_lists branch September 30, 2022 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants