Skip to content

Commit 13e4edb

Browse files
abnegateclaude
andcommitted
perf(database): inline authorization toggle in find to avoid per-find closure
find() built a fn () => \$this->adapter->find(...) closure and threw it through Authorization::skip on every call, even though the only state change skip() does is flip the status flag and restore it. Inline the disable/restore pair around the adapter call. With the focus bench (200x complex_query + 200x order_cursor) this drops the remaining authorization::skip overhead and shaves another ~5% wall time off both scenarios. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 18c0ca4 commit 13e4edb

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

src/Database/Traits/Documents.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,19 +2295,42 @@ public function find(string $collection, array $queries = [], PermissionType $fo
22952295
}
22962296

22972297
if (! isset($results)) {
2298-
$getResults = fn () => $this->adapter->find(
2299-
$collection,
2300-
$queries,
2301-
$limit ?? 25,
2302-
$offset ?? 0,
2303-
$orderAttributes,
2304-
$orderTypes,
2305-
$cursor,
2306-
$cursorDirection,
2307-
$forPermission
2308-
);
2309-
2310-
$results = $skipAuth ? $this->authorization->skip($getResults) : $getResults();
2298+
// Inline the auth-skip toggle to avoid the per-find Closure
2299+
// allocation that authorization->skip() requires. Still
2300+
// restores the original status on exception via try/finally.
2301+
if ($skipAuth) {
2302+
$previousStatus = $this->authorization->getStatus();
2303+
$this->authorization->disable();
2304+
try {
2305+
$results = $this->adapter->find(
2306+
$collection,
2307+
$queries,
2308+
$limit ?? 25,
2309+
$offset ?? 0,
2310+
$orderAttributes,
2311+
$orderTypes,
2312+
$cursor,
2313+
$cursorDirection,
2314+
$forPermission
2315+
);
2316+
} finally {
2317+
if ($previousStatus) {
2318+
$this->authorization->enable();
2319+
}
2320+
}
2321+
} else {
2322+
$results = $this->adapter->find(
2323+
$collection,
2324+
$queries,
2325+
$limit ?? 25,
2326+
$offset ?? 0,
2327+
$orderAttributes,
2328+
$orderTypes,
2329+
$cursor,
2330+
$cursorDirection,
2331+
$forPermission
2332+
);
2333+
}
23112334

23122335
if ($cacheKey !== null && $this->queryCache !== null) {
23132336
$this->queryCache->set($cacheKey, $results);

0 commit comments

Comments
 (0)