Skip to content
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

[5.6] Event wildcard listeners cache to save on string operations #23299

Conversation

jarektkaczyk
Copy link
Contributor

Hi!

Str::is is relatively expensive operation, which significantly slows down request, when goes in the number of dozens of thousand calls. Unfortunately Dispatcher::getWildcardListeners method calls it arbitrary number of times (depends on how many wildcard listeners of any kind you registered in your app) for every single event being fired.

It may not be significant for ordinary webpage, but it is worth the effort in the high volume & data heavy API, where simple Database\Connection::logQuery (without any listener registered, ie. without any action taken) may cause Str::is to be called thousands of times, thus slowing down API request by 5-10% in my example.

This PR introduces instance based cache, which does the following:

  1. remembers all wildcard listeners for a fired event after first getWildcardListeners call
  2. from now on uses the cache instead of calling aforementioned string operations again
  3. flushes the cache whenever a new wildcard listener is being registered

Below excerpt from blackfire showing the difference in example call.

after 84ms:
image

before 364ms:
image

…sive Str::is calls, which usually goes in dozens of thousands
@taylorotwell taylorotwell merged commit c86efa6 into laravel:5.6 Feb 26, 2018
@jarektkaczyk jarektkaczyk deleted the feature/instance-cache-for-wildcard-listeners branch July 12, 2018 08:24
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.

None yet

2 participants