Skip to content

Commit 0199fa3

Browse files
authored
perf(core): improve overall discovery performance (#1333)
1 parent e9a5a17 commit 0199fa3

17 files changed

+221
-102
lines changed

packages/console/src/Exceptions/ConsoleExceptionHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tempest\Console\Console;
88
use Tempest\Console\ExitCode;
9+
use Tempest\Console\GlobalFlags;
910
use Tempest\Console\HasExitCode;
1011
use Tempest\Console\Input\ConsoleArgumentBag;
1112
use Tempest\Container\Container;
@@ -51,7 +52,7 @@ public function handle(Throwable $throwable): void
5152
->writeln($this->getSnippet($throwable->getFile(), $throwable->getLine()))
5253
->writeln();
5354

54-
if ($this->argumentBag->get('-v') !== null) {
55+
if ($this->argumentBag->get(GlobalFlags::VERBOSE_SHORTHAND->value) || $this->argumentBag->get(GlobalFlags::VERBOSE->value)) {
5556
foreach ($throwable->getTrace() as $i => $trace) {
5657
$this->console->writeln("<style='bold fg-blue'>#{$i}</style> " . $this->formatTrace($trace));
5758
}

packages/console/src/GlobalFlags.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ enum GlobalFlags: string
1010

1111
case FORCE = 'force';
1212
case FORCE_SHORTHAND = '-f';
13+
case VERBOSE = 'verbose';
14+
case VERBOSE_SHORTHAND = '-v';
1315
case HELP = 'help';
1416
case HELP_SHORTHAND = '-h';
1517
case INTERACTION = 'interaction';

packages/core/src/Commands/DiscoveryGenerateCommand.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ public function __invoke(): void
4343
$this->clearDiscoveryCache();
4444

4545
$this->console->task(
46-
label: "Generating discovery cache using the {$strategy->value} strategy",
46+
label: "Generating discovery cache using the `{$strategy->value}` strategy",
4747
handler: fn (Closure $log) => $this->generateDiscoveryCache($strategy, $log),
4848
);
49-
50-
$this->discoveryCache->storeStrategy($strategy);
5149
}
5250

5351
public function clearDiscoveryCache(): void
@@ -68,16 +66,12 @@ public function generateDiscoveryCache(DiscoveryCacheStrategy $strategy, Closure
6866

6967
$discoveries = $loadDiscoveryClasses->build();
7068

71-
foreach ($discoveries as $discovery) {
72-
$log($discovery::class);
73-
$discoveryItems = $discovery->getItems();
74-
75-
if ($strategy === DiscoveryCacheStrategy::PARTIAL) {
76-
$discoveryItems = $discoveryItems->onlyVendor();
77-
}
78-
79-
$this->discoveryCache->store($discovery, $discoveryItems);
69+
foreach ($this->kernel->discoveryLocations as $location) {
70+
$this->discoveryCache->store($location, $discoveries);
71+
$log($location->path);
8072
}
73+
74+
$this->discoveryCache->storeStrategy($strategy);
8175
}
8276

8377
public function resolveKernel(): Kernel

packages/core/src/DiscoveryCache.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1010
use Tempest\Discovery\Discovery;
1111
use Tempest\Discovery\DiscoveryItems;
12+
use Tempest\Discovery\DiscoveryLocation;
1213
use Throwable;
1314

1415
use function Tempest\internal_storage_path;
@@ -32,24 +33,40 @@ public function __construct(
3233
);
3334
}
3435

35-
public function restore(string $className): ?DiscoveryItems
36+
/**
37+
* @return array<class-string<\Tempest\Discovery\Discovery>, DiscoveryItems>
38+
*/
39+
public function restore(DiscoveryLocation $location): ?array
3640
{
3741
if (! $this->enabled) {
3842
return null;
3943
}
4044

4145
return $this->pool
42-
->getItem(str_replace('\\', '_', $className))
46+
->getItem($location->key)
4347
->get();
4448
}
4549

46-
public function store(Discovery $discovery, DiscoveryItems $discoveryItems): void
50+
/**
51+
* @param Discovery[] $discoveries
52+
*/
53+
public function store(DiscoveryLocation $location, array $discoveries): void
4754
{
48-
$key = str_replace('\\', '_', $discovery::class);
55+
$cachedForLocation = [];
56+
57+
foreach ($discoveries as $discovery) {
58+
$items = $discovery->getItems();
59+
60+
if ($this->strategy === DiscoveryCacheStrategy::PARTIAL) {
61+
$items = $items->onlyVendor();
62+
}
63+
64+
$cachedForLocation[$discovery::class] = $items->getForLocation($location);
65+
}
4966

5067
$item = $this->pool
51-
->getItem($key)
52-
->set($discoveryItems);
68+
->getItem($location->key)
69+
->set($cachedForLocation);
5370

5471
$this->pool->save($item);
5572
}

packages/core/src/DiscoveryCacheInitializer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function initialize(Container $container): DiscoveryCache
2222

2323
private function resolveDiscoveryCacheStrategy(bool $isProduction): DiscoveryCacheStrategy
2424
{
25-
if ($this->isDiscoveryGenerateCommand()) {
25+
if ($this->isDiscoveryGenerateCommand() || $this->isDiscoveryClearCommand()) {
2626
return DiscoveryCacheStrategy::NONE;
2727
}
2828

@@ -49,6 +49,17 @@ private function isDiscoveryGenerateCommand(): bool
4949

5050
$command = $_SERVER['argv'][1] ?? null;
5151

52-
return $command === 'dg' || $command === 'discovery:generate';
52+
return $command === 'dg' || $command === 'discovery:generate' || $command === 'd:g';
53+
}
54+
55+
private function isDiscoveryClearCommand(): bool
56+
{
57+
if (PHP_SAPI !== 'cli') {
58+
return false;
59+
}
60+
61+
$command = $_SERVER['argv'][1] ?? null;
62+
63+
return $command === 'dc' || $command === 'discovery:clear' || $command === 'd:c';
5364
}
5465
}

packages/core/src/DiscoveryCacheStrategy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
enum DiscoveryCacheStrategy: string
88
{
9-
case FULL = 'all';
9+
case FULL = 'full';
1010
case PARTIAL = 'partial';
1111
case NONE = 'none';
1212
case INVALID = 'invalid';
@@ -16,7 +16,7 @@ public static function make(mixed $input): self
1616
return match ($input) {
1717
true, 'true', '1', 1, 'all', 'full' => self::FULL,
1818
'partial' => self::PARTIAL,
19-
'invalid' => self::INVALID,
19+
null, 'invalid' => self::INVALID,
2020
default => self::NONE,
2121
};
2222
}

packages/core/src/FrameworkKernel.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Tempest\Core;
66

77
use Dotenv\Dotenv;
8-
use Tempest\Console\Exceptions\ConsoleExceptionHandler;
98
use Tempest\Container\Container;
109
use Tempest\Container\GenericContainer;
1110
use Tempest\Core\Kernel\FinishDeferredTasks;
@@ -15,7 +14,6 @@
1514
use Tempest\Core\Kernel\RegisterEmergencyExceptionHandler;
1615
use Tempest\Core\ShellExecutors\GenericShellExecutor;
1716
use Tempest\EventBus\EventBus;
18-
use Tempest\Router\Exceptions\HttpExceptionHandler;
1917

2018
final class FrameworkKernel implements Kernel
2119
{

0 commit comments

Comments
 (0)