forked from eclipse-platform/eclipse.platform.swt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Amortize quadratic execution time of traversal eclipse-platform#882
TreeItem.getItem(int) had linear complexity, full traversal had quadratic complexity. To alleviate this, we cache the result. Note: this only speeds up breadth-first traversal as cache operates on "item of interest" basis, dropping contents if another item is requested.
- Loading branch information
Showing
3 changed files
with
76 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItemCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2023 IBM Corporation and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Vasili Gulevich - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.swt.widgets; | ||
|
||
import java.util.*; | ||
|
||
import org.eclipse.swt.internal.gtk.*; | ||
|
||
/** Volatile information about a TreeItem, that takes long to compute | ||
* @since 3.125**/ | ||
class TreeItemCache { | ||
final TreeItem owner; | ||
private TreeItem[] children; | ||
private int itemCount = -1; | ||
|
||
TreeItemCache(TreeItem owner) { | ||
this.owner = Objects.requireNonNull(owner); | ||
} | ||
|
||
int getItemCount() { | ||
if (itemCount < 0) { | ||
itemCount = GTK.gtk_tree_model_iter_n_children (owner.parent.modelHandle, owner.handle); | ||
} | ||
return itemCount; | ||
} | ||
|
||
TreeItem[] getItems() { | ||
if (children == null) { | ||
children = owner.parent.getItems(owner.handle); | ||
itemCount = children.length; | ||
} | ||
return children; | ||
} | ||
|
||
public void reset() { | ||
itemCount = -1; | ||
children = null; | ||
} | ||
} |