Skip to content

Commit

Permalink
chore: optimize PHP code to be faster
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfs7 committed Oct 19, 2023
1 parent e077b8d commit b76f052
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions core/kernel/persistence/smoothsql/class.Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ public function getClassesResourceIds(array $classIds): array
return [];
}

$inQuery = implode(',', array_fill(0, count($classIds), '?'));
$query = "SELECT subject, object FROM statements WHERE predicate IN (?, ?) AND object IN ($inQuery)";
$query = sprintf(
'SELECT subject, object FROM statements WHERE predicate IN (?, ?) AND object IN (%s)',
implode(',', array_fill(0, count($classIds), '?'))
);

$statement = $this->getPersistence()->query(
$query,
Expand All @@ -174,12 +176,11 @@ public function getClassesResourceIds(array $classIds): array
$results = $statement->fetchAll();
$resourceIds = [];

foreach ($classIds as $classId) {
/**
* @TODO FIXME Better filter it instead of go through all results every time
*/
$resources = array_filter($results, static fn (array $result): bool => $result['object'] === $classId);
$resourceIds[$classId] = array_column($resources, 'subject');
foreach ($results as $result) {
$resourceId = $result['subject'];
$classId = $result['object'];
$resourceIds[$classId] = $resourceIds[$classId] ?? [];
$resourceIds[$classId][] = $resourceId;
}

return $resourceIds;
Expand Down

0 comments on commit b76f052

Please sign in to comment.