From b5b31ad0c67233bd5284c1f2f73c57f87d733f86 Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Thu, 19 Nov 2026 13:30:38 -0300 Subject: [PATCH 1/2] DatabaseManager::getDrivers() without rebuilding the name list when every driver exists --- src/Database/DatabaseManager.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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), From 9d46061cb445428c16519d31f6626c7e6025e22a Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Thu, 19 Nov 2026 14:42:41 -0300 Subject: [PATCH 2/2] Select::fetchOne() runs a planned limit(1) shape without cloning the select --- src/ORM/Select.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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) {