Skip to content

Commit 5c2ff27

Browse files
authored
Merge pull request #6878 from Majkl578/dev/UoW-spl_object_id
Replace spl_object_hash() with more efficient spl_object_id()
2 parents 67dedeb + a346655 commit 5c2ff27

11 files changed

+78
-78
lines changed

lib/Doctrine/ORM/Cache/Persister/Collection/NonStrictReadWriteCachedCollectionPersister.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function delete(PersistentCollection $collection)
5454

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

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

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

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

8383
return;
8484
}
8585

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

88-
$this->queuedCache['update'][spl_object_hash($collection)] = [
88+
$this->queuedCache['update'][spl_object_id($collection)] = [
8989
'key' => $key,
9090
'list' => $collection
9191
];

lib/Doctrine/ORM/Cache/Persister/Collection/ReadWriteCachedCollectionPersister.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function delete(PersistentCollection $collection)
9090
return;
9191
}
9292

93-
$this->queuedCache['delete'][spl_object_hash($collection)] = [
93+
$this->queuedCache['delete'][spl_object_id($collection)] = [
9494
'key' => $key,
9595
'lock' => $lock
9696
];
@@ -118,7 +118,7 @@ public function update(PersistentCollection $collection)
118118
return;
119119
}
120120

121-
$this->queuedCache['update'][spl_object_hash($collection)] = [
121+
$this->queuedCache['update'][spl_object_id($collection)] = [
122122
'key' => $key,
123123
'lock' => $lock
124124
];

lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAl
172172
/** @var ToManyAssociationMetadata $association */
173173
$association = $class->getProperty($fieldName);
174174
$value = $association->getValue($entity);
175-
$oid = spl_object_hash($entity);
175+
$oid = spl_object_id($entity);
176176

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

346-
$oid = spl_object_hash($parentObject);
346+
$oid = spl_object_id($parentObject);
347347

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

423423
$this->uow->setOriginalEntityProperty(
424-
spl_object_hash($element),
424+
spl_object_id($element),
425425
$inverseAssociation->getName(),
426426
$parentObject
427427
);
@@ -434,7 +434,7 @@ protected function hydrateRowData(array $row, array &$result)
434434
$inverseAssociation->setValue($element, $parentObject);
435435

436436
$this->uow->setOriginalEntityProperty(
437-
spl_object_hash($element),
437+
spl_object_id($element),
438438
$mappedBy,
439439
$parentObject
440440
);

lib/Doctrine/ORM/Mapping/ClassMetadata.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function __clone()
308308
*/
309309
public function __toString()
310310
{
311-
return __CLASS__ . '@' . spl_object_hash($this);
311+
return __CLASS__ . '@' . spl_object_id($this);
312312
}
313313

314314
/**

lib/Doctrine/ORM/Mapping/Driver/DriverChain.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function getAllClassNames()
128128

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

133133
if (!isset($driverClasses[$oid])) {
134134
$driverClasses[$oid] = $driver->getAllClassNames();

lib/Doctrine/ORM/ORMInvalidArgumentException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public static function invalidAssociation(ClassMetadata $targetClass, Associatio
249249
*/
250250
private static function objToStr($obj) : string
251251
{
252-
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj);
252+
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_id($obj);
253253
}
254254

255255
/**

lib/Doctrine/ORM/PersistentCollection.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function hydrateAdd($element)
160160
$inversedAssociation->setValue($element, $this->owner);
161161

162162
$this->em->getUnitOfWork()->setOriginalEntityProperty(
163-
spl_object_hash($element), $this->backRefFieldName, $this->owner
163+
spl_object_id($element), $this->backRefFieldName, $this->owner
164164
);
165165
}
166166
}
@@ -187,7 +187,7 @@ public function hydrateSet($key, $element)
187187
$inversedAssociation->setValue($element, $this->owner);
188188

189189
$this->em->getUnitOfWork()->setOriginalEntityProperty(
190-
spl_object_hash($element), $this->backRefFieldName, $this->owner
190+
spl_object_id($element), $this->backRefFieldName, $this->owner
191191
);
192192
}
193193
}
@@ -243,8 +243,8 @@ public function getDeleteDiff()
243243
$collectionItems = $this->collection->toArray();
244244

245245
return \array_values(\array_diff_key(
246-
\array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot),
247-
\array_combine(\array_map('spl_object_hash', $collectionItems), $collectionItems)
246+
\array_combine(\array_map('spl_object_id', $this->snapshot), $this->snapshot),
247+
\array_combine(\array_map('spl_object_id', $collectionItems), $collectionItems)
248248
));
249249
}
250250

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

261261
return \array_values(\array_diff_key(
262-
\array_combine(\array_map('spl_object_hash', $collectionItems), $collectionItems),
263-
\array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot)
262+
\array_combine(\array_map('spl_object_id', $collectionItems), $collectionItems),
263+
\array_combine(\array_map('spl_object_id', $this->snapshot), $this->snapshot)
264264
));
265265
}
266266

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

737737
if ($newObjectsThatWereNotLoaded) {

lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class DefaultRepositoryFactory implements RepositoryFactory
2626
*/
2727
public function getRepository(EntityManagerInterface $entityManager, $entityName)
2828
{
29-
$repositoryHash = $entityManager->getClassMetadata($entityName)->getClassName() . spl_object_hash($entityManager);
29+
$repositoryHash = $entityManager->getClassMetadata($entityName)->getClassName() . spl_object_id($entityManager);
3030

3131
if (isset($this->repositoryList[$repositoryHash])) {
3232
return $this->repositoryList[$repositoryHash];

lib/Doctrine/ORM/Tools/DebugUnitOfWorkListener.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function dumpIdentityMap(EntityManagerInterface $em)
7979
fwrite($fh, "Class: ". $className . "\n");
8080

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

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

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

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

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

120120
foreach ($value->unwrap() as $obj) {
121-
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
121+
fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_id($obj)."\n");
122122
}
123123
}
124124
}

0 commit comments

Comments
 (0)