Skip to content

Commit 79f7d1b

Browse files
abnegateclaude
andcommitted
perf(database): hoist invariants out of find post-processing loop
The per-result-row foreach was calling \$collection->getId() and isset(\$this->documentTypes[\$collection->getId()]) up to three times per iteration, even though both values are fixed for the entire find. Compute them once before the loop. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7622f59 commit 79f7d1b

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/Database/Traits/Documents.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,18 +2385,24 @@ public function find(string $collection, array $queries = [], PermissionType $fo
23852385
}
23862386
}
23872387

2388+
// Hoist invariants out of the per-document loop. Collection id and
2389+
// documentType lookup don't change per result row, but were being
2390+
// re-evaluated for every document on every find.
2391+
$collectionId = $collection->getId();
2392+
$hasCustomType = isset($this->documentTypes[$collectionId]);
2393+
23882394
foreach ($results as $index => $node) {
23892395
$node = $this->adapter->castingAfter($collection, $node);
23902396
$node = $this->casting($collection, $node);
23912397
$node = $this->decode($collection, $node, $selections);
23922398

23932399
// Convert to custom document type if mapped
2394-
if (isset($this->documentTypes[$collection->getId()])) {
2395-
$node = $this->createDocumentInstance($collection->getId(), $node->getArrayCopy());
2400+
if ($hasCustomType) {
2401+
$node = $this->createDocumentInstance($collectionId, $node->getArrayCopy());
23962402
}
23972403

23982404
if (! $node->isEmpty()) {
2399-
$node->setAttribute('$collection', $collection->getId());
2405+
$node->setAttribute('$collection', $collectionId);
24002406
}
24012407

24022408
$results[$index] = $node;

0 commit comments

Comments
 (0)