Skip to content

Commit 41b1dc7

Browse files
authored
Merge pull request #5 from dimadimx/feature/get_by_ids_feedback
performance feedback
2 parents 303f38d + e2b648b commit 41b1dc7

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/Skeleton/Object/Get.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ public function get_classname() {
4444
* @return array $obecjts
4545
*/
4646
public static function get_by_ids($ids) {
47-
$result = [];
48-
4947
// Preserve the order of the ids
50-
foreach ($ids as $id) {
51-
$result[$id] = null;
52-
}
48+
$result = array_fill_keys($ids, null);
5349

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

58+
// we use it to avoid repeatedly iterating if some cache is missing
59+
$cached_objects_map = [];
60+
6261
foreach ($cached_objects as $cached_object) {
62+
$cached_objects_map[$cached_object->id] = $cached_object;
6363
unset($cache_keys[$cached_object->id]);
6464
}
6565

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

7070
foreach ($ids as $id) {
71-
foreach ($cached_objects as $cached_object) {
72-
if ($cached_object->id === $id) {
73-
$result[$id] = $cached_object;
74-
}
71+
if (isset($cached_objects_map[$id]) === false) {
72+
continue;
7573
}
74+
$result[$id] = $cached_objects_map[$id];
7675
}
7776
}
7877

@@ -81,7 +80,7 @@ public static function get_by_ids($ids) {
8180
if ($value !== null) {
8281
continue;
8382
}
84-
$return[$id] = self::get_by_id($id);
83+
$result[$id] = self::get_by_id($id);
8584
}
8685

8786
return $result;

0 commit comments

Comments
 (0)