Skip to content

Conversation

@guiherzog
Copy link
Contributor

Optimising getViewState tree traversal to improve performance on folders with too many children.

Context

We are currently running VSCode in a monorepo folder that has hundreds of thousands of subfolders/files. There are a few performance issues happening due to that and the getViewState has been identified as one of them.

Problem

Whenever a folder was opened, such as branches (88K children), the UI would freeze when opening new folders, changing tabs and also every few seconds. The reason for that was because the onWillSaveWorkspace is called every few secs and whenever VSCode becomes the active tab to save the list of folders that are expanded in storage so the user can refresh the page and still have their expanded folders open.

The issue was that the getViewState() function that returns a list of expanded folders was traversing the tree too slowly.

Solution

In this case, it does not matter by which order we traverse the tree to get all states. Array.shift() has O(n) performance while Array.pop() has O(1), therefore, changing it to pop makes it N times faster.

In the image below you can see that after changing from BFS traversal to DFS, the function improved from an average of 4.5 seconds to 4 milliseconds for the monorepo that contains a folder with a 100K children.

Before:
Benchmark before optimisation ~4.6s

After:
Benchmark after optimisation ~4ms

@guiherzog guiherzog changed the title Improving performance of tree traversal getViewState function Improving performance of tree traversal on getViewState function Jan 29, 2021
@kieferrm kieferrm requested a review from joaomoreno January 29, 2021 18:46
@joaomoreno
Copy link
Member

joaomoreno commented Feb 1, 2021

You get:

  • 5 ⭐ for the context, problem & solution description
  • 5 ⭐ for the solution
  • 5 ⭐ for renaming queue to stack

Fantastic observation and fix, thanks! 🚀 🍻

@joaomoreno joaomoreno added this to the February 2021 milestone Feb 1, 2021
@joaomoreno joaomoreno added the tree-widget Tree widget issues label Feb 1, 2021
@joaomoreno joaomoreno merged commit a9b9890 into microsoft:master Feb 1, 2021
@guiherzog
Copy link
Contributor Author

Thank you for the compliments!
I am glad it's merged.

plainerman pushed a commit to plainerman/vscode that referenced this pull request Feb 2, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tree-widget Tree widget issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants