Skip to content

Commit 775dd33

Browse files
abnegateclaude
andcommitted
perf(relationships): fast-exit hooks when collection has no relationships
processQueries and convertQueries both walked the entire query list before deciding there was nothing to do for a flat-table collection. Short-circuit when \$relationships is empty so the common case of plain reads against non-relational schemas pays nothing for the relationship hook integration. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ef6b8b5 commit 775dd33

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/Database/Hook/Relationships.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ public function processQueries(array $relationships, array $queries): array
916916
{
917917
$nestedSelections = [];
918918

919+
// Fast exit: collections without relationships short-circuit before
920+
// walking the query list. This is the common case for flat tables.
921+
if (empty($relationships)) {
922+
return $nestedSelections;
923+
}
924+
919925
// Pre-index relationships by key once so per-value lookups are O(1)
920926
// instead of O(relationships) with a fresh array_filter each iteration.
921927
/** @var array<string, Document> $relationshipsByKey */
@@ -999,6 +1005,12 @@ public function processQueries(array $relationships, array $queries): array
9991005
*/
10001006
public function convertQueries(array $relationships, array $queries, ?Document $collection = null): ?array
10011007
{
1008+
// Fast exit: nothing to convert when the collection has no
1009+
// relationship attributes — saves the per-find query walk.
1010+
if (empty($relationships)) {
1011+
return $queries;
1012+
}
1013+
10021014
$hasRelationshipQuery = false;
10031015
foreach ($queries as $query) {
10041016
$attr = $query->getAttribute();

0 commit comments

Comments
 (0)