-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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. |
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 |
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? |
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)? |
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. |
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?
The text was updated successfully, but these errors were encountered: