Skip to content

Commit

Permalink
Merge pull request #18 from veewee/type-lookup-cache
Browse files Browse the repository at this point in the history
Introduce a type lookup cache for faster en/decoding
  • Loading branch information
veewee authored Jun 13, 2024
2 parents df48620 + cddf586 commit 682d7c8
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Metadata/Collection/TypeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use IteratorAggregate;
use Soap\Engine\Exception\MetadataException;
use Soap\Engine\Metadata\Model\Type;
use function array_key_exists;
use function Psl\Dict\reindex;

/**
* @implements IteratorAggregate<int<0,max>, Type>
Expand All @@ -20,12 +22,24 @@ final class TypeCollection implements Countable, IteratorAggregate
*/
private array $types;

/**
* @var array<string, Type>
*/
private array $qualifiedLookup;

/**
* @no-named-arguments
*/
public function __construct(Type ...$types)
{
$this->types = $types;
$this->qualifiedLookup = reindex(
$types,
static fn (Type $type): string => self::createLookupKey(
$type->getXsdType()->getName(),
$type->getXsdType()->getXmlNamespace()
)
);
}

/**
Expand Down Expand Up @@ -99,12 +113,16 @@ public function fetchFirstByName(string $name): Type
*/
public function fetchByNameAndXmlNamespace(string $name, string $namespace): Type
{
foreach ($this->types as $type) {
if ($name === $type->getName() && $namespace === $type->getXsdType()->getXmlNamespace()) {
return $type;
}
$lookupKey = self::createLookupKey($name, $namespace);
if (!array_key_exists($lookupKey, $this->qualifiedLookup)) {
throw MetadataException::typeNotFound($name);
}

throw MetadataException::typeNotFound($name);
return $this->qualifiedLookup[$lookupKey];
}

private static function createLookupKey(string $name, string $namespace): string
{
return $namespace . ':' . $name;
}
}

0 comments on commit 682d7c8

Please sign in to comment.