From 44395d6faaafad410e38e022602ac611781f8bf5 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Thu, 18 Jan 2024 13:04:05 +0700 Subject: [PATCH] perf: Optimize `ObjectCache` & `Context`. --- src/Context/Context.php | 23 ++++++++++++----------- src/ObjectCache/ObjectCache.php | 30 ++++++++++-------------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/Context/Context.php b/src/Context/Context.php index b2f8721..747453f 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -28,16 +28,6 @@ private function __construct( ) { } - /** - * @template T of object - * @param class-string $class - * @return T - */ - public function __invoke(string $class): object - { - return $this->get($class); - } - /** * @param array $context * @return self @@ -46,7 +36,7 @@ public static function create(array $context = []): self { $self = new self(); - foreach($context as $value) { + foreach ($context as $value) { $self = $self->with($value); } @@ -95,4 +85,15 @@ public function get(string $class): object // @phpstan-ignore-next-line return $this->context[$class] ?? throw new ContextMemberNotFoundException(); } + + /** + * @template T of object + * @param class-string $class + * @return T + */ + public function __invoke(string $class): object + { + // @phpstan-ignore-next-line + return $this->context[$class] ?? throw new ContextMemberNotFoundException(); + } } diff --git a/src/ObjectCache/ObjectCache.php b/src/ObjectCache/ObjectCache.php index e86fe1e..51242fa 100644 --- a/src/ObjectCache/ObjectCache.php +++ b/src/ObjectCache/ObjectCache.php @@ -72,18 +72,6 @@ public function preCache(mixed $source, Type $targetType, Context $context): voi $this->preCache[$key][$targetTypeString] = true; } - private function isPreCached(mixed $source, Type $targetType, Context $context): bool - { - if (!is_object($source)) { - return false; - } - - $targetTypeString = $this->typeResolver->getTypeString($targetType); - $key = spl_object_id($source); - - return isset($this->preCache[$key][$targetTypeString]); - } - public function containsTarget(mixed $source, Type $targetType, Context $context): bool { if (!is_object($source)) { @@ -102,14 +90,6 @@ public function containsTarget(mixed $source, Type $targetType, Context $context public function getTarget(mixed $source, Type $targetType, Context $context): mixed { - if ($this->isPreCached($source, $targetType, $context)) { - throw new CircularReferenceException($source, $targetType, context: $context); - } - - if ($this->isBlacklisted($source)) { - throw new CachedTargetObjectNotFoundException(); - } - if (!is_object($source)) { throw new CachedTargetObjectNotFoundException(); } @@ -117,6 +97,16 @@ public function getTarget(mixed $source, Type $targetType, Context $context): mi $targetTypeString = $this->typeResolver->getTypeString($targetType); $key = spl_object_id($source); + // check if precached + + if (isset($this->preCache[$key][$targetTypeString])) { + throw new CircularReferenceException($source, $targetType, context: $context); + } + + if ($this->isBlacklisted($source)) { + throw new CachedTargetObjectNotFoundException(); + } + /** @var object */ return $this->cache[$key][$targetTypeString] ?? throw new CachedTargetObjectNotFoundException();