Commit 2a39fd0
authored
Async TreeSitterClient (#191)
### Description
Modifies `TreeSitterClient` to support doing work on a background thread
when doing so is beneficial. This allows the editor to display content
while languages load, scroll smoothly while highlight queries perform in
the background, and on large documents perform edits asynchronously to
keep UI responsive.
Edits and highlights are only queued for background work when:
- An edit takes too long (`Constants.parserTimeout`) and a parser times
out.
- An edit or highlight is too large (`Constants.maxSyncEditLength`,
`Constants.maxSyncQueryLength`)
- The document is large enough to make tree-sitter slow no matter what
(`Constants.maxSyncContentLength`)
- There are async jobs already being performed.
Changes:
- Created a `TreeSitterState` object that handles managing the language
layer tree (language layers renamed to `LanguageLayer`)
- Handles setting and resetting languages
- Manages injection layers.
- Added `PthreadLock` for efficient, thread-safe locking via a wrapped
`pthread_mutex`.
- **TreeSitterClient**
- Simplified TreeSitterClient.swift to only contain async methods,
initialization and configuration, and HighlightProviding conformance
code.
- Created async versions of `applyEdit` and `queryHighlightsFor`
- Added two queues: `queuedEdits` and `queuedQueries` to hold any
enqueued edits and queries.
- Added two locks:
- `stateLock`: Must be acquired before performing any actions on the
state object
- `queueLock`: Must be acquired before modifying the previously
mentioned queues.
- Added private methods for starting and stopping the background task in
`runningTask`:
- `beginTasksIfNeeded` starts the background task if there is any
outstanding work and there is no existing task.
- `determineNextJob` dequeues a job(s) to perform in the background
task. Handles job ordering and greedily dequeues highlight jobs as
determined by `Constants.simultaneousHighlightLimit`
- `cancelAllRunningTasks` cancels the running task and removes all
queued jobs.
### Related Issues
- #132 - This PR is half of this issue. The other half has to do with
text editing performance which this PR does not change.
### Checklist
<!--- Add things that are not yet implemented above -->
- [x] I read and understood the [contributing
guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md)
as well as the [code of
conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md)
- [x] The issues this PR addresses are related to each other
- [x] My changes generate no new warnings
- [x] My code builds and runs on my machine
- [x] My changes are all related to the related issue above
- [x] I documented my code
### Screenshots
Async language loading. Swift has a terrible initial loading time and
this recording shows that the document now opens instantly but performs
the initial work on a background thread. *Note that this initial loading
time is nearly nonexistent in subsequent document loads due to the
queries being cached.*
Previously, this would have blocked the main thread for the entire
duration of the language load (up to 5s in my testing).
https://github.com/CodeEditApp/CodeEditTextView/assets/35942988/d7feb9ae-0846-4ad3-8598-17f7b6d753e0
Async highlighting means extremely long documents can load and highlight
without affecting displaying and scrolling. This is still slightly
"hitch"-y due to NSTextStorage performance when applying highlight data
to the text view.
https://github.com/CodeEditApp/CodeEditTextView/assets/35942988/affcaf41-d9db-4210-80f1-cb972c5060f21 parent 1dfd0b5 commit 2a39fd0
File tree
19 files changed
+972
-477
lines changed- Sources/CodeEditTextView
- Controller
- Enums
- Extensions
- STTextView+
- Highlighting
- TreeSitter
- Tests/CodeEditTextViewTests
19 files changed
+972
-477
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
Lines changed: 31 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
32 | 36 | | |
33 | 37 | | |
34 | | - | |
35 | | - | |
36 | | - | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
38 | 61 | | |
39 | 62 | | |
40 | 63 | | |
| |||
Lines changed: 4 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 20 | + | |
24 | 21 | | |
| 22 | + | |
25 | 23 | | |
26 | | - | |
| 24 | + | |
27 | 25 | | |
28 | 26 | | |
29 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
83 | | - | |
84 | | - | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
125 | | - | |
| 124 | + | |
126 | 125 | | |
127 | 126 | | |
128 | 127 | | |
| |||
172 | 171 | | |
173 | 172 | | |
174 | 173 | | |
175 | | - | |
176 | 174 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 175 | + | |
181 | 176 | | |
182 | 177 | | |
| 178 | + | |
183 | 179 | | |
184 | 180 | | |
185 | 181 | | |
| |||
251 | 247 | | |
252 | 248 | | |
253 | 249 | | |
254 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
255 | 253 | | |
256 | 254 | | |
257 | 255 | | |
| |||
0 commit comments