diff --git a/src/Database/DatabaseManager.php b/src/Database/DatabaseManager.php index cd147c5..fba880f 100644 --- a/src/Database/DatabaseManager.php +++ b/src/Database/DatabaseManager.php @@ -39,6 +39,9 @@ final class DatabaseManager implements DatabaseProviderInterface, LoggerAwareInt /** @var array Drivers created from the config, closed with the manager. */ private array $ownDrivers = []; + /** @var list|null names of the configured drivers (the configuration does not change) */ + private ?array $driverNames = null; + private ?LoggerInterface $logger = null; public function __construct( @@ -136,6 +139,21 @@ public function getDrivers(): array { // Names only: building every configured driver here just to read its key made each call // construct throwaway driver objects. + $configured = $this->driverNames ??= $this->config->getDriverNames(); + if (\count($this->drivers) >= \count($configured)) { + // Every configured driver exists already (the usual case after the first request): + // the merge below would return exactly these, in this order. + $complete = true; + foreach ($configured as $name) { + if (!isset($this->drivers[$name])) { + $complete = false; + break; + } + } + if ($complete) { + return \array_values($this->drivers); + } + } $names = \array_unique( \array_merge( \array_keys($this->drivers), diff --git a/src/ORM/Select.php b/src/ORM/Select.php index fc68a33..8a5c7c8 100644 --- a/src/ORM/Select.php +++ b/src/ORM/Select.php @@ -1060,7 +1060,10 @@ public function fetchOne(?array $query = null): ?object { if (!$this->customized && ($query === null || $query === [])) { // Same entity as below through the cached statement and row maker of the shape. - $rows = (clone $this)->limit(1)->fetchShape(); + // Once the shape with `limit(1)` has a plan, it runs without cloning this select. + $rows = isset($this->shapes[$this->shape . "limit\0" . "1\1"]) + ? $this->fetchShape("limit\0" . "1\1") + : (clone $this)->limit(1)->fetchShape(); if ($rows !== null) { return $rows[0] ?? null; } @@ -1214,13 +1217,14 @@ private function applyRecorded(): void * * @return list|null */ - private function fetchShape(): ?array + /** @param string $suffix recorded calls (no values) appended to this shape, already planned */ + private function fetchShape(string $suffix = ''): ?array { if ($this->customized) { return null; } - $plan = $this->shapes[$this->shape] ?? null; - if ($plan === null && \count($this->shapes) < 1024) { + $plan = $this->shapes[$this->shape . $suffix] ?? null; + if ($plan === null && $suffix === '' && \count($this->shapes) < 1024) { $plan = $this->shapes[$this->shape] = $this->shapePlan(); } if (!$plan) {