You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/_posts/2020-07-20-three-architectures-for-responsive-ide.adoc
+37-37Lines changed: 37 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -18,24 +18,24 @@ Specifically, we'll look at the backbone infrastructure of an IDE which serves t
18
18
19
19
== Map Reduce
20
20
21
-
The first architecture is reminiscent of map-reduce paradigm.
21
+
The first architecture is reminiscent of the map-reduce paradigm.
22
22
The idea is to split analysis into relatively simple indexing phase, and a separate full analysis phase.
23
23
24
24
The core constraint of indexing is that it runs on a per-file basis.
25
-
The indexer takes a text of a single file, parses it, and spits out some data about the file.
25
+
The indexer takes the text of a single file, parses it, and spits out some data about the file.
26
26
The indexer can't touch other files.
27
27
28
28
Full analysis can read other files, and it leverages information from the index to save work.
29
29
30
30
This all sounds way too abstract, so let's look at a specific example -- Java.
31
31
In Java, each file starts with a package declaration.
32
32
The indexer concatenates the name of the package with a class name to get a fully-qualified name (FQN).
33
-
It also collects the set of method declared in the class, the list of superclasses and interfaces, etc.
33
+
It also collects the set of methods declared in the class, the list of superclasses and interfaces, etc.
34
34
35
35
Per-file data is merged into an index which maps FQNs to classes.
36
36
Note that constructing this mapping is an embarrassingly parallel task -- all files are parsed independently.
37
37
Moreover, this map is cheap to update.
38
-
When a file change arrives, this file's contribution from the index is removed, the text of file is changed and the indexer runs on the new text and adds new contributions.
38
+
When a file change arrives, this file's contribution from the index is removed, the text of the file is changed and the indexer runs on the new text and adds the new contributions.
39
39
The amount of work to do is proportional to the number of changed files, and is independent from the total number of files.
40
40
41
41
Let's see how FQN index can be used to quickly provide completion.
@@ -72,27 +72,27 @@ public class Main {
72
72
73
73
The user has just typed `Foo.f().`, and we need to figure out that the type of receiver expression is `Bar`, and suggest `g` as a completion.
74
74
75
-
First, as the `Main.java` file is modified, we run the indexer on this single file.
76
-
Nothing has changed (the file still contains `Main` class with static `main` method), so we don't need to update FQN index.
75
+
First, as the file `Main.java` is modified, we run the indexer on this single file.
76
+
Nothing has changed (the file still contains the class `Main` with a static `main` method), so we don't need to update the FQN index.
77
77
78
-
Next, we need to resolve `Foo` name.
79
-
We parse the file, notice an `import` and lookup `mypackage.Foo` in the FQN index.
78
+
Next, we need to resolve the name `Foo`.
79
+
We parse the file, notice an `import` and look up `mypackage.Foo` in the FQN index.
80
80
In the index, we also find that `Foo` has a static method `f`, so we resolve the call as well.
81
81
The index also stores the return type of `f`, but, and this is crucial, it stores it as a string `"Bar"`, and not as a direct reference to the class `Bar`.
82
82
83
83
The reason for that is `+import java.util.*+` in `Foo.java`.
84
84
`Bar` can refer either to `java.util.Bar` or to `mypackage.Bar`.
85
-
The indexer doesn't know which one, because it can look *only* at the text of the `Foo.java`.
85
+
The indexer doesn't know which one, because it can look *only* at the text of `Foo.java`.
86
86
In other words, while the index does store the return types of methods, it stores them in an unresolved form.
87
87
88
-
The next step is to resolve `Bar` identifier in the context of `Foo.java` file.
89
-
This uses FQN index, and lands into the `mypackage.Bar` class.
90
-
There the desired `g` method is found.
88
+
The next step is to resolve the identifier `Bar` in the context of `Foo.java`.
89
+
This uses the FQN index, and lands in the class `mypackage.Bar`.
90
+
There the desired method `g` is found.
91
91
92
-
All together, only three files were touched during completion.
93
-
FQN index allowed to completely ignore all the other files in the project.
92
+
Altogether, only three files were touched during completion.
93
+
The FQN index allowed us to completely ignore all the other files in the project.
94
94
95
-
One problem with the approach described thus far is that resolving types from the index requires non-trivial amount of work.
95
+
One problem with the approach described thus far is that resolving types from the index requires a non-trivial amount of work.
96
96
This work might be duplicated if, for example, `Foo.f` is called several times.
97
97
The fix is to add a cache.
98
98
Name resolution results are memoized, so that the cost is paid only once.
@@ -103,28 +103,28 @@ To sum up, the first approach works like this:
103
103
. Each file is being indexed, independently and in parallel, producing a "stub" -- a set of visible top-level declarations, with unresolved types.
104
104
. All stubs are merged into a single index data structure.
105
105
. Name resolution and type inference work primarily off the stubs.
106
-
. Name resolution is lazy (we only resolved type from the stub when we need it) and memoized (each type is resolved only once).
106
+
. Name resolution is lazy (we only resolve a type from the stub when we need it) and memoized (each type is resolved only once).
107
107
. The caches are completely invalidated on every change
108
108
. The index is updated incrementally:
109
-
* if the edit doesn't change file's stub, no change to the index is required.
109
+
* if the edit doesn't change the file's stub, no change to the index is required.
110
110
* otherwise, old keys are removed and new keys are added
111
111
112
-
Note an interesting interplay between "dumb" indexes which can be incrementally updated, and "smart" caches, which are re-computed from scratch.
112
+
Note an interesting interplay between "dumb" indexes which can be updated incrementally, and "smart" caches, which are re-computed from scratch.
113
113
114
114
This approach combines simplicity and stellar performance.
115
115
The bulk of work is the indexing phase, and you can parallelize and even distribute it across several machine.
116
-
The two example of this architecture are https://www.jetbrains.com/idea/[IntelliJ] and https://sorbet.org/[Sorbet].
116
+
Two examples of this architecture are https://www.jetbrains.com/idea/[IntelliJ] and https://sorbet.org/[Sorbet].
117
117
118
118
The main drawback of this approach is that it works only when it works -- not every language has a well-defined FQN concept.
119
-
I think overall it's a good idea to design name resolution and module system (mostly boring parts of the language) such that they work well with map-reduce paradigm.
119
+
I think overall it's a good idea to design name resolution and module systems (mostly boring parts of a language) such that they work well with the map-reduce paradigm.
120
120
121
-
* Require `package` declarations or infer them from a file-system layout
121
+
* Require `package` declarations or infer them from the file-system layout
122
122
* Forbid meta-programming facilities which add new top-level declarations, or restrict them in such way that they can be used by the indexer.
123
123
For example, preprocessor-like compiler plugins that access a single file at a time might be fine.
124
124
* Make sure that each source element corresponds to a single semantic element.
125
125
For example, if the language supports conditional compilation, make sure that it works during name resolution (like Kotlin's https://kotlinlang.org/docs/reference/platform-specific-declarations.html[expect/actual]) and not during parsing (like conditional compilation in most other languages).
126
126
Otherwise, you'd have to index the same file with different conditional compilation settings, and that is messy.
127
-
* Make sure that FQN are enough for most of the name resolution.
127
+
* Make sure that FQNs are enough for most of the name resolution.
128
128
129
129
The last point is worth elaborating. Let's look at the following Rust example:
130
130
@@ -158,12 +158,12 @@ However, to make sure that `s.f` indeed refers to `f` from `T`, we also need to
158
158
The second approach places even more restrictions on the language.
159
159
It requires:
160
160
161
-
* "declaration before use" rule,
161
+
* a "declaration before use" rule,
162
162
* headers or equivalent interface files.
163
163
164
164
Two such languages are {cpp} and OCaml.
165
165
166
-
The idea of the approach is simple -- just use traditional compiler, by snapshotting its state immediately after imports for each compilation unit.
166
+
The idea of the approach is simple -- just use a traditional compiler and snapshot its state immediately after imports for each compilation unit.
167
167
An example:
168
168
169
169
[source,c++]
@@ -175,11 +175,11 @@ void main() {
175
175
}
176
176
----
177
177
178
-
Here, the compiler fully processes `iostream` (and any further headers it includes), snapshots its state and proceeds with parsing the program itself.
178
+
Here, the compiler fully processes `iostream` (and any further headers included), snapshots its state and proceeds with parsing the program itself.
179
179
When the user types more characters, the compiler restarts from the point just after the include.
180
180
As the size of each compilation unit itself is usually reasonable, the analysis is fast.
181
181
182
-
If the user types something into the header file than the caches need to be invalidated.
182
+
If the user types something into the header file, then the caches need to be invalidated.
183
183
However, changes to headers are comparatively rare, most of the code lives in `.cpp` files.
184
184
185
185
In a sense, headers correspond to the stubs of the first approach, with two notable differences:
@@ -191,7 +191,7 @@ In a sense, headers correspond to the stubs of the first approach, with two nota
191
191
The two examples of this approach are https://github.com/ocaml/merlin[Merlin] of OCaml and https://clangd.llvm.org/[clangd].
192
192
193
193
The huge benefit of this approach is that it allows re-use of an existing batch compiler.
194
-
Two other approaches described in the article typically result in compiler re-writes.
194
+
The two other approaches described in this article typically result in compiler re-writes.
195
195
The drawback is that almost nobody likes headers and forward declarations.
196
196
197
197
@@ -231,8 +231,8 @@ bitflags! {
231
231
----
232
232
233
233
`bitflags` is macro which comes from another crate and defines a top-level declaration.
234
-
We can't put the results of macro expansion into the index, because it depends on macro definition in another file.
235
-
We can put macro call itself into an index, but that is mostly useless, as the items, declared by the macro, would miss the index.
234
+
We can't put the results of macro expansion into the index, because it depends on a macro definition in another file.
235
+
We can put the macro call itself into an index, but that is mostly useless, as the items, declared by the macro, would miss the index.
236
236
237
237
Here's another one:
238
238
@@ -245,14 +245,14 @@ mod bar;
245
245
----
246
246
247
247
Modules `foo` and `bar` refer to the same file, `foo.rs`, which effectively means that items from `foo.rs` are duplicated.
248
-
If `foo.rs` contains `struct S;` declaration, than `foo::S` and `bar::S` are different types.
248
+
If `foo.rs` contains the declaration `struct S;`, then `foo::S` and `bar::S` are different types.
249
249
You also can't fit that into an index, because those `mod` declarations are in a different file.
250
250
251
251
The second approach doesn't work either.
252
252
In {cpp}, the compilation unit is a single file.
253
253
In Rust, the compilation unit is a whole crate, which consists of many files and is typically much bigger.
254
-
And Rust has procedural macros, which means that even surface analysis of code can take unbounded amount of time.
255
-
And there are no header files, so IDE has to process the whole crate.
254
+
And Rust has procedural macros, which means that even surface analysis of code can take an unbounded amount of time.
255
+
And there are no header files, so the IDE has to process the whole crate.
256
256
Additionally, intra-crate name resolution is much more complicated (declaration before use vs. fixed point iteration intertwined with macro expansion).
257
257
258
258
It seems that purely laziness based models do not work for Rust.
@@ -262,22 +262,22 @@ For this reason, in rust-analyzer we resort to a smart solution.
262
262
We compensate for the deficit of laziness with incrementality.
263
263
Specifically, we use a generic framework for incremental computation -- https://github.com/salsa-rs/salsa[salsa].
264
264
265
-
The idea behind salsa is rather simple -- all function calls inside the compiler are instrumented to record which other functions were called during execution.
265
+
The idea behind salsa is rather simple -- all function calls inside the compiler are instrumented to record which other functions were called during their execution.
266
266
The recorded traces are used to implement fine-grained incrementality.
267
267
If after modification the results of all of the dependencies are the same, the old result is reused.
268
268
269
269
270
270
There's also an additional, crucial, twist -- if a function is re-executed due to a change in dependency, the new result is compared with the old one.
271
271
If despite a different input they are the same, the propagation of invalidation stops.
272
272
273
-
Using this engine, we were able to implement rather fancy update strategy.
274
-
Unlike map reduce approach, our indices can store resolved types, which are invalidated only when top-level change occurs.
273
+
Using this engine, we were able to implement a rather fancy update strategy.
274
+
Unlike the map reduce approach, our indices can store resolved types, which are invalidated only when a top-level change occurs.
275
275
Even after a top-level change, we are able to re-use results of most macro expansions.
276
-
And typing inside top-level macro also doesn't invalidate caches unless the expansion of the macro introduces a different set of items.
276
+
And typing inside of a top-level macro also doesn't invalidate caches unless the expansion of the macro introduces a different set of items.
277
277
278
278
The main benefit of this approach is generality and correctness.
279
279
If you have an incremental computation engine at your disposal, it becomes relatively easy to experiment with the way you structure the computation.
280
-
The code looks mostly like a boring imperative compiler, and you are immune from cache invalidation bugs (we had one, due to procedural macro being non-deterministic).
280
+
The code looks mostly like a boring imperative compiler, and you are immune to cache invalidation bugs (we had one, due to procedural macros being non-deterministic).
281
281
282
282
The main drawback is extra complexity, slower performance (fine-grained tracking of dependencies takes time and memory) and a feeling that this is a somewhat uncharted territory yet :-)
Copy file name to clipboardExpand all lines: thisweek/_posts/2020-07-06-changelog-32.adoc
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ This week we'd like to thank one of our individual sponsors, https://github.com/
12
12
@anp maintains https://moxie.rs[moxie] project: a lightweight platform-agnostic declarative UI runtime, because incremental feedback latency and quality are core to building interactive software.
13
13
14
14
15
-
**Become a sponsor:** https://opencollective.com/rust-analyzer/[opecollective.com/rust-analyzer]
15
+
**Become a sponsor:** https://opencollective.com/rust-analyzer/[opencollective.com/rust-analyzer]
0 commit comments