Skip to content

Commit

Permalink
[GTK] Tree optimization: speedup TreeItem constructor by using known
Browse files Browse the repository at this point in the history
iterator eclipse-platform#882

This change eliminates a single hotspot. The overall complexity of
tree building and element reveal is still quadratic.

Performance improvement is proven by running
org.eclipse.swt.tests.junit.performance.Test_org_eclipse_swt_widgets_Tree.jfaceReveal()

In test jfaceReveal[Shape: STAR, virtual: true]:
10_000 elements: 620_682_717ns -> 501_378_662ns
100_000 elements: 62_804_214_489ns -> 52_781_899_817ns (-16%)
  • Loading branch information
basilevs authored and SyntevoAlex committed Nov 28, 2023
1 parent a213c88 commit 16d0ae0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,22 @@ TreeItem _getItem (long iter) {
parentIter = OS.g_malloc (GTK.GtkTreeIter_sizeof ());
GTK.gtk_tree_model_get_iter (modelHandle, parentIter, path);
}
items [id] = new TreeItem (this, parentIter, SWT.NONE, indices [indices.length -1], false);
items [id] = new TreeItem (this, parentIter, SWT.NONE, indices [indices.length -1], iter);
GTK.gtk_tree_path_free (path);
if (parentIter != 0) OS.g_free (parentIter);
return items [id];
}

TreeItem _getItem (long parentIter, int index) {
long iter = OS.g_malloc (GTK.GtkTreeIter_sizeof ());
GTK.gtk_tree_model_iter_nth_child(modelHandle, iter, parentIter, index);
int id = getId (iter, true);
OS.g_free (iter);
if (items [id] != null) return items [id];
return items [id] = new TreeItem (this, parentIter, SWT.NONE, index, false);
try {
GTK.gtk_tree_model_iter_nth_child (modelHandle, iter, parentIter, index);
int id = getId (iter, true);
if (items [id] != null) return items [id];
return items [id] = new TreeItem (this, parentIter, SWT.NONE, index, iter);
} finally {
OS.g_free (iter);
}
}

void reallocateIds(int newSize) {
Expand Down Expand Up @@ -3532,7 +3535,7 @@ void setItemCount (long parentIter, int count) {
OS.g_free (iters);
} else {
for (int i=itemCount; i<count; i++) {
new TreeItem (this, parentIter, SWT.NONE, itemCount, true);
new TreeItem (this, parentIter, SWT.NONE, itemCount, 0);
}
}
if (!isVirtual) setRedraw (true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class TreeItem extends Item {
* @see Widget#getStyle
*/
public TreeItem (Tree parent, int style) {
this (checkNull (parent), 0, style, -1, true);
this (checkNull (parent), 0, style, -1, 0);
}

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ public TreeItem (Tree parent, int style) {
* @see Tree#setRedraw
*/
public TreeItem (Tree parent, int style, int index) {
this (checkNull (parent), 0, style, checkIndex (index), true);
this (checkNull (parent), 0, style, checkIndex (index), 0);
}

/**
Expand All @@ -129,7 +129,7 @@ public TreeItem (Tree parent, int style, int index) {
* @see Widget#getStyle
*/
public TreeItem (TreeItem parentItem, int style) {
this (checkNull (parentItem).parent, parentItem.handle, style, -1, true);
this (checkNull (parentItem).parent, parentItem.handle, style, -1, 0);
}

/**
Expand Down Expand Up @@ -158,17 +158,19 @@ public TreeItem (TreeItem parentItem, int style) {
* @see Tree#setRedraw
*/
public TreeItem (TreeItem parentItem, int style, int index) {
this (checkNull (parentItem).parent, parentItem.handle, style, checkIndex (index), true);
this (checkNull (parentItem).parent, parentItem.handle, style, checkIndex (index), 0);
}

TreeItem (Tree parent, long parentIter, int style, int index, boolean create) {
TreeItem (Tree parent, long parentIter, int style, int index, long iter) {
super (parent, style);
this.parent = parent;
if (create) {
if (iter == 0) {
parent.createItem (this, parentIter, index);
} else {
assert handle == 0;
handle = OS.g_malloc (GTK.GtkTreeIter_sizeof ());
GTK.gtk_tree_model_iter_nth_child (parent.modelHandle, handle, parentIter, index);
if (handle == 0) error(SWT.ERROR_NO_HANDLES);
C.memmove(handle, iter, GTK.GtkTreeIter_sizeof ());
}
}

Expand Down

0 comments on commit 16d0ae0

Please sign in to comment.