Hash tree paths incrementally when restoring the expansion state - #4247
Open
vogella wants to merge 2 commits into
Open
Hash tree paths incrementally when restoring the expansion state#4247vogella wants to merge 2 commits into
vogella wants to merge 2 commits into
Conversation
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
Contributor
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.
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 whosehashCoderecurses over its children, as an LSPDocumentSymboldoes, 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
internalSetExpandedTreePathsnow 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.CustomHashtablekeeps 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: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
IElementComparerwas already cheap, so this helps the clients that have not set one, which is the default.