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

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jan 1, 2025

closes #1055

we observed perf improvements on a similar visitor in staabm/phpstan-dba#667 (comment)

@staabm
Copy link
Contributor Author

staabm commented Jan 1, 2025

I think in CloningVisitor we don't need a weak-ref because we clone the elements, right?

if ($this->weakReferences) {
$parent = \WeakReference::create($parent);
}
$node->setAttribute('parent', $parent);
Copy link
Owner

Choose a reason for hiding this comment

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

One thing I'm wondering about here is whether we should reuse the same attribute names, or use a different one like 'weak_parent', to avoid having two possible types for these attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could use separate attribute names, but then I would go with a separate WeakNodeConnectingVisitor class and leave NodeConnectingVisitor untouched ..?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adjusted

Copy link
Owner

Choose a reason for hiding this comment

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

Given that the implementation themselves are pretty trivial, I'd be fine with adding separate classes as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am fine with the PR as is, in case you are also ok with it

@staabm
Copy link
Contributor Author

staabm commented Feb 21, 2025

Friendly ping

Copy link
Owner

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Can you please also update https://github.com/nikic/PHP-Parser/blob/master/doc/component/FAQ.markdown to mention the new option (and why it is recommended to use it)?

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?

if ($this->previous !== null) {
if (
$this->weakReferences
&& $this->previous->getAttribute('weak_parent') === $node->getAttribute('weak_parent')
Copy link
Owner

Choose a reason for hiding this comment

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

I'd split these conditions. If weakReferences we don't want to fall through to the condition below.

Copy link
Owner

Choose a reason for hiding this comment

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

Same here, update class comment.

@staabm
Copy link
Contributor Author

staabm commented Feb 25, 2025

addressed your feedback, thank you.

@nikic nikic merged commit c9d0b6c into nikic:master Feb 26, 2025
10 checks passed
@staabm staabm deleted the weak branch February 26, 2025 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inefficient NodeConnectingVisitor
2 participants