Skip to content

Commit

Permalink
Merge pull request #5 from dimadimx/feature/get_by_ids_feedback
Browse files Browse the repository at this point in the history
performance feedback
  • Loading branch information
christopheg authored Dec 24, 2024
2 parents 303f38d + e2b648b commit 41b1dc7
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/Skeleton/Object/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ public function get_classname() {
* @return array $obecjts
*/
public static function get_by_ids($ids) {
$result = [];

// Preserve the order of the ids
foreach ($ids as $id) {
$result[$id] = null;
}
$result = array_fill_keys($ids, null);

if (get_called_class()::trait_cache_enabled()) {
$prefix = get_called_class()::trait_get_cache_prefix();
Expand All @@ -59,7 +55,11 @@ public static function get_by_ids($ids) {
}
$cached_objects = get_called_class()::cache_multi_get($cache_keys);

// we use it to avoid repeatedly iterating if some cache is missing
$cached_objects_map = [];

foreach ($cached_objects as $cached_object) {
$cached_objects_map[$cached_object->id] = $cached_object;
unset($cache_keys[$cached_object->id]);
}

Expand All @@ -68,11 +68,10 @@ public static function get_by_ids($ids) {
}

foreach ($ids as $id) {
foreach ($cached_objects as $cached_object) {
if ($cached_object->id === $id) {
$result[$id] = $cached_object;
}
if (isset($cached_objects_map[$id]) === false) {
continue;
}
$result[$id] = $cached_objects_map[$id];
}
}

Expand All @@ -81,7 +80,7 @@ public static function get_by_ids($ids) {
if ($value !== null) {
continue;
}
$return[$id] = self::get_by_id($id);
$result[$id] = self::get_by_id($id);
}

return $result;
Expand Down

0 comments on commit 41b1dc7

Please sign in to comment.