Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve performance of populating a massive tree view?

First of all I am answering my own question Q/A style, so I don't necessarily need anyone to answer this. It's something I've learned and many can make use of this.

I have a tree view which consists of many different nodes. Each node has an object behind it in its Data property, and the objects refer to different levels of hierarchy from one master list of objects, which is quite large (many thousands of items). One node represents a specific property on this main listed object, where the tree allows the user to select a node to view those items which fall under that specific selected category.

When the tree is being populated, it becomes extremely time consuming (in some cases 2 minutes), because each node needs to iterate through every item in this large list and find each item in this list which falls under any given node. Therefore, if there are to be 500 nodes in this tree, then it iterates through this large list 500 times. There are a total of 3 levels of hierarchy - the performance choke comes in when loading the second and third levels, but the first level is simple and quick.

Now there aren't any options to improve the performance of iterating through this list hundreds of times. What I'm wondering is are there any known tricks to improve the performance of populating the tree view?

Here's how it currently works:

var
  X: Integer;
  N: TTreeNode;
  O: TMyObject;
begin
  for X := 0 to MyObjectList.Count - 1 do begin
    O:= TMyObject(MyObjectList[X]); //Object which Node represents
    N:= TreeView.Items.AddChild(nil, O.Caption);
    N.Data:= O;
    LoadNextLevel(N); //Populates child nodes by iterating through master list again
  end;
end;

And a similar method for each additional level.

PS - The first level of hierarchy is populated from a separate list of its own (about 50 objects) whereas the second and third levels are populated from the properties of these many thousands of objects in a master list. This is why the first level loads quickly, and the rest is slow.

like image 873
Jerry Dodge Avatar asked Nov 13 '13 01:11

Jerry Dodge


1 Answers

If you really care about speed of populating a massive treeview, you should look into virtualTreeView (http://code.google.com/p/virtual-treeview/).
It's an open source treeview specifically designed to be virtual and maximize speed/memory for large treeviews.
It's an amazing component.

like image 143
Dave Novo Avatar answered Oct 05 '22 02:10

Dave Novo