Skip to content

Commit

Permalink
Optimize TypeRegistry::lookupName() from O(N) to O(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 15, 2023
1 parent 7844191 commit 92a3c40
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Types/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Doctrine\DBAL\Exception;

use function array_search;
use function in_array;
use function assert;
use function spl_object_id;

/**
* The type registry is responsible for holding a map of all known DBAL types.
Expand All @@ -17,11 +17,14 @@ final class TypeRegistry
{
/** @var array<string, Type> Map of type names and their corresponding flyweight objects. */
private array $instances;
/** @var array<int, string> */
private array $instancesReverseIndex;

/** @param array<string, Type> $instances */
public function __construct(array $instances = [])
{
$this->instances = [];
$this->instances = [];
$this->instancesReverseIndex = [];
foreach ($instances as $name => $type) {
$this->register($name, $type);
}
Expand Down Expand Up @@ -80,7 +83,8 @@ public function register(string $name, Type $type): void
throw Exception::typeAlreadyRegistered($type);
}

$this->instances[$name] = $type;
$this->instances[$name] = $type;
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}

/**
Expand All @@ -94,11 +98,12 @@ public function override(string $name, Type $type): void
throw Exception::typeNotFound($name);
}

if (! in_array($this->findTypeName($type), [$name, null], true)) {
if (($this->findTypeName($type) ?? $name) !== $name) {
throw Exception::typeAlreadyRegistered($type);
}

$this->instances[$name] = $type;
$this->instances[$name] = $type;
$this->instancesReverseIndex[spl_object_id($type)] = $name;
}

/**
Expand All @@ -115,11 +120,8 @@ public function getMap(): array

private function findTypeName(Type $type): ?string
{
$name = array_search($type, $this->instances, true);

if ($name === false) {
return null;
}
$name = $this->instancesReverseIndex[spl_object_id($type)] ?? null;
assert(($this->instances[$name] ?? null) === $type);

return $name;
}
Expand Down

0 comments on commit 92a3c40

Please sign in to comment.