8000 [12.x] perf: support iterables for event discovery paths by calebdw · Pull Request #55699 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] perf: support iterables for event discovery paths #55699

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
May 10, 2025

Conversation

calebdw
Copy link
Contributor
@calebdw calebdw commented May 9, 2025

Hello!

This adds support for passing iterables to discover events. It also improves the performance of the event discovery by allowing multiple directories to be passed to DiscoverEvents::within().

Motivation

We have many modules and want to discover all listeners in a Listeners directory, to do that we have the following:

// bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
    ->withEvents(array_keys(iterator_to_array(
        Finder::create()
            ->directories()
            ->in([base_path('modules')])
            ->path('Listeners'),
    )))
    // ...

However, the problem is that the file system is walked on every request (adding ~30ms to the boot time) and this argument isn't even used when the events are cached---in short this is a wasted operation in production.

This PR will allow for something like the following (which does not walk the file system if the events are cached):

// bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
    ->withEvents(new LazyCollection(
        fn () => Finder::create()
            ->directories()
            ->in([base_path('modules')])
            ->path('Listeners')
    )->keys())
    // ...

Thanks!

@calebdw calebdw force-pushed the calebdw/push-zlwtltklxypn branch 7 times, most recently from 30a132d to 0584d85 Compare May 9, 2025 19:51
@taylorotwell
Copy link
Member

Can you revert all of the styling changes so it's easier to see the real changes.

@taylorotwell taylorotwell marked this pull request as draft May 10, 2025 17:51
@calebdw calebdw force-pushed the calebdw/push-zlwtltklxypn branch from 0584d85 to 07749cf Compare May 10, 2025 18:18
@calebdw calebdw marked this pull request as ready for review May 10, 2025 18:18
@calebdw
Copy link
Contributor Author
calebdw commented May 10, 2025

Yes, reverted

$discovered,
DiscoverEvents::within($directory, $this->eventDiscoveryBasePath())
);
}, []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array_merge_recursive can be totally removed? Does this not break anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because the DiscoverEvents::within() does the same thing with its double foreach loops:

public static function within($listenerPath, $basePath)
{
$listeners = new Collection(static::getListenerEvents(
Finder::create()->files()->in($listenerPath), $basePath
));
$discoveredEvents = [];
foreach ($listeners as $listener => $events) {
foreach ($events as $event) {
if (! isset($discoveredEvents[$event])) {
$discoveredEvents[$event] = [];
}
$discoveredEvents[$event][] = $listener;
}
}
return $discoveredEvents;
}

The Finder::in() method can accept an array and we're able to process all the listeners at once instead of repeated calls for every directory and then having to merge all of the arrays into one large array

@taylorotwell taylorotwell merged commit bcfa7c3 into laravel:12.x May 10, 2025
58 checks passed
@calebdw
Copy link
Contributor Author
calebdw commented May 10, 2025

Thanks!

@calebdw calebdw deleted the calebdw/push-zlwtltklxypn branch May 10, 2025 19:31
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
0