Skip to content

Commit 82a579a

Browse files
committed
Replace in_array() with isset() for nested groups in GroupsExclusionStrategy
When nestedGroups is enabled, shouldSkipProperty() used in_array() O(n) to check group membership. Build a hashmap from getGroupsFor() result and use isset() O(1) instead.
1 parent adf5ad2 commit 82a579a

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/Exclusion/GroupsExclusionStrategy.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,29 @@ public function shouldSkipProperty(PropertyMetadata $property, Context $navigato
5454
if ($this->nestedGroups) {
5555
$groups = $this->getGroupsFor($navigatorContext);
5656

57+
$groupsMap = [];
58+
foreach ($groups as $k => $v) {
59+
if (is_string($k)) {
60+
// nested group entry (key = property name, value = sub-groups)
61+
continue;
62+
}
63+
64+
if (is_string($v)) {
65+
$groupsMap[$v] = true;
66+
}
67+
}
68+
5769
if (!$property->groups) {
58-
return !in_array(self::DEFAULT_GROUP, $groups);
70+
return !isset($groupsMap[self::DEFAULT_GROUP]);
5971
}
6072

61-
return $this->shouldSkipUsingGroups($property, $groups);
73+
foreach ($property->groups as $group) {
74+
if (isset($groupsMap[$group])) {
75+
return false;
76+
}
77+
}
78+
79+
return true;
6280
} else {
6381
if (!$property->groups) {
6482
return !isset($this->groups[self::DEFAULT_GROUP]);
@@ -74,17 +92,6 @@ public function shouldSkipProperty(PropertyMetadata $property, Context $navigato
7492
}
7593
}
7694

77-
private function shouldSkipUsingGroups(PropertyMetadata $property, array $groups): bool
78-
{
79-
foreach ($property->groups as $group) {
80-
if (in_array($group, $groups)) {
81-
return false;
82-
}
83-
}
84-
85-
return true;
86-
}
87-
8895
public function getGroupsFor(Context $navigatorContext): array
8996
{
9097
if (!$this->nestedGroups) {

0 commit comments

Comments
 (0)