diff --git a/CHANGELOG.md b/CHANGELOG.md index ba1100e..f9b86a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * perf(`ScalarToScalarTransformer`): Optimize. * perf: Remove type guesser from `TypeResolver`. * perf(`ObjectCache`): Optimize using plain arrays. +* perf: Optimize `ObjectCache` & `Context`. ## 0.5.14 diff --git a/src/Context/Context.php b/src/Context/Context.php index 3311b26..b2f8721 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -93,6 +93,6 @@ public function without(object|string $value): self public function get(string $class): object { // @phpstan-ignore-next-line - return $this->context[$class] ?? throw new ContextMemberNotFoundException($class); + return $this->context[$class] ?? throw new ContextMemberNotFoundException(); } } diff --git a/src/Context/ContextMemberNotFoundException.php b/src/Context/ContextMemberNotFoundException.php index 164d5e6..1fa451a 100644 --- a/src/Context/ContextMemberNotFoundException.php +++ b/src/Context/ContextMemberNotFoundException.php @@ -17,11 +17,4 @@ class ContextMemberNotFoundException extends RuntimeException { - /** - * @param class-string $class - */ - public function __construct(string $class) - { - parent::__construct(sprintf('Cannot find object with class "%s" in context', $class)); - } } diff --git a/src/ObjectCache/ObjectCache.php b/src/ObjectCache/ObjectCache.php index a01acd2..e86fe1e 100644 --- a/src/ObjectCache/ObjectCache.php +++ b/src/ObjectCache/ObjectCache.php @@ -84,20 +84,6 @@ private function isPreCached(mixed $source, Type $targetType, Context $context): return isset($this->preCache[$key][$targetTypeString]); } - private function removePrecache(mixed $source, Type $targetType, Context $context): void - { - if (!is_object($source)) { - return; - } - - $targetTypeString = $this->typeResolver->getTypeString($targetType); - $key = spl_object_id($source); - - if (isset($this->preCache[$key][$targetTypeString])) { - unset($this->preCache[$key][$targetTypeString]); - } - } - public function containsTarget(mixed $source, Type $targetType, Context $context): bool { if (!is_object($source)) { @@ -156,7 +142,7 @@ public function saveTarget( if ( $addIfAlreadyExists === false - && $this->containsTarget($source, $targetType, $context) + && isset($this->cache[$key][$targetTypeString]) ) { throw new LogicException(sprintf( 'Target object for source object "%s" and target type "%s" already exists', @@ -170,6 +156,11 @@ public function saveTarget( } $this->cache[$key][$targetTypeString] = $target; - $this->removePrecache($source, $targetType, $context); + + // remove precache + + if (isset($this->preCache[$key][$targetTypeString])) { + unset($this->preCache[$key][$targetTypeString]); + } } }