Skip to content

TreeSitter Performance And Stability#263

Merged
thecoolwinter merged 11 commits intoCodeEditApp:mainfrom
thecoolwinter:fix/tree-sitter-crashes
Sep 8, 2024
Merged

TreeSitter Performance And Stability#263
thecoolwinter merged 11 commits intoCodeEditApp:mainfrom
thecoolwinter:fix/tree-sitter-crashes

Conversation

@thecoolwinter
Copy link
Collaborator

@thecoolwinter thecoolwinter commented Sep 4, 2024

Description

Stabilizes the highlighting and editing experience for large documents and slow tree-sitter languages. All async operations run using an updated execution manager that is safer, faster, and much easier to use.

Safety Improvements:

  • Explicitly performs getting text for tree-sitter on the main thread.
  • Parses on the main thread, timing out every few ms to avoid clogging up the main thread.
  • To avoid potential corrupted tree-sitter state, the state object is now copied for each edit and applied to the client at the end of the edit. If the edit operation is canceled, the half-parsed state is thrown away.
  • HighlightProviding now has @MainActor marked callbacks and protocol required functions. In async contexts these will throw a compiler error if not called on the main thread.

Performance Improvements:

  • If running asynchronously, tree-sitter edits cancel all previous edits. If an edit is canceled, the edit is added to an atomic queue. The next edit that isn't cancelled will pick up and apply all the queued edits.
    • This causes a massive performance improvement as tree-sitter's parser gets very stuck if the text doesn't match the tree-sitter tree. By keeping the text and edits in sync we reduce edit parse time drastically.
  • Instead of using a serial dispatch queue, the executor now uses Swift's shared thread pool via Tasks. On top of that, because we're controlling when tasks execute in a queue, operations that access the tree-sitter tree can now run in parallel.

Highlighter Changes:

  • The HighlightProviding callbacks now return a Result object. If the result is a failure and returns a cancelled error, the highlighter now re-invalidates the queried ranges. This means when highlights are cancelled because of some other async operation, they are always eventually fulfilled.
  • The highlighter now logs errors from it's providers.

TreeSitter Execution:

  • Operations make use of Swift Tasks to execute, allowing us to use task cancellation, priority, etc.
  • Operations are now cancellable by priority, so reset operations can cancel all edits, highlights and resets, edits can cancel all edits and highlights, etc.
  • Cancelling an operation now has many more checks to ensure cancelled tasks don't perform extra work (while parsing, before starting an operation, while waiting in the queue).

Related Issues

  • N/A

Checklist

  • I read and understood the contributing guide as well as the code of conduct
  • The issues this PR addresses are related to each other
  • My changes generate no new warnings
  • My code builds and runs on my machine
  • My changes are all related to the related issue above
  • I documented my code

Screenshots

Demo: Writing a simple C program with main, a string, and a few keywords in a large C file.
These use sqlite3.c for demos. It's just a large C file that I often use for performance demos.

Current editing experience. Note incorrect highlights, extremely slow highlighting and maxed thread use.

Screen.Recording.2024-09-04.at.2.54.16.PM.mov

New editing experience for large files, with metrics:

Screen.Recording.2024-09-04.at.2.51.01.PM.mov

austincondiff
austincondiff previously approved these changes Sep 7, 2024
0xWDG
0xWDG previously approved these changes Sep 7, 2024
tom-ludwig
tom-ludwig previously approved these changes Sep 7, 2024
@thecoolwinter thecoolwinter merged commit 7d08e74 into CodeEditApp:main Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants