Skip to content

Commit

Permalink
perf: Optimize ObjectCache & Context.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 18, 2024
1 parent 705b44e commit 44395d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
23 changes: 12 additions & 11 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ private function __construct(
) {
}

/**
* @template T of object
* @param class-string<T> $class
* @return T
*/
public function __invoke(string $class): object
{
return $this->get($class);
}

/**
* @param array<int,object> $context
* @return self
Expand All @@ -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);
}

Expand Down Expand Up @@ -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<T> $class
* @return T
*/
public function __invoke(string $class): object
{
// @phpstan-ignore-next-line
return $this->context[$class] ?? throw new ContextMemberNotFoundException();
}
}
30 changes: 10 additions & 20 deletions src/ObjectCache/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -102,21 +90,23 @@ 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();
}

$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();
Expand Down

0 comments on commit 44395d6

Please sign in to comment.