framework icon indicating copy to clipboard operation
framework copied to clipboard

TreeGrid#expand "is very slow" with 10'000 subnodes (Vaadin 8.1.3)

Open Petikoch opened this issue 9 years ago • 7 comments

Given the following example:

package treegrid_expand_slow;

import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.TreeGrid;
import com.vaadin.ui.UI;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@SpringUI
public class TreegridExpandSlowUI extends UI {

    private static final int NUMBER_OF_SUBPROJECTS = 10_000;

    @Override
    protected void init(VaadinRequest request) {
        TreeGrid<JProject> treeGrid = new TreeGrid<>();
        final List<JProject> rootProjects = createRootProjects();

        treeGrid.setItems(rootProjects, JProject::getSubProjects);

        treeGrid.addColumn(JProject::getName).setCaption("Project Name");
        treeGrid.addColumn(JProject::getHoursDone).setCaption("Hours Done");
        treeGrid.addColumn(JProject::getLastModified).setCaption("Last Modified");

        treeGrid.setSizeFull();
        setContent(treeGrid);
    }

    private List<JProject> createRootProjects() {
        List<JProject> rootProjects = new ArrayList<>();
        rootProjects.add(
                new JProject(
                        "All projects",
                        NUMBER_OF_SUBPROJECTS,
                        LocalDateTime.now(),
                        IntStream.rangeClosed(1, NUMBER_OF_SUBPROJECTS).boxed()
                                .map(integer -> new JProject("Project #" + integer, 1, LocalDateTime.now(), new ArrayList<>()))
                                .collect(Collectors.toList())
                )
        );
        return rootProjects;
    }
}


package treegrid_expand_slow;

import java.time.LocalDateTime;
import java.util.List;

public class JProject {

    private final String name;
    private final int hoursDone;
    private final LocalDateTime lastModified;
    private final List<JProject> subProjects;

    public JProject(String name, int hoursDone, LocalDateTime lastModified, List<JProject> subProjects) {
        this.name = name;
        this.hoursDone = hoursDone;
        this.lastModified = lastModified;
        this.subProjects = subProjects;
    }

    public String getName() {
        return name;
    }

    public int getHoursDone() {
        return hoursDone;
    }

    public LocalDateTime getLastModified() {
        return lastModified;
    }

    public List<JProject> getSubProjects() {
        return subProjects;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        JProject jProject = (JProject) o;

        return name.equals(jProject.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}

When: I open the browser

Then: the TreeGrid is displayed "very fast".

When: I expand the "All Projects" root node

Then: I wait a couple of seconds (5 secs on my machine) until I see the first 26 sub projects.

I guess It looks for me like TreeGrid pushes all the data to the client in an expand event, even only some of them will be displayed. There seems to be no optimization on transferring only a actually useful amount of data.

In my real world application, I have a some nodes in a TreeGrid with 50'000 subnodes and clicking the expand triangle takes up to a minute. During that minute a full CPU core is 100% used and the electric power bill is enormous. The profiler tool shows the time is spent in the HierarchyMapper.

Question Is this a current limitation of TreeGrid or do I make it wrong? Are there workarounds?

Thanks for your time & analysis, Peti

Petikoch avatar Sep 08 '17 12:09 Petikoch

We have also noticed this. It is not a problem of sending too much data from server to client. To my understanding the problem comes from bad programming around hierarchy mapper. Some pieces of code look very pretty with modern lambda and stream stuff, but nonetheless when you repeatedly run through all of your nodes performance suffers. We had a example with 51000 nodes, here it does not take a Minute, but more than an hour to handle all nodes.

Have found three methods that cause problems: (1) HierchyMapper.getDepth(), or .getParentOfItem(). Here the mapper should either also maintain the reverse order, not only parent->children, or at least offer a possibility that application code can provide this Information. (2) ActiveDataHandler.getActiveData() - on each invocation this compiles a map of all nodes, and is only used via #containsKey() to check if a certain node is in the tree - so would be much easier if the ActiveDataHandler would answer this directly (something like #isNodeActive(aNode) ). (3) HierarchyMapper.getIndexOf() - here also every invocations assembles the whole tree into a flat list, then searches through this list to find the flat index of an entry.

ui5moebus avatar Sep 11 '17 07:09 ui5moebus

Thanks @ui5moebus for the insights... this absolutely makes sense and explains very well the high CPU usage.

@hesara How does the vaadin team handle such issues? Does the team address them anyway in upcoming releases? Or is vaadin more a community driven project and pull requests from the community will fix the issue?

Petikoch avatar Sep 11 '17 07:09 Petikoch

Hi @Petikoch and @ui5moebus ,

We are aware that there are issues with the performance of the TreeGrid in this specific case. I spent quite a long time during June attempting to make the hierarchy data handling better (there were some initial design flaws). To improve this, we would need to make some changes to the hierarchy data provider (suggested parent information) and some to the data communication (I already had some plans for that). Since this would more or less break some APIs we can't do it in a maintenance release.

We should be able to make improvements in the framework to remove the need for .getIndexOf by caching data on the client a bit differently, but this will take a lot of testing since lazy loading, sorting, filtering and generic cache invalidation is not a trivial topic to "just fix".

tsuoanttila avatar Sep 11 '17 08:09 tsuoanttila

Thank you @tsuoanttila for the insights.

Please take a moment to think about a little optimization: by adding a second map in the HierarchyMapper (private Map<T, T> child2ParentMap) and keeping track of that relation (instead of calculating it in getParentOfItem), the initial example with 10'000 subnodes performs much better. The CPU usage is significant smaller.

Of course, this is not the solution, but it reduces the pain a bit.

And of course, a better data structure like a bidirectional multimap or something like a treenode-datastructure would be even more elegant.

Petikoch avatar Sep 11 '17 15:09 Petikoch

@tsuoanttila another question regarding a potential workaround...

By using the TreeGrid with a custom HierarchicalDataProvider - which would fake the in-memory data as "external backend data" with lazy loading, offset, etc (by return isInMemory() -> false) - would this solve the issue as workaround?

Petikoch avatar Sep 13 '17 11:09 Petikoch

The algorithm in HierarchyMapper has two key bottlenecks. First, getParentIndex(...) itself is slow because it runs linearly through the entire list. Second of all, getParentOfItem(...) is slow because it runs through all entries on every iteration.

pwilkin avatar Sep 14 '17 10:09 pwilkin

Thank you very much @pwilkin @hesara @tsuoanttila for the fix.

Works well with Vaadin 8.1.5.

Petikoch avatar Oct 03 '17 08:10 Petikoch