Skip to content

Commit

Permalink
Performance issues with metadatas (#137)
Browse files Browse the repository at this point in the history
* Cache DH Annotations
* speed
* remove getOwner call

Co-authored-by: a.dmitryuk <a.dmitryuk@movavi.com>
  • Loading branch information
2 people authored and DamienHarper committed Dec 5, 2022
1 parent 3f9e595 commit 1f8b062
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
62 changes: 33 additions & 29 deletions src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,29 @@ private function hydrateWithScheduledCollectionUpdates(Transaction $transaction,
if (null !== $owner && $this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();

if (null !== $mapping) {
/** @var object $entity */
foreach ($collection->getInsertDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->associate(
$owner,
$entity,
$mapping,
);
}
if (null === $mapping) {
continue;
}

/** @var object $entity */
foreach ($collection->getInsertDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->associate(
$owner,
$entity,
$mapping,
);
}
}

/** @var object $entity */
foreach ($collection->getDeleteDiff() as $entity) {
if ($this->provider->isAudited($entity) && $collection->getOwner()) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
/** @var object $entity */
foreach ($collection->getDeleteDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
}
}
Expand All @@ -124,16 +126,18 @@ private function hydrateWithScheduledCollectionDeletions(Transaction $transactio
if (null !== $owner && $this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();

if (null !== $mapping) {
/** @var object $entity */
foreach ($collection->toArray() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
if (null === $mapping) {
continue;
}

/** @var object $entity */
foreach ($collection->toArray() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/Provider/Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ public function isAudited(object|string $entity): bool
$class = DoctrineHelper::getRealClassName($entity);

// is $entity part of audited entities?
if (!\array_key_exists($class, $configuration->getEntities())) {
$entities = $configuration->getEntities();
if (!\array_key_exists($class, $entities)) {
// no => $entity is not audited
return false;
}

$entityOptions = $configuration->getEntities()[$class];
$entityOptions = $entities[$class];

if (isset($entityOptions['enabled'])) {
return (bool) $entityOptions['enabled'];
Expand Down Expand Up @@ -225,14 +226,25 @@ public function setStorageMapper(callable $storageMapper): void
$this->configuration->setStorageMapper($storageMapper);
}

public function loadAnnotations(EntityManagerInterface $entityManager, ?array $entities = null): self
public function loadAnnotations(EntityManagerInterface $entityManager, array $entities): self
{
\assert($this->configuration instanceof Configuration); // helps PHPStan
$ormConfiguration = $entityManager->getConfiguration();
$metadataCache = $ormConfiguration->getMetadataCache();

$annotationLoader = new AnnotationLoader($entityManager);
$this->configuration->setEntities(array_merge(
$entities ?? $this->configuration->getEntities(),
$annotationLoader->load()
));

if (null !== $metadataCache) {
$item = $metadataCache->getItem('__DH_ANNOTATIONS__');
if (!$item->isHit() || !\is_array($annotationEntities = $item->get())) {
$annotationEntities = $annotationLoader->load();
$item->set($annotationEntities);
$metadataCache->save($item);
}
} else {
$annotationEntities = $annotationLoader->load();
}
$this->configuration->setEntities(array_merge($entities, $annotationEntities));

return $this;
}
Expand Down

0 comments on commit 1f8b062

Please sign in to comment.