Skip to content

Remove WeakMap entries whose key is only reachable through the entry value #10932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Zend/tests/weakrefs/gh10043-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Self-referencing map entry GC - 001
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$map[$obj] = $obj;

gc_collect_cycles();

var_dump($map);

$obj = null;
gc_collect_cycles();

var_dump($map);

--EXPECTF--
object(WeakMap)#%d (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
["value"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
}
}
object(WeakMap)#%d (0) {
}
44 changes: 44 additions & 0 deletions Zend/tests/weakrefs/gh10043-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Self-referencing map entry GC - 002
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$map[$obj] = [$obj];

gc_collect_cycles();

var_dump($map);

$obj = null;
gc_collect_cycles();

var_dump($map);

--EXPECTF--
object(WeakMap)#%d (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
["value"]=>
array(1) {
[0]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
}
}
}
object(WeakMap)#1 (0) {
}
50 changes: 50 additions & 0 deletions Zend/tests/weakrefs/gh10043-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
Self-referencing map entry GC - 003
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$map[$obj] = [$obj, $map];
$ref = WeakReference::create($map);

gc_collect_cycles();

var_dump($ref->get());

gc_collect_cycles();

// $obj is first in the root buffer
$obj = null;
$map = null;
gc_collect_cycles();

var_dump($ref->get());

--EXPECTF--
object(WeakMap)#%d (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
["value"]=>
array(2) {
[0]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
[1]=>
*RECURSION*
}
}
}
NULL
50 changes: 50 additions & 0 deletions Zend/tests/weakrefs/gh10043-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
Self-referencing map entry GC - 004
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$map[$obj] = [$map, $obj];
$ref = WeakReference::create($map);

gc_collect_cycles();

var_dump($ref->get());

gc_collect_cycles();

// $map is first in the root buffer
$map = null;
$obj = null;
gc_collect_cycles();

var_dump($ref->get());

--EXPECTF--
object(WeakMap)#%d (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
["value"]=>
array(2) {
[0]=>
*RECURSION*
[1]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
}
}
}
NULL
49 changes: 49 additions & 0 deletions Zend/tests/weakrefs/gh10043-005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
Self-referencing map entry GC - 005
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$value = [$obj];
$map[$obj] = $value;
$obj = null;

gc_collect_cycles();

var_dump($map);

gc_collect_cycles();

$value = null;

gc_collect_cycles();

var_dump($map);

--EXPECTF--
object(WeakMap)#%d (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
["value"]=>
array(1) {
[0]=>
object(Value)#%d (1) {
["value"]=>
string(1) "a"
}
}
}
}
object(WeakMap)#1 (0) {
}
41 changes: 41 additions & 0 deletions Zend/tests/weakrefs/gh10043-006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Self-referencing map entry GC - 006
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

$map = new WeakMap();
$obj = new Value('a');
$map[$obj] = $obj;

gc_collect_cycles();

$obj2 = $obj;
$obj = null;
$map2 = $map;
$map = null;

gc_collect_cycles();

var_dump($map2);

--EXPECT--
object(WeakMap)#1 (1) {
[0]=>
array(2) {
["key"]=>
object(Value)#2 (1) {
["value"]=>
string(1) "a"
}
["value"]=>
object(Value)#2 (1) {
["value"]=>
string(1) "a"
}
}
}
33 changes: 33 additions & 0 deletions Zend/tests/weakrefs/gh10043-007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Self-referencing map entry GC - 007
--FILE--
<?php

class Canary extends stdClass
{
public function __construct(public string $name)
{
}

function __destruct()
{
echo $this->name."\n";
}
}

$container = new Canary('container');
$canary = new Canary('canary');
$container->canary = $canary;

$map = new \WeakMap();
$map[$canary] = $container;

echo 1;
unset($container, $canary);
gc_collect_cycles();
echo 2;

--EXPECT--
1container
canary
2
30 changes: 30 additions & 0 deletions Zend/tests/weakrefs/gh10043-008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Self-referencing map entry GC - 008
--FILE--
<?php

class Canary extends stdClass
{
public function __construct(public string $name)
{
}

function __destruct()
{
echo $this->name."\n";
}
}

$canary = new Canary('canary');

$map = new \WeakMap();
$map[$canary] = $canary;

echo 1;
unset($canary);
gc_collect_cycles();
echo 2;

--EXPECT--
1canary
2
28 changes: 28 additions & 0 deletions Zend/tests/weakrefs/gh10043-009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Self-referencing map entry GC - 009
--FILE--
<?php

class Value {
public function __construct(public readonly string $value) {
}
}

function possibleRoot($var) {
}

$map = new WeakMap();
$obj = new stdClass();
$map[$obj] = new Value('a');
$map[$map] = $map;
$ref = WeakReference::create($map);

possibleRoot($obj);
$map = null;

gc_collect_cycles();

var_dump($ref->get());
?>
--EXPECT--
NULL
Loading