Skip to content

Performance problem about reindexChildren with replace parentNode #1281

Closed
@gjchoi

Description

@gjchoi

Hello,
my application suffers from poor performance in case of over 10Mbyte html data..

In my case,
I take a parsed HTML document from jsoup parser, and replace element with wrapperElement class ( custom class RichElement ) to add more custom data with element attributes.

This call the method addChildren at Element.class,
with over 100,000 parsed elements,
but this is very slow...

because addChildren call reparentChild ( => setParentNode) 100,000 times,
each setParentNode occures u​nnecessary reindexing for sibling children about 1/2 * 100,000 times in this case;;

Do you hava any idea,
only replace parent keep children without u​nnecessary reindexing?

or

Could you add some method like below?

Element.class

    protected Element replaceParent(List<Node> children) {

        ArrayList<Node> nodes = new ArrayList<>(children);
        Node[] nodeArray = nodes.toArray(new Node[nodes.size()]);
        replaceChildrenWithoutReindex(nodeArray);
        return this;
    }

Node.class

    protected void replaceChildrenWithoutReindex(Node... children) {
        Validate.isTrue(this.childNodeSize() == 0, "replaceChildrenWithoutReindex valid with new parent (no child)");


        List<Node> nodes = this.ensureChildNodes();
        Node[] var3 = children;
        int var4 = children.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Node child = var3[var5];
            //this.reparentChild(child); 
            child.parentNode = this;       //without reindex, reparent parent keep sibling indexes;
            nodes.add(child);
            child.setSiblingIndex(nodes.size() - 1);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementAn improvement / new feature idea

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions