Skip to content

Commit

Permalink
Merge pull request #6878 from Majkl578/dev/UoW-spl_object_id
Browse files Browse the repository at this point in the history
Replace spl_object_hash() with more efficient spl_object_id()
  • Loading branch information
Ocramius authored Dec 22, 2017
2 parents 67dedeb + a346655 commit 5c2ff27
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function delete(PersistentCollection $collection)

$this->persister->delete($collection);

$this->queuedCache['delete'][spl_object_hash($collection)] = $key;
$this->queuedCache['delete'][spl_object_id($collection)] = $key;
}

/**
Expand All @@ -78,14 +78,14 @@ public function update(PersistentCollection $collection)
($this->association instanceof ToManyAssociationMetadata && $this->association->getOrderBy())) {
$this->persister->update($collection);

$this->queuedCache['delete'][spl_object_hash($collection)] = $key;
$this->queuedCache['delete'][spl_object_id($collection)] = $key;

return;
}

$this->persister->update($collection);

$this->queuedCache['update'][spl_object_hash($collection)] = [
$this->queuedCache['update'][spl_object_id($collection)] = [
'key' => $key,
'list' => $collection
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function delete(PersistentCollection $collection)
return;
}

$this->queuedCache['delete'][spl_object_hash($collection)] = [
$this->queuedCache['delete'][spl_object_id($collection)] = [
'key' => $key,
'lock' => $lock
];
Expand Down Expand Up @@ -118,7 +118,7 @@ public function update(PersistentCollection $collection)
return;
}

$this->queuedCache['update'][spl_object_hash($collection)] = [
$this->queuedCache['update'][spl_object_id($collection)] = [
'key' => $key,
'lock' => $lock
];
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAl
/** @var ToManyAssociationMetadata $association */
$association = $class->getProperty($fieldName);
$value = $association->getValue($entity);
$oid = spl_object_hash($entity);
$oid = spl_object_id($entity);

if (! $value instanceof PersistentCollection) {
$value = $association->wrap($entity, $value, $this->em);
Expand Down Expand Up @@ -343,7 +343,7 @@ protected function hydrateRowData(array $row, array &$result)
continue;
}

$oid = spl_object_hash($parentObject);
$oid = spl_object_id($parentObject);

// Check the type of the relation (many or single-valued)
if (! ($association instanceof ToOneAssociationMetadata)) {
Expand Down Expand Up @@ -421,7 +421,7 @@ protected function hydrateRowData(array $row, array &$result)
$inverseAssociation->setValue($element, $parentObject);

$this->uow->setOriginalEntityProperty(
spl_object_hash($element),
spl_object_id($element),
$inverseAssociation->getName(),
$parentObject
);
Expand All @@ -434,7 +434,7 @@ protected function hydrateRowData(array $row, array &$result)
$inverseAssociation->setValue($element, $parentObject);

$this->uow->setOriginalEntityProperty(
spl_object_hash($element),
spl_object_id($element),
$mappedBy,
$parentObject
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function __clone()
*/
public function __toString()
{
return __CLASS__ . '@' . spl_object_hash($this);
return __CLASS__ . '@' . spl_object_id($this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/DriverChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function getAllClassNames()

/* @var $driver MappingDriver */
foreach ($this->drivers as $namespace => $driver) {
$oid = spl_object_hash($driver);
$oid = spl_object_id($driver);

if (!isset($driverClasses[$oid])) {
$driverClasses[$oid] = $driver->getAllClassNames();
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static function invalidAssociation(ClassMetadata $targetClass, Associatio
*/
private static function objToStr($obj) : string
{
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj);
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_id($obj);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function hydrateAdd($element)
$inversedAssociation->setValue($element, $this->owner);

$this->em->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($element), $this->backRefFieldName, $this->owner
spl_object_id($element), $this->backRefFieldName, $this->owner
);
}
}
Expand All @@ -187,7 +187,7 @@ public function hydrateSet($key, $element)
$inversedAssociation->setValue($element, $this->owner);

$this->em->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($element), $this->backRefFieldName, $this->owner
spl_object_id($element), $this->backRefFieldName, $this->owner
);
}
}
Expand Down Expand Up @@ -243,8 +243,8 @@ public function getDeleteDiff()
$collectionItems = $this->collection->toArray();

return \array_values(\array_diff_key(
\array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot),
\array_combine(\array_map('spl_object_hash', $collectionItems), $collectionItems)
\array_combine(\array_map('spl_object_id', $this->snapshot), $this->snapshot),
\array_combine(\array_map('spl_object_id', $collectionItems), $collectionItems)
));
}

Expand All @@ -259,8 +259,8 @@ public function getInsertDiff()
$collectionItems = $this->collection->toArray();

return \array_values(\array_diff_key(
\array_combine(\array_map('spl_object_hash', $collectionItems), $collectionItems),
\array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot)
\array_combine(\array_map('spl_object_id', $collectionItems), $collectionItems),
\array_combine(\array_map('spl_object_id', $this->snapshot), $this->snapshot)
));
}

Expand Down Expand Up @@ -722,16 +722,16 @@ protected function doInitialize()
/**
* @param object[] $newObjects
*
* Note: the only reason why this entire looping/complexity is performed via `spl_object_hash`
* Note: the only reason why this entire looping/complexity is performed via `spl_object_id`
* is because we want to prevent using `array_udiff()`, which is likely to cause very
* high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in
* core, which is faster than using a callback for comparisons
*/
private function restoreNewObjectsInDirtyCollection(array $newObjects) : void
{
$loadedObjects = $this->collection->toArray();
$newObjectsByOid = \array_combine(\array_map('spl_object_hash', $newObjects), $newObjects);
$loadedObjectsByOid = \array_combine(\array_map('spl_object_hash', $loadedObjects), $loadedObjects);
$newObjectsByOid = \array_combine(\array_map('spl_object_id', $newObjects), $newObjects);
$loadedObjectsByOid = \array_combine(\array_map('spl_object_id', $loadedObjects), $loadedObjects);
$newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, $loadedObjectsByOid);

if ($newObjectsThatWereNotLoaded) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class DefaultRepositoryFactory implements RepositoryFactory
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
$repositoryHash = $entityManager->getClassMetadata($entityName)->getClassName() . spl_object_hash($entityManager);
$repositoryHash = $entityManager->getClassMetadata($entityName)->getClassName() . spl_object_id($entityManager);

if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Tools/DebugUnitOfWorkListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function dumpIdentityMap(EntityManagerInterface $em)
fwrite($fh, "Class: ". $className . "\n");

foreach ($map as $entity) {
fwrite($fh, " Entity: " . $this->getIdString($entity, $uow) . " " . spl_object_hash($entity)."\n");
fwrite($fh, " Entity: " . $this->getIdString($entity, $uow) . " " . spl_object_id($entity)."\n");
fwrite($fh, " Associations:\n");

$cm = $em->getClassMetadata($className);
Expand All @@ -104,21 +104,21 @@ public function dumpIdentityMap(EntityManagerInterface $em)
fwrite($fh, "[PROXY] ");
}

fwrite($fh, $this->getIdString($value, $uow) . " " . spl_object_hash($value) . "\n");
fwrite($fh, $this->getIdString($value, $uow) . " " . spl_object_id($value) . "\n");
} else {
$initialized = !($value instanceof PersistentCollection) || $value->isInitialized();

if ($initialized) {
fwrite($fh, "[INITIALIZED] " . $this->getType($value). " " . count($value) . " elements\n");

foreach ($value as $obj) {
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_id($obj)."\n");
}
} else {
fwrite($fh, "[PROXY] " . $this->getType($value) . " unknown element size\n");

foreach ($value->unwrap() as $obj) {
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_id($obj)."\n");
}
}
}
Expand Down
Loading

0 comments on commit 5c2ff27

Please sign in to comment.