Skip to content

Commit

Permalink
Prototype item count cache in TreeItem eclipse-platform#822
Browse files Browse the repository at this point in the history
Add cache invalidation only to obvious places for now.
  • Loading branch information
basilevs committed Nov 11, 2023
1 parent a362620 commit 999ba74
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class TreeItem extends Item {
Font[] cellFont;
String [] strings;
boolean cached, grayed, isExpanded, updated, settingData;
int itemCountCache = -1;
static final int EXPANDER_EXTRA_PADDING = 4;

/**
Expand Down Expand Up @@ -166,6 +167,7 @@ public TreeItem (TreeItem parentItem, int style, int index) {
this.parent = parent;
if (create) {
parent.createItem (this, parentIter, index);
resetParentCache();
} else {
handle = OS.g_malloc (GTK.GtkTreeIter_sizeof ());
GTK.gtk_tree_model_iter_nth_child (parent.modelHandle, handle, parentIter, index);
Expand Down Expand Up @@ -768,7 +770,7 @@ Rectangle getImageBoundsInPixels (int index) {
public int getItemCount () {
checkWidget();
if (!parent.checkData (this)) error (SWT.ERROR_WIDGET_DISPOSED);
return GTK.gtk_tree_model_iter_n_children (parent.modelHandle, handle);
return getItemCountImpl();
}

/**
Expand All @@ -791,8 +793,7 @@ public int getItemCount () {
public TreeItem getItem (int index) {
checkWidget();
if (index < 0) error (SWT.ERROR_INVALID_RANGE);
if (!parent.checkData (this)) error (SWT.ERROR_WIDGET_DISPOSED);
int itemCount = GTK.gtk_tree_model_iter_n_children (parent.modelHandle, handle);
int itemCount = getItemCountImpl();
if (index >= itemCount) error (SWT.ERROR_INVALID_RANGE);
return parent._getItem (handle, index);
}
Expand Down Expand Up @@ -1099,6 +1100,7 @@ public void dispose () {
*/
public void removeAll () {
checkWidget ();
itemCountCache = 0;
long modelHandle = parent.modelHandle;
int length = GTK.gtk_tree_model_iter_n_children (modelHandle, handle);
if (length == 0) return;
Expand Down Expand Up @@ -1629,6 +1631,7 @@ public void setImage (Image [] images) {
public void setItemCount (int count) {
checkWidget ();
count = Math.max (0, count);
itemCountCache = count;
parent.setItemCount (handle, count);
}

Expand Down Expand Up @@ -1708,4 +1711,19 @@ public void setText (String [] strings) {
if (string != null) setText (i, string);
}
}

private void resetParentCache() {
TreeItem parentItem = getParentItem();
if (parentItem != null) {
parentItem.itemCountCache = -1;
}
}

private int getItemCountImpl() {
if (itemCountCache < 0) {
itemCountCache = GTK.gtk_tree_model_iter_n_children (parent.modelHandle, handle);
}
return itemCountCache;
}

}

0 comments on commit 999ba74

Please sign in to comment.