Skip to content

Commit

Permalink
perf(TransformerRegistry): Cache TransformerInterface instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 18, 2024
1 parent f06e184 commit 705b44e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* perf: Remove type guesser from `TypeResolver`.
* perf(`ObjectCache`): Optimize using plain arrays.
* perf: Optimize `ObjectCache` & `Context`.
* perf(`TransformerRegistry`): Cache `TransformerInterface` instances.

## 0.5.14

Expand Down
11 changes: 10 additions & 1 deletion src/TransformerRegistry/CachingTransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ public function __construct(
) {
}

/**
* @var array<string,TransformerInterface>
*/
private array $transformers = [];

public function get(string $id): TransformerInterface
{
return $this->decorated->get($id);
if (isset($this->transformers[$id])) {
return $this->transformers[$id];
}

return $this->transformers[$id] = $this->decorated->get($id);
}

public function findBySourceAndTargetTypes(
Expand Down
11 changes: 10 additions & 1 deletion src/TransformerRegistry/TransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ public function __construct(
) {
}

/**
* @var array<string,TransformerInterface>
*/
private array $transformers = [];

public function get(string $id): TransformerInterface
{
if (isset($this->transformers[$id])) {
return $this->transformers[$id];
}

$transformer = $this->transformersLocator->get($id);

if (!$transformer instanceof TransformerInterface) {
Expand All @@ -44,7 +53,7 @@ public function get(string $id): TransformerInterface
));
}

return $transformer;
return $this->transformers[$id] = $transformer;
}

/**
Expand Down

0 comments on commit 705b44e

Please sign in to comment.