Skip to content

Commit

Permalink
perf: Optimize Context & ObjectCache
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 17, 2024
1 parent 6ed91b5 commit 31604c2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* perf: Add caching for `TransformerRegistry`.
* perf: Optimize `guessTypeFromVariable`
* perf: Optimize `Context` & `ObjectCache`

## 0.5.14

Expand Down
10 changes: 10 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@
<file name="tests/IntegrationTest/MappingTest.php" />
</errorLevel>
</PossiblyInvalidArgument>
<InvalidReturnStatement>
<errorLevel type="suppress">
<file name="src/Context/Context.php" />
</errorLevel>
</InvalidReturnStatement>
<InvalidReturnType>
<errorLevel type="suppress">
<file name="src/Context/Context.php" />
</errorLevel>
</InvalidReturnType>
</issueHandlers>
</psalm>
13 changes: 2 additions & 11 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@

class CachedTargetObjectNotFoundException extends RuntimeException
{
public function __construct()
{
}
}
22 changes: 2 additions & 20 deletions src/ObjectCache/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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])) {
Expand All @@ -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]);
Expand All @@ -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])) {
Expand All @@ -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]);
Expand All @@ -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 */
Expand Down

0 comments on commit 31604c2

Please sign in to comment.