[6.x] Optimize Service Provider registration #30960
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.
As per the discussion in #30952 and according to my tests,
strpos
is considerably faster than calling startsWith for short haystacks. This function is called for every provider on boot.A default laravel installation will load 25 service providers.
I adapted the benchmark to test checking the default laravel packages. Benchmark code and results here: https://gist.github.com/36864/4bc70175ad0bb60b3261d1100f249cf5
Class names are unlikely to be longer than 100 characters, so this change should not have a negative impact.
According to the same benchmarks, a direct substring comparison would also be faster than the current implementation, but slower than
strpos
for stringsSample result, over 1000000 iterations, checking if the provider class name starts with
Illuminate\
: