Skip to content

Commit

Permalink
perf: Change 'simpletypes' cache to array.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 17, 2024
1 parent 31604c2 commit 9f85c41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* perf: Add caching for `TransformerRegistry`.
* perf: Optimize `guessTypeFromVariable`
* perf: Optimize `Context` & `ObjectCache`
* perf: Change 'simpletypes' cache to array.

## 0.5.14

Expand Down
13 changes: 6 additions & 7 deletions src/TypeResolver/CachingTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public function __construct(
/** @psalm-suppress PropertyTypeCoercion */
$this->typeStringCache = new \WeakMap();

/** @psalm-suppress PropertyTypeCoercion */
$this->simpleTypesCache = new \WeakMap();

/** @psalm-suppress PropertyTypeCoercion */
$this->isSimpleTypeCache = new \WeakMap();
}
Expand Down Expand Up @@ -60,23 +57,25 @@ public function getTypeString(Type|MixedType $type): string
// can be expensive in a loop. we cache using a weakmap

/**
* @var \WeakMap<Type,array<array-key,MixedType|Type>>
* @var array<string,array<array-key,MixedType|Type>>
*/
private \WeakMap $simpleTypesCache;
private array $simpleTypesCache = [];

public function getSimpleTypes(Type|MixedType $type): array
{
if ($type instanceof MixedType) {
return [$type];
}

if ($result = $this->simpleTypesCache[$type] ?? null) {
$key = md5(serialize($type));

if ($result = $this->simpleTypesCache[$key] ?? null) {
return $result;
}

$simpleTypes = $this->decorated->getSimpleTypes($type);

$this->simpleTypesCache->offsetSet($type, $simpleTypes);
$this->simpleTypesCache[$key] = $simpleTypes;

return $simpleTypes;
}
Expand Down

0 comments on commit 9f85c41

Please sign in to comment.