From 1f8b06285b6d47463f495a1d345dec7d47b1d20d Mon Sep 17 00:00:00 2001 From: Alexander Dmitryuk Date: Sun, 4 Dec 2022 17:04:56 +0100 Subject: [PATCH] Performance issues with metadatas (#137) * Cache DH Annotations * speed * remove getOwner call Co-authored-by: a.dmitryuk --- .../Transaction/TransactionHydrator.php | 62 ++++++++++--------- src/Provider/Doctrine/DoctrineProvider.php | 26 +++++--- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php b/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php index 230b7f0c..fa09fd66 100644 --- a/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php +++ b/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php @@ -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, + ); } } } @@ -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, + ); } } } diff --git a/src/Provider/Doctrine/DoctrineProvider.php b/src/Provider/Doctrine/DoctrineProvider.php index 8caa3ebd..c1b78b77 100644 --- a/src/Provider/Doctrine/DoctrineProvider.php +++ b/src/Provider/Doctrine/DoctrineProvider.php @@ -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']; @@ -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; }