Skip to content

Commit

Permalink
Replace spl_object_hash() with spl_object_id()
Browse files Browse the repository at this point in the history
It is more efficient, and can be provided to 7.1 users thanks to
symfony/polyfill-php72.
  • Loading branch information
Majkl578 authored and greg0ire committed Jul 10, 2021
1 parent 7a56ca1 commit 84ad007
Showing 14 changed files with 132 additions and 131 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
"doctrine/persistence": "^2.2",
"psr/cache": "^1 || ^2 || ^3",
"symfony/console": "^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/polyfill-php72": "^1.23",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
use Doctrine\ORM\Cache\CollectionCacheKey;
use Doctrine\ORM\PersistentCollection;

use function spl_object_hash;
use function spl_object_id;

class NonStrictReadWriteCachedCollectionPersister extends AbstractCollectionPersister
{
@@ -47,7 +47,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;
}

/**
@@ -69,14 +69,14 @@ public function update(PersistentCollection $collection)
if ($isDirty && ! $isInitialized || isset($this->association['orderBy'])) {
$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,
];
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Persisters\Collection\CollectionPersister;

use function spl_object_hash;
use function spl_object_id;

class ReadWriteCachedCollectionPersister extends AbstractCollectionPersister
{
@@ -78,7 +78,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,
];
@@ -106,7 +106,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,
];
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
use function is_array;
use function key;
use function ltrim;
use function spl_object_hash;
use function spl_object_id;

/**
* The ObjectHydrator constructs an object graph out of an SQL result set.
@@ -162,7 +162,7 @@ private function initRelatedCollection(
string $fieldName,
string $parentDqlAlias
): PersistentCollection {
$oid = spl_object_hash($entity);
$oid = spl_object_id($entity);
$relation = $class->associationMappings[$fieldName];
$value = $class->reflFields[$fieldName]->getValue($entity);

@@ -356,7 +356,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 (! ($relation['type'] & ClassMetadata::TO_ONE)) {
@@ -429,7 +429,7 @@ protected function hydrateRowData(array $row, array &$result)
$inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']];
if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_id($element), $inverseAssoc['fieldName'], $parentObject);
}
} elseif ($parentClass === $targetClass && $relation['mappedBy']) {
// Special case: bi-directional self-referencing one-one on the same class
@@ -438,7 +438,7 @@ protected function hydrateRowData(array $row, array &$result)
} else {
// For sure bidirectional, as there is no inverse side in unidirectional mappings
$targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_id($element), $relation['mappedBy'], $parentObject);
}

// Update result pointer
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
use function is_subclass_of;
use function ltrim;
use function method_exists;
use function spl_object_hash;
use function spl_object_id;
use function str_replace;
use function strpos;
use function strtolower;
@@ -833,7 +833,7 @@ public function getFieldValue($entity, $field)
*/
public function __toString()
{
return self::class . '@' . spl_object_hash($this);
return self::class . '@' . spl_object_id($this);
}

/**
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
use function is_object;
use function method_exists;
use function reset;
use function spl_object_hash;
use function spl_object_id;
use function sprintf;

/**
@@ -239,7 +239,7 @@ public static function invalidEntityName($entityName)
*/
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);
}

/**
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
use function array_walk;
use function get_class;
use function is_object;
use function spl_object_hash;
use function spl_object_id;

/**
* A PersistentCollection represents a collection of elements that have persistent state.
@@ -158,7 +158,7 @@ public function hydrateAdd($element): void
);

$this->em->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($element),
spl_object_id($element),
$this->backRefFieldName,
$this->owner
);
@@ -687,16 +687,16 @@ protected function doInitialize(): void
/**
* @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) {
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
use function is_array;
use function is_object;
use function reset;
use function spl_object_hash;
use function spl_object_id;
use function sprintf;
use function strpos;
use function strtoupper;
@@ -132,7 +132,7 @@ class BasicEntityPersister implements EntityPersister
/**
* Queued inserts.
*
* @psalm-var array<string, object>
* @psalm-var array<int, object>
*/
protected $queuedInserts = [];

@@ -233,7 +233,7 @@ public function getResultSetMapping()
*/
public function addInsert($entity)
{
$this->queuedInserts[spl_object_hash($entity)] = $entity;
$this->queuedInserts[spl_object_id($entity)] = $entity;
}

/**
@@ -640,7 +640,7 @@ protected function prepareUpdateData($entity)
}

if ($newVal !== null) {
$oid = spl_object_hash($newVal);
$oid = spl_object_id($newVal);

if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
// The associated entity $newVal is not yet persisted, so we must
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public function getResultSetMapping();
/**
* Get all queued inserts.
*
* @psalm-return array<string, object>
* @psalm-return array<string|int, object>
*/
public function getInserts();

4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;

use function spl_object_hash;
use function spl_object_id;

/**
* This factory is used to create default repository objects for entities at runtime.
@@ -24,7 +24,7 @@ final class DefaultRepositoryFactory implements RepositoryFactory
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
{
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_id($entityManager);

if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Tools/DebugUnitOfWorkListener.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
use function fwrite;
use function gettype;
use function is_object;
use function spl_object_hash;
use function spl_object_id;

/**
* Use this logger to dump the identity map during the onFlush event. This is useful for debugging
@@ -74,7 +74,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);
@@ -91,7 +91,7 @@ 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();
@@ -101,12 +101,12 @@ public function dumpIdentityMap(EntityManagerInterface $em)
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");
}
}
}
Loading

0 comments on commit 84ad007

Please sign in to comment.