Skip to content

Commit 5403473

Browse files
authored
📦 Use WeakMap instead of SplObjectStorage to simplify GC work (#745)
1 parent 0d50325 commit 5403473

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/Context/Context.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
namespace TheCodingMachine\GraphQLite\Context;
66

7-
use SplObjectStorage;
87
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
98
use TheCodingMachine\GraphQLite\PrefetchBuffer;
9+
use WeakMap;
1010

1111
/**
1212
* A context class that should be passed to the Webonyx executor.
1313
*/
1414
class Context implements ContextInterface, ResetableContextInterface
1515
{
16-
private SplObjectStorage $prefetchBuffers;
16+
private WeakMap $prefetchBuffers;
1717

1818
public function __construct()
1919
{
20-
$this->prefetchBuffers = new SplObjectStorage();
20+
$this->prefetchBuffers = new WeakMap();
2121
}
2222

2323
/**
@@ -38,6 +38,6 @@ public function getPrefetchBuffer(ParameterInterface $field): PrefetchBuffer
3838

3939
public function reset(): void
4040
{
41-
$this->prefetchBuffers = new SplObjectStorage();
41+
$this->prefetchBuffers = new WeakMap();
4242
}
4343
}

src/PrefetchBuffer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace TheCodingMachine\GraphQLite;
66

77
use GraphQL\Type\Definition\ResolveInfo;
8-
use SplObjectStorage;
8+
use WeakMap;
99

1010
use function md5;
1111
use function serialize;
@@ -18,12 +18,12 @@ class PrefetchBuffer
1818
/** @var array<string, array<int, object>> An array of buffered, indexed by hash of arguments. */
1919
private array $objects = [];
2020

21-
/** @var SplObjectStorage A Storage of prefetch method results, holds source to resolved values. */
22-
private SplObjectStorage $results;
21+
/** @var WeakMap A Storage of prefetch method results, holds source to resolved values. */
22+
private WeakMap $results;
2323

2424
public function __construct()
2525
{
26-
$this->results = new SplObjectStorage();
26+
$this->results = new WeakMap();
2727
}
2828

2929
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */

0 commit comments

Comments
 (0)