Skip to content
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

Support WeakReferences in NodeConnectingVisitor #1057

Merged
merged 7 commits into from
Feb 26, 2025
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ phpstan: tools/vendor

php-cs-fixer: tools/vendor
php tools/vendor/bin/php-cs-fixer fix

tests:
php vendor/bin/phpunit
14 changes: 14 additions & 0 deletions doc/component/FAQ.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ obtained through `$node->getAttribute('next')`.

`ParentConnectingVisitor` and `NodeConnectingVisitor` should not be used at the same time. The latter
includes the functionality of the former.


How can I limit the impact of cyclic references in the AST?
-----

NodeConnectingVisitor adds a parent reference, which introduces a cycle. This means that the AST can now only be collected by the cycle garbage collector.
This in turn can lead to performance and/or memory issues.

To break the cyclic references between AST nodes `NodeConnectingVisitor` supports a boolean `$weakReferences` constructor parameter.
When set to `true`, all attributes added by `NodeConnectingVisitor` will be wrapped into a `WeakReference` object.

After enabling this parameter, the parent node can be obtained through `$node->getAttribute('weak_parent')`,
the previous node can be obtained through `$node->getAttribute('weak_previous')`, and the next node can be
obtained through `$node->getAttribute('weak_next')`.
32 changes: 27 additions & 5 deletions lib/PhpParser/NodeVisitor/NodeConnectingVisitor.php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the class comment for the new option?

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
* Visitor that connects a child node to its parent node
* as well as its sibling nodes.
*
* On the child node, the parent node can be accessed through
* With <code>$weakReferences=false</code> on the child node, the parent node can be accessed through
* <code>$node->getAttribute('parent')</code>, the previous
* node can be accessed through <code>$node->getAttribute('previous')</code>,
* and the next node can be accessed through <code>$node->getAttribute('next')</code>.
*
* With <code>$weakReferences=true</code> attribute names are prefixed by "weak_", e.g. "weak_parent".
*/
final class NodeConnectingVisitor extends NodeVisitorAbstract {
/**
Expand All @@ -25,19 +27,39 @@ final class NodeConnectingVisitor extends NodeVisitorAbstract {
*/
private $previous;

private bool $weakReferences;

public function __construct(bool $weakReferences = false) {
$this->weakReferences = $weakReferences;
}

public function beforeTraverse(array $nodes) {
$this->stack = [];
$this->previous = null;
}

public function enterNode(Node $node) {
if (!empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
$parent = $this->stack[count($this->stack) - 1];
if ($this->weakReferences) {
$node->setAttribute('weak_parent', \WeakReference::create($parent));
} else {
$node->setAttribute('parent', $parent);
}
}

if ($this->previous !== null && $this->previous->getAttribute('parent') === $node->getAttribute('parent')) {
$node->setAttribute('previous', $this->previous);
$this->previous->setAttribute('next', $node);
if ($this->previous !== null) {
if (
$this->weakReferences
) {
if ($this->previous->getAttribute('weak_parent') === $node->getAttribute('weak_parent')) {
$node->setAttribute('weak_previous', \WeakReference::create($this->previous));
$this->previous->setAttribute('weak_next', \WeakReference::create($node));
}
} elseif ($this->previous->getAttribute('parent') === $node->getAttribute('parent')) {
$node->setAttribute('previous', $this->previous);
$this->previous->setAttribute('next', $node);
}
}

$this->stack[] = $node;
Expand Down
17 changes: 15 additions & 2 deletions lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@
/**
* Visitor that connects a child node to its parent node.
*
* On the child node, the parent node can be accessed through
* With <code>$weakReferences=false</code> on the child node, the parent node can be accessed through
* <code>$node->getAttribute('parent')</code>.
*
* With <code>$weakReferences=true</code> the attribute name is "weak_parent" instead.
*/
final class ParentConnectingVisitor extends NodeVisitorAbstract {
/**
* @var Node[]
*/
private array $stack = [];

private bool $weakReferences;

public function __construct(bool $weakReferences = false) {
$this->weakReferences = $weakReferences;
}

public function beforeTraverse(array $nodes) {
$this->stack = [];
}

public function enterNode(Node $node) {
if (!empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
$parent = $this->stack[count($this->stack) - 1];
if ($this->weakReferences) {
$node->setAttribute('weak_parent', \WeakReference::create($parent));
} else {
$node->setAttribute('parent', $parent);
}
}

$this->stack[] = $node;
Expand Down
24 changes: 24 additions & 0 deletions test/PhpParser/NodeVisitor/NodeConnectingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ public function testConnectsNodeToItsParentNodeAndItsSiblingNodes(): void {

$this->assertSame(Else_::class, get_class($node->getAttribute('next')));
}

public function testWeakReferences(): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php if (true) {} else {}'
);

$traverser = new NodeTraverser();

$traverser->addVisitor(new NodeConnectingVisitor(true));

$ast = $traverser->traverse($ast);

$node = (new NodeFinder())->findFirstInstanceof($ast, Else_::class);

$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_parent'));
$this->assertSame(If_::class, get_class($node->getAttribute('weak_parent')->get()));
$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_previous'));
$this->assertSame(ConstFetch::class, get_class($node->getAttribute('weak_previous')->get()));

$node = (new NodeFinder())->findFirstInstanceof($ast, ConstFetch::class);

$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_next'));
$this->assertSame(Else_::class, get_class($node->getAttribute('weak_next')->get()));
}
}
18 changes: 18 additions & 0 deletions test/PhpParser/NodeVisitor/ParentConnectingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,22 @@ public function testConnectsChildNodeToParentNode(): void {

$this->assertSame('C', $node->getAttribute('parent')->name->toString());
}

public function testWeakReferences(): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php class C { public function m() {} }'
);

$traverser = new NodeTraverser();

$traverser->addVisitor(new ParentConnectingVisitor(true));

$ast = $traverser->traverse($ast);

$node = (new NodeFinder())->findFirstInstanceof($ast, ClassMethod::class);

$weakReference = $node->getAttribute('weak_parent');
$this->assertInstanceOf(\WeakReference::class, $weakReference);
$this->assertSame('C', $weakReference->get()->name->toString());
}
}