Skip to content

Commit 6f87c6a

Browse files
committed
Introduce a type lookup cache for faster en/decoding
1 parent df48620 commit 6f87c6a

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Metadata/Collection/TypeCollection.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use IteratorAggregate;
1010
use Soap\Engine\Exception\MetadataException;
1111
use Soap\Engine\Metadata\Model\Type;
12+
use function Psl\Dict\reindex;
1213

1314
/**
1415
* @implements IteratorAggregate<int<0,max>, Type>
@@ -20,12 +21,24 @@ final class TypeCollection implements Countable, IteratorAggregate
2021
*/
2122
private array $types;
2223

24+
/**
25+
* @var array<string, Type>
26+
*/
27+
private array $qualifiedLookup;
28+
2329
/**
2430
* @no-named-arguments
2531
*/
2632
public function __construct(Type ...$types)
2733
{
2834
$this->types = $types;
35+
$this->qualifiedLookup = reindex(
36+
$types,
37+
static fn (Type $type): string => self::createLookupKey(
38+
$type->getXsdType()->getName(),
39+
$type->getXsdType()->getXmlNamespace()
40+
)
41+
);
2942
}
3043

3144
/**
@@ -99,12 +112,16 @@ public function fetchFirstByName(string $name): Type
99112
*/
100113
public function fetchByNameAndXmlNamespace(string $name, string $namespace): Type
101114
{
102-
foreach ($this->types as $type) {
103-
if ($name === $type->getName() && $namespace === $type->getXsdType()->getXmlNamespace()) {
104-
return $type;
105-
}
115+
$lookupKey = self::createLookupKey($name, $namespace);
116+
if (!\array_key_exists($lookupKey, $this->qualifiedLookup)) {
117+
throw MetadataException::typeNotFound($name);
106118
}
107119

108-
throw MetadataException::typeNotFound($name);
120+
return $this->qualifiedLookup[$lookupKey];
121+
}
122+
123+
private static function createLookupKey(string $name, string $namespace): string
124+
{
125+
return $namespace . ':' . $name;
109126
}
110127
}

0 commit comments

Comments
 (0)