-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Improve node.children handling #1969
Comments
Notes from reading the code:
|
Enforcing the usage of Another issue I have is with |
|
I opened this issue because of the problematic if-clause. It's O(n²) and there are no ways around the problem with custom plugins. Like you suggested, removing the check and using As for |
UPDATE: since there is no way to bypass this problem with the custom plugin codes itself, I did the following:
notes:
|
The thing is that of course there isn't any breakage because it's deleted anyway. I bet this change could slow down some SVGs where the hidden elements have many many children. |
I'm having hard time seeing how this would slow down anything? The elements with many children were the problem in my case, since each element of the list calls |
Well ideally it should always return visitSkip whenever a node is detached. I might be misinterpreting your comment, because what I got from it is that you only added a new visitSkip to one plugin. |
Ah, I see. I also removed the if-clause from xast.js and manually added all the necessary files to my project up until that. That being said, if you decide to fix this, I'll be glad to assist. I'd even love to get the detachNodeFromParent issues fixed too, but that requires a structural change. |
I'm dealing with very large SVG pictures which I need to shrink. My setup is running SVGO in AWS Lambda through API Gateway, which puts my time limit to 29 seconds, which in several cases is not enough for the optimization.
I've started delving deeper into where the time goes, since in my mind the optimization process should be linear in several of the preset plugins. I have an example SVG with around 46000 lines/polylines/what have you (which eventually shrink to 27 paths) and even the simplest plugins seem to take 500 ms on average. This is caused by the following
.includes
check inxast.js
:if (parentNode.children.includes(node)) {
for (const child of node.children) {
visit(child, visitor, node);
}
}
Although I'm sure this check is there for a reason, removing it had no effect on my sample data result aside from removing 15 seconds out of the 19 it previously took. The way
xast.visit
function currently works it's not possible to, for example, use a Set to handle the validity of nodes. Handling the deletion of children in plugins is also fairly ineffective, sometimes usingsplice
withindexOf
and sometimesxast.detachNodeFromParent
.Introducing an index Set to each node, refactoring
xast.js
and preset plugins to support the index Set is easy, but this change would possibly break existing self-made plugins. My goal here is to start a small discussion about this, since there are easy performance improvements to be gained.What does removing the if-clause break? Should the plugins be advised not to modify their siblings (so the check is unneccessary)? One possible solution might be to offer an interface to get
.next()
andprevious()
siblings for the plugins, and then tag the nodes deleted and only delete them after the forementioned for-loop has ended.The text was updated successfully, but these errors were encountered: