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

Inefficient NodeConnectingVisitor #1055

Open
staabm opened this issue Dec 30, 2024 · 5 comments · May be fixed by #1057
Open

Inefficient NodeConnectingVisitor #1055

staabm opened this issue Dec 30, 2024 · 5 comments · May be fixed by #1057

Comments

@staabm
Copy link
Contributor

staabm commented Dec 30, 2024

when using NodeConnectingVisitor most AST nodes get additional attributes, which turns GC inefficient.

PHPStan enabled a optimization in https://phpstan.org/blog/preprocessing-ast-for-custom-rules to get rid of NodeConnectingVisitor and I also got reports in phpstan-dba which shows concrete profile numbers about this very same problem (I think).

is there anything which can be improved in NodeConnectingVisitor to better support GC?

@nikic
Copy link
Owner

nikic commented Dec 30, 2024

NodeConnectingVisitor adds a parent reference, which introduces a cycle. This means that the AST can now only be collected by cycle GC. As adding the parent reference is the point of the visitor, there's nothing that can be done about it. This is also why the AST does not provide parent references by default and requires the opt-in via the visitor.

The only improvement I can think of here would be to allow explicitly destroying AST nodes, which would clear out the parent reference and break the cycle. This would have to be done explicitly by the user though.

@staabm
Copy link
Contributor Author

staabm commented Dec 30, 2024

Maybe this "side-effects" should be mentioned in the class comment, so people get aware of it?

I am fine if there is no room for improvement.

In my case I copied the visitor and reduced it to the props I really need to make the problem less impactful

@nikic
Copy link
Owner

nikic commented Dec 30, 2024

Thinking about this again, a possible improvement would be to store the parent reference as a WeakReference. That will make usage a bit more awkward, but avoid the cycle. I can't change the existing visitor, but maybe could add an option to use a weak reference?

@staabm
Copy link
Contributor Author

staabm commented Dec 30, 2024

why does only the parent reference create a cycle? wouldn't previous/next refernce also create a cycle of nodes (which all have the same parent)?

@nikic
Copy link
Owner

nikic commented Dec 30, 2024

Yes, previous/next also create a cycle (if both a present, it's not a cycle if there's only one). I'd switch all three to use WeakReference if going down this path.

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 a pull request may close this issue.

2 participants