Skip to content

Hash tree paths incrementally when restoring the expansion state - #4247

Open
vogella wants to merge 2 commits into
eclipse-platform:masterfrom
vogella:jface/tree-expansion-hashing
Open

Hash tree paths incrementally when restoring the expansion state#4247
vogella wants to merge 2 commits into
eclipse-platform:masterfrom
vogella:jface/tree-expansion-hashing

Conversation

@vogella

@vogella vogella commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Stacked on #4246, which adds the tests this change is measured and guarded by. It carries that commit as its first commit, because a pull request from a fork cannot use a fork branch as its base. Review #4246 first; once it merges, the diff here collapses to the second commit alone.

Restoring the expansion state walks every item of the tree and looks each one up in a table keyed by TreePath. A tree path hashes all of its segments, so an element whose hashCode recurses over its children, as an LSP DocumentSymbol does, was hashed once for every descendant of every ancestor on its path. The cost of the walk grew with the square of the tree rather than with its size, which is what makes a large outline take seconds to refresh.

A tree path hashes as the sum of its segments, and a sum composes, so internalSetExpandedTreePaths now carries the parent's hash down the recursion and derives each child's from it with a single element hash. Once the set of paths to expand is exhausted the remaining items can only be collapsed, which needs neither a path nor its hash, so the walk switches to a plain collapse. CustomHashtable keeps the hash in its entries, which lets a lookup rule out an entry without comparing the keys and lets the table grow without hashing the keys again.

Measured with TreeViewerExpansionTest, recursive hash visits and the minimum of 100 restores:

shape before after
fully expanded, 156 of 1884 elements 361056 visits, 8.3 ms 44676 visits, 5.1 ms
2 of 1803 elements, all materialized 1087807 visits, 10.2 ms 4206 visits, 0.2 ms

The second shape, a large document with one container open, is where the old behaviour was worst: every item that could no longer match still paid a full path hash. A viewer with an IElementComparer was already cheap, so this helps the clients that have not set one, which is the default.

Restoring the expansion state by tree path had thin coverage. The one test
that exercised it, MultipleEqualElementsTreeViewerTest, ran without an element
comparer, although the comparer decides how a tree path hashes and compares,
and nothing checked what happens to the items the walk reaches after the last
path to expand has been found.

TreeViewerExpansionStateTest covers the round trip with and without a comparer,
restoring a subset and an empty set, restoring from equal but distinct elements
the way a content provider does after a refresh, and the state of the
containers the walk passes once its work is done.

The hash table a viewer keeps to map elements to items had no coverage of its
own at all. It is package private, so StructuredViewerElementMapTest reaches it
the way a client does, through a comparer that decides the hashes: several keys
in one slot, keys whose hashes differ but share a slot, a table grown well past
its initial capacity, and removal.

Also gives TreeViewerExpansionTest a second shape, a few large containers with
one of them expanded, which is where restoring is most expensive.
Restoring the expansion state walks every item of the tree and looks each one
up in a table keyed by TreePath. A tree path hashes all of its segments, so an
element whose hashCode is recursive over its children, as an LSP DocumentSymbol
is, was hashed once for every descendant of every ancestor on its path. The
cost of the walk grew with the square of the tree rather than with its size.

A tree path hashes as the sum of its segments, which composes, so
internalSetExpandedTreePaths now carries the parent's hash down the recursion
and derives each child's from it with a single element hash. Once the set of
paths to expand is exhausted the remaining items can only be collapsed, which
needs neither a path nor its hash, so the walk switches to a plain collapse.

CustomHashtable keeps the hash in its entries, which lets a lookup rule out an
entry without comparing the keys and lets the table grow without hashing the
keys again, and gains a remove that takes an already computed hash.

Measured with TreeViewerExpansionTest, recursive hash visits and the minimum of
100 restores:

  fully expanded, 156 of 1884 elements    361056 -> 44676 visits, 8.3 -> 5.1 ms
  2 of 1803 elements, all materialized   1087807 ->  4206 visits, 10.2 -> 0.2 ms
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

   858 files  ± 0     858 suites  ±0   49m 58s ⏱️ - 4m 36s
 8 168 tests +11   7 925 ✅ +11  243 💤 ±0  0 ❌ ±0 
20 406 runs  +33  19 752 ✅ +33  654 💤 ±0  0 ❌ ±0 

Results for commit f9f9c77. ± Comparison against base commit 61565e6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant