Skip to content

Commit

Permalink
refacto: use Weakmap for UnitOfWork internal storage instead of uid i…
Browse files Browse the repository at this point in the history
…ndexed arrays
  • Loading branch information
vgreb committed Dec 18, 2024
1 parent 301b1d7 commit 8a663ca
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 131 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ name: Tests

on:
push:
branches:
- "master"
pull_request:
schedule:
- cron: '0 0 * * MON'

jobs:
run:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
php:
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4
composer:
- v2
name: Run Unit Tests on PHP ${{ matrix.php }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "library",
"license": "Apache-2.0",
"require": {
"php": ">=7.1",
"php": ">=8.0",
"pimple/pimple": "^3.0",
"aura/sqlquery": "^2.6",
"doctrine/cache": "^1.6"
Expand Down
19 changes: 10 additions & 9 deletions src/Ting/Repository/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use CCMBenchmark\Ting\Serializer\UnserializeInterface;
use CCMBenchmark\Ting\UnitOfWork;
use Generator;
use WeakMap;

/**
* @template T
Expand All @@ -45,7 +46,7 @@ class Hydrator implements HydratorInterface
protected $objectDatabase = [];
protected $objectSchema = [];
protected $unserializeAliases = [];
protected $alreadyManaged = [];
protected WeakMap $alreadyManaged;
protected $references = [];
protected $metadataList = [];

Expand All @@ -69,6 +70,11 @@ class Hydrator implements HydratorInterface
*/
protected $identityMap = false;

public function __construct()
{
$this->alreadyManaged = new WeakMap();
}

/**
* @param bool $enable
* @throws Exception
Expand Down Expand Up @@ -243,7 +249,7 @@ private function hasVirtualObject(array $result)
*/
private function unserializeVirtualObjectProperty(\stdClass $virtualObject)
{
foreach ($this->unserializeAliases as $aliasName => list($unserialize, $options)) {
foreach ($this->unserializeAliases as $aliasName => [$unserialize, $options]) {
if (isset($virtualObject->$aliasName) === true) {
$virtualObject->$aliasName = $unserialize->unserialize($virtualObject->$aliasName, $options);
}
Expand Down Expand Up @@ -326,7 +332,6 @@ function (Metadata $metadata) use ($column, &$result) {
if ($this->identityMap === false) {
// If identityMap is disabled, it clones the object and reset UUID
$result[$column['table']] = clone $this->references[$ref];
unset($result[$column['table']]->tingUUID);
} else {
// If identityMap is enabled, it uses the same object
$result[$column['table']] = $this->references[$ref];
Expand Down Expand Up @@ -440,13 +445,9 @@ function (Metadata $metadata) use ($column, &$result) {
*/
private function manageIfYouCan($entity)
{
if (isset($entity->tingUUID) === true && isset($this->alreadyManaged[$entity->tingUUID]) === true) {
return;
}

if (\is_object($entity) === true && ($entity instanceof NotifyPropertyInterface) === true) {
if (\is_object($entity) === true && ($entity instanceof NotifyPropertyInterface) === true && $this->alreadyManaged->offsetExists($entity) === false) {
$this->unitOfWork->manage($entity);
$this->alreadyManaged[$entity->tingUUID] = true;
$this->alreadyManaged[$entity] = true;
}
}
}
1 change: 1 addition & 0 deletions src/Ting/Repository/HydratorRelational.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ final class HydratorRelational extends Hydrator

public function __construct()
{
parent::__construct();
$this->config = new \SplDoublyLinkedList();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Ting/Repository/HydratorSingleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
class HydratorSingleObject extends Hydrator
{

public function __construct()
{
parent::__construct();
}

/**
* @return \Generator<int, T>
*/
Expand Down
Loading

0 comments on commit 8a663ca

Please sign in to comment.