chore: Optimize Filter() for small lists #4282
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 callByRoleName
on each element in a short list.I ran some benchmarks and saw preparing as a user with 0 roles is the fastest at
12803317
ns. Running aByRoleName
is slowest with many roles at968460
ns. This means if you took the bestPrepare
time and worseByRoleName
time, you'd get the performance cutoff at ~13 items in a list (assuming the time afterPrepare()
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