[GTK] Cache Tree and TreeItem child counts - #3664
Merged
vogella merged 1 commit intoOct 5, 2026
Merged
Conversation
gtk_tree_model_iter_n_children walks the sibling list, so every getItemCount() call was linear in the number of children. A counter bumped on each row insertion or removal now guards lazily cached counts, which makes repeated getItemCount() calls constant time. getItem(int) and indexOf still use linear GTK lookups, so loops over all children remain quadratic. Part of eclipse-platform#882 Assisted-by: multiple AI agents and layers of automated tooling 🤖
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
All GTK model mutation paths reviewed correctly invalidate the caches, with comprehensive regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Caches GTK Tree child counts to make repeated getItemCount() calls constant time.
Changes:
- Adds root and per-item child-count caches.
- Invalidates caches on structural mutations.
- Adds cache-invalidation tests.
| File | Description |
|---|---|
Tree.java |
Adds root caching and structural versioning. |
TreeItem.java |
Adds per-item caching and invalidation. |
Test_org_eclipse_swt_widgets_Tree.java |
Tests mutation and virtual-tree scenarios. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On GTK, Tree.getItemCount() and TreeItem.getItemCount() call gtk_tree_model_iter_n_children, which walks the sibling list on every call. This caches the counts behind a counter that is bumped on every row insertion or removal, so repeated calls become constant time: 40k getItemCount() calls on a 40k-child item drop from 7 to 12 s to about 1 ms, and a reveal-like loop of getItemCount() plus getItem(i) gets 3 to 5 times faster.
This is a partial fix for #882, the same idea as #883: getItem(int) and indexOf still use linear GTK lookups, so loops over all children stay quadratic until children are tracked on the Java side. New tests cover the invalidation on insert, dispose, setItemCount, removeAll and column changes.