Skip to content

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

Merged
taylorotwell merged 1 commit intolaravel:5.6from
jarektkaczyk:feature/instance-cache-for-wildcard-listeners
Feb 26, 2018
Merged

[5.6] Event wildcard listeners cache to save on string operations#23299
taylorotwell merged 1 commit intolaravel:5.6from
jarektkaczyk:feature/instance-cache-for-wildcard-listeners

Conversation

@jarektkaczyk
Copy link
Copy Markdown
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.

2 participants