Skip to content

Commit

Permalink
Do not iterate in constructor on TreeItem eclipse-platform#882
Browse files Browse the repository at this point in the history
This  first step eliminates a single hotspot. The overall complexity of
tree building is still quadratic.
  • Loading branch information
basilevs committed Nov 26, 2023
1 parent 58c0570 commit 9ff75d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,23 @@ 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);
long iter = OS.g_malloc(GTK.GtkTreeIter_sizeof());
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 +3536,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 9ff75d4

Please sign in to comment.