Skip to content

Commit

Permalink
[performance] faster Tree expand - eclipse.platform.swt#901
Browse files Browse the repository at this point in the history
* move setExpanded() after expanding the children
* insert every item at index 0 (in reverse order)

eclipse-platform/eclipse.platform.swt#901
  • Loading branch information
EcljpseB0T committed Nov 30, 2023
1 parent 918fd74 commit 726b26b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;

Expand Down Expand Up @@ -362,8 +363,9 @@ private void createAddedElements(Widget widget, Object[] elements) {

// Optimize for the empty case
if (items.length == 0) {
for (Object element : elements) {
createTreeItem(widget, element, -1);
// For performance insert every item at index 0 (in reverse order):
for (int i = elements.length - 1; i >= 0; i--) {
createTreeItem(widget, elements[i], 0);
}
return;
}
Expand Down Expand Up @@ -846,8 +848,9 @@ void createChildren(final Widget widget, boolean materialize) {
} else {
children = getSortedChildren(parentElement);
}
for (Object element : children) {
createTreeItem(widget, element, -1);
// For performance insert every item at index 0 (in reverse order):
for (int i = children.length - 1; i >= 0; i--) {
createTreeItem(widget, children[i], 0);
}
}
} finally {
Expand All @@ -856,16 +859,16 @@ void createChildren(final Widget widget, boolean materialize) {
}

/**
* Creates a single item for the given parent and synchronizes it with the
* given element.
* Creates a single item for the given parent and synchronizes it with the given
* element. The fastest way to insert many items is documented in
* {@link TreeItem#TreeItem(Tree,int,int)}
*
* @param parent
* the parent widget
* @param element
* the element
* @param index
* if non-negative, indicates the position to insert the item
* into its parent
* @param parent the parent widget
* @param element the element
* @param index if non-negative, indicates the position to insert the item
* into its parent
* @see org.eclipse.swt.widgets.TreeItem#TreeItem(org.eclipse.swt.widgets.Tree,
* int, int)
*/
protected void createTreeItem(Widget parent, Object element, int index) {
Item item = newItem(parent, SWT.NULL, index);
Expand Down Expand Up @@ -1831,9 +1834,6 @@ protected void internalExpandToLevel(Widget widget, int level) {
return;
}
createChildren(widget, false);
if (widget instanceof Item) {
setExpanded((Item) widget, true);
}
if (level == ALL_LEVELS || level > 1) {
Item[] children = getChildren(widget);
if (children != null) {
Expand All @@ -1844,6 +1844,10 @@ protected void internalExpandToLevel(Widget widget, int level) {
}
}
}
// for performance expand after creating children:
if (widget instanceof Item) {
setExpanded((Item) widget, true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,9 @@ void handleExpandableNodeClicked(Widget w) {
item.dispose();

// create children on parent
for (Object element : children) {
createTreeItem(parent, element, -1);
// For performance insert every item at index 0 (in reverse order):
for (int i = children.length - 1; i >= 0; i--) {
createTreeItem(parent, children[i], 0);
}

// If we've expanded but still have not reached the limit
Expand Down

0 comments on commit 726b26b

Please sign in to comment.