From 31604c2348ab0327031a1c6b0440d5d7c47472f1 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:13:37 +0700 Subject: [PATCH] perf: Optimize `Context` & `ObjectCache` --- CHANGELOG.md | 1 + psalm.xml | 10 +++++++++ src/Context/Context.php | 13 ++--------- .../CachedTargetObjectNotFoundException.php | 3 +++ src/ObjectCache/ObjectCache.php | 22 ++----------------- 5 files changed, 18 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 256959e..803dde9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * perf: Add caching for `TransformerRegistry`. * perf: Optimize `guessTypeFromVariable` +* perf: Optimize `Context` & `ObjectCache` ## 0.5.14 diff --git a/psalm.xml b/psalm.xml index c052189..c297242 100644 --- a/psalm.xml +++ b/psalm.xml @@ -36,5 +36,15 @@ + + + + + + + + + + diff --git a/src/Context/Context.php b/src/Context/Context.php index 8a968c0..3311b26 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -92,16 +92,7 @@ public function without(object|string $value): self */ public function get(string $class): object { - if (!isset($this->context[$class])) { - throw new ContextMemberNotFoundException($class); - } - - $result = $this->context[$class]; - - if (!is_a($result, $class)) { - throw new LogicException(sprintf('Object found, but not the requested type "%s".', $class)); - } - - return $result; + // @phpstan-ignore-next-line + return $this->context[$class] ?? throw new ContextMemberNotFoundException($class); } } diff --git a/src/ObjectCache/Exception/CachedTargetObjectNotFoundException.php b/src/ObjectCache/Exception/CachedTargetObjectNotFoundException.php index d45e825..a25f697 100644 --- a/src/ObjectCache/Exception/CachedTargetObjectNotFoundException.php +++ b/src/ObjectCache/Exception/CachedTargetObjectNotFoundException.php @@ -17,4 +17,7 @@ class CachedTargetObjectNotFoundException extends RuntimeException { + public function __construct() + { + } } diff --git a/src/ObjectCache/ObjectCache.php b/src/ObjectCache/ObjectCache.php index 19957f7..b601f9e 100644 --- a/src/ObjectCache/ObjectCache.php +++ b/src/ObjectCache/ObjectCache.php @@ -17,7 +17,6 @@ use Rekalogika\Mapper\Exception\LogicException; use Rekalogika\Mapper\ObjectCache\Exception\CachedTargetObjectNotFoundException; use Rekalogika\Mapper\ObjectCache\Exception\CircularReferenceException; -use Rekalogika\Mapper\ObjectCache\Exception\NonSimpleTypeException; use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; use Symfony\Component\PropertyInfo\Type; @@ -47,13 +46,6 @@ private function isBlacklisted(mixed $source): bool return $source instanceof \DateTimeInterface; } - private function assertSimpleType(Type $type, Context $context): void - { - if (!$this->typeResolver->isSimpleType($type)) { - throw new NonSimpleTypeException($type, context: $context); - } - } - /** * Precaching indicates we want to cache the target, but haven't done so * yet. If the object is still in precached status, obtaining it from the @@ -74,8 +66,6 @@ public function preCache(mixed $source, Type $targetType, Context $context): voi return; } - $this->assertSimpleType($targetType, $context); - $targetTypeString = $this->typeResolver->getTypeString($targetType); if (!isset($this->preCache[$source])) { @@ -93,8 +83,6 @@ private function isPreCached(mixed $source, Type $targetType, Context $context): return false; } - $this->assertSimpleType($targetType, $context); - $targetTypeString = $this->typeResolver->getTypeString($targetType); return isset($this->preCache[$source][$targetTypeString]); @@ -106,8 +94,6 @@ private function removePrecache(mixed $source, Type $targetType, Context $contex return; } - $this->assertSimpleType($targetType, $context); - $targetTypeString = $this->typeResolver->getTypeString($targetType); if (isset($this->preCache[$source][$targetTypeString])) { @@ -125,8 +111,6 @@ public function containsTarget(mixed $source, Type $targetType, Context $context return false; } - $this->assertSimpleType($targetType, $context); - $targetTypeString = $this->typeResolver->getTypeString($targetType); return isset($this->cache[$source][$targetTypeString]); @@ -139,15 +123,13 @@ public function getTarget(mixed $source, Type $targetType, Context $context): mi } if ($this->isBlacklisted($source)) { - throw new CachedTargetObjectNotFoundException(context: $context); + throw new CachedTargetObjectNotFoundException(); } if (!is_object($source)) { - throw new CachedTargetObjectNotFoundException(context: $context); + throw new CachedTargetObjectNotFoundException(); } - $this->assertSimpleType($targetType, $context); - $targetTypeString = $this->typeResolver->getTypeString($targetType); /** @var object */