Skip to content

Commit

Permalink
perf(ObjectCache): Optimize using plain arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 17, 2024
1 parent 1aa32bf commit b8f8c3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* perf(`TypeCheck`): Improve `isVariableInstanceOf`.
* perf(`ScalarToScalarTransformer`): Optimize.
* perf: Remove type guesser from `TypeResolver`.
* perf(`ObjectCache`): Optimize using plain arrays.

## 0.5.14

Expand Down
44 changes: 21 additions & 23 deletions src/ObjectCache/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@
final class ObjectCache
{
/**
* @var \SplObjectStorage<object,\ArrayObject<string,object>>
* @var array<int,array<string,object>>
*/
private \SplObjectStorage $cache;
private array $cache = [];

/**
* @var \SplObjectStorage<object,\ArrayObject<string,true>>
* @var array<int,array<string,true>>
*/
private \SplObjectStorage $preCache;
private array $preCache = [];

public function __construct(
private TypeResolverInterface $typeResolver
) {
/** @psalm-suppress MixedPropertyTypeCoercion */
$this->cache = new \SplObjectStorage();
/** @psalm-suppress MixedPropertyTypeCoercion */
$this->preCache = new \SplObjectStorage();
}

private function isBlacklisted(mixed $source): bool
Expand Down Expand Up @@ -67,14 +63,13 @@ public function preCache(mixed $source, Type $targetType, Context $context): voi
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

if (!isset($this->preCache[$source])) {
/** @var \ArrayObject<string,true> */
$arrayObject = new \ArrayObject();
$this->preCache[$source] = $arrayObject;
if (!isset($this->preCache[$key])) {
$this->preCache[$key] = [];
}

$this->preCache->offsetGet($source)->offsetSet($targetTypeString, true);
$this->preCache[$key][$targetTypeString] = true;
}

private function isPreCached(mixed $source, Type $targetType, Context $context): bool
Expand All @@ -84,8 +79,9 @@ private function isPreCached(mixed $source, Type $targetType, Context $context):
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

return isset($this->preCache[$source][$targetTypeString]);
return isset($this->preCache[$key][$targetTypeString]);
}

private function removePrecache(mixed $source, Type $targetType, Context $context): void
Expand All @@ -95,9 +91,10 @@ private function removePrecache(mixed $source, Type $targetType, Context $contex
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

if (isset($this->preCache[$source][$targetTypeString])) {
unset($this->preCache[$source][$targetTypeString]);
if (isset($this->preCache[$key][$targetTypeString])) {
unset($this->preCache[$key][$targetTypeString]);
}
}

Expand All @@ -112,8 +109,9 @@ public function containsTarget(mixed $source, Type $targetType, Context $context
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

return isset($this->cache[$source][$targetTypeString]);
return isset($this->cache[$key][$targetTypeString]);
}

public function getTarget(mixed $source, Type $targetType, Context $context): mixed
Expand All @@ -131,9 +129,10 @@ public function getTarget(mixed $source, Type $targetType, Context $context): mi
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

/** @var object */
return $this->cache[$source][$targetTypeString]
return $this->cache[$key][$targetTypeString]
?? throw new CachedTargetObjectNotFoundException();
}

Expand All @@ -153,6 +152,7 @@ public function saveTarget(
}

$targetTypeString = $this->typeResolver->getTypeString($targetType);
$key = spl_object_id($source);

if (
$addIfAlreadyExists === false
Expand All @@ -165,13 +165,11 @@ public function saveTarget(
));
}

if (!isset($this->cache[$source])) {
/** @var \ArrayObject<string,object> */
$arrayObject = new \ArrayObject();
$this->cache[$source] = $arrayObject;
if (!isset($this->cache[$key])) {
$this->cache[$key] = [];
}

$this->cache->offsetGet($source)->offsetSet($targetTypeString, $target);
$this->cache[$key][$targetTypeString] = $target;
$this->removePrecache($source, $targetType, $context);
}
}

0 comments on commit b8f8c3c

Please sign in to comment.