Skip to content

Commit c37dabb

Browse files
committed
refactor: adds mapping for lookup optimization
1 parent de089d4 commit c37dabb

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/Providers/Models/DTO/ModelMetadata.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class ModelMetadata extends AbstractDataValueObject
5454
*/
5555
protected array $supportedOptions;
5656

57+
/**
58+
* @var array<string, true> Map of supported capabilities for O(1) lookups.
59+
*/
60+
private array $capabilitiesMap = [];
61+
62+
/**
63+
* @var array<string, SupportedOption> Map of supported options by name for O(1) lookups.
64+
*/
65+
private array $optionsMap = [];
66+
5767
/**
5868
* Constructor.
5969
*
@@ -80,6 +90,16 @@ public function __construct(string $id, string $name, array $supportedCapabiliti
8090
$this->name = $name;
8191
$this->supportedCapabilities = $supportedCapabilities;
8292
$this->supportedOptions = $supportedOptions;
93+
94+
// Build capability map for efficient lookups
95+
foreach ($supportedCapabilities as $capability) {
96+
$this->capabilitiesMap[$capability->value] = true;
97+
}
98+
99+
// Build options map for efficient lookups
100+
foreach ($supportedOptions as $option) {
101+
$this->optionsMap[$option->getName()] = $option;
102+
}
83103
}
84104

85105
/**
@@ -199,22 +219,22 @@ public function toArray(): array
199219
*/
200220
public function meetsRequirements(ModelRequirements $requirements): bool
201221
{
202-
// Check if all required capabilities are supported
222+
// Check if all required capabilities are supported using map lookup
203223
foreach ($requirements->getRequiredCapabilities() as $requiredCapability) {
204-
if (!in_array($requiredCapability, $this->supportedCapabilities, true)) {
224+
if (!isset($this->capabilitiesMap[$requiredCapability->value])) {
205225
return false;
206226
}
207227
}
208228

209229
// Check if all required options are supported with the specified values
210230
foreach ($requirements->getRequiredOptions() as $requiredOption) {
211-
$supportedOption = $this->findSupportedOption($requiredOption->getName());
212-
213-
// If the option is not supported at all, fail
214-
if ($supportedOption === null) {
231+
// Use map lookup instead of linear search
232+
if (!isset($this->optionsMap[$requiredOption->getName()])) {
215233
return false;
216234
}
217235

236+
$supportedOption = $this->optionsMap[$requiredOption->getName()];
237+
218238
// Check if the required value is supported by this option
219239
if (!$supportedOption->isSupportedValue($requiredOption->getValue())) {
220240
return false;
@@ -224,24 +244,6 @@ public function meetsRequirements(ModelRequirements $requirements): bool
224244
return true;
225245
}
226246

227-
/**
228-
* Finds a supported option by name.
229-
*
230-
* @since n.e.x.t
231-
*
232-
* @param string $name The option name to find.
233-
* @return SupportedOption|null The supported option, or null if not found.
234-
*/
235-
private function findSupportedOption(string $name): ?SupportedOption
236-
{
237-
foreach ($this->supportedOptions as $supportedOption) {
238-
if ($supportedOption->getName() === $name) {
239-
return $supportedOption;
240-
}
241-
}
242-
243-
return null;
244-
}
245247

246248
/**
247249
* {@inheritDoc}

0 commit comments

Comments
 (0)