4
4
5
5
This chapter is based on the explanation given by Niko Matsakis in this
6
6
[ video] ( https://www.youtube.com/watch?v=_muY4HjSqVw ) about
7
- [ ` Salsa ` ] ( https://github.com/salsa-rs/salsa ) . To find out more you may
8
- want to watch [ ` Salsa ` In More
7
+ [ Salsa] ( https://github.com/salsa-rs/salsa ) . To find out more you may
8
+ want to watch [ Salsa In More
9
9
Depth] ( https://www.youtube.com/watch?v=i_IhACacPRY ) , also by Niko
10
10
Matsakis.
11
11
12
- > As of <!-- date-check --> November 2022, although ` Salsa ` is inspired by (among
13
- > other things) ` rustc ` 's query system, it is not used directly in ` rustc ` . It
14
- > _ is_ used in [ chalk] , an implementation of Rust's ` trait ` system, and
12
+ > As of <!-- date-check --> November 2022, although Salsa is inspired by (among
13
+ > other things) rustc's query system, it is not used directly in rustc. It
14
+ > _ is_ used in [ chalk] , an implementation of Rust's trait system, and
15
15
> extensively in [ ` rust-analyzer ` ] , the official implementation of the language
16
16
> server protocol for Rust, but there are no medium or long-term concrete
17
17
> plans to integrate it into the compiler.
@@ -21,46 +21,46 @@ Matsakis.
21
21
22
22
## What is Salsa?
23
23
24
- ` Salsa ` is a library for incremental recomputation. This means it allows reusing
24
+ Salsa is a library for incremental recomputation. This means it allows reusing
25
25
computations that were already done in the past to increase the efficiency
26
26
of future computations.
27
27
28
- The objectives of ` Salsa ` are:
28
+ The objectives of Salsa are:
29
29
* Provide that functionality in an automatic way, so reusing old computations
30
30
is done automatically by the library.
31
31
* Doing so in a "sound", or "correct", way, therefore leading to the same
32
32
results as if it had been done from scratch.
33
33
34
- ` Salsa ` 's actual model is much richer, allowing many kinds of inputs and many
35
- different outputs. For example, integrating ` Salsa ` with an IDE could mean that
34
+ Salsa's actual model is much richer, allowing many kinds of inputs and many different outputs.
35
+ For example, integrating Salsa with an IDE could mean that
36
36
the inputs could be manifests (` Cargo.toml ` , ` rust-toolchain.toml ` ), entire
37
37
source files (` foo.rs ` ), snippets and so on. The outputs of such an integration
38
38
could range from a binary executable, to lints, types (for example, if a user
39
39
selects a certain variable and wishes to see its type), completions, etc.
40
40
41
41
## How does it work?
42
42
43
- The first thing that ` Salsa ` has to do is identify the "base inputs" that
43
+ The first thing that Salsa has to do is identify the "base inputs" that
44
44
are not something computed but given as input.
45
45
46
- Then ` Salsa ` has to also identify intermediate, "derived" values, which are
46
+ Then Salsa has to also identify intermediate, "derived" values, which are
47
47
something that the library produces, but, for each derived value there's a
48
48
"pure" function that computes the derived value.
49
49
50
50
For example, there might be a function ` ast(x: Path) -> AST ` . The produced
51
51
Abstract Syntax Tree (` AST ` ) isn't a final value, it's an intermediate value
52
52
that the library would use for the computation.
53
53
54
- This means that when you try to compute with the library, ` Salsa ` is going to
54
+ This means that when you try to compute with the library, Salsa is going to
55
55
compute various derived values, and eventually read the input and produce the
56
56
result for the asked computation.
57
57
58
- In the course of computing, ` Salsa ` tracks which inputs were accessed and which
58
+ In the course of computing, Salsa tracks which inputs were accessed and which
59
59
values are derived. This information is used to determine what's going to
60
60
happen when the inputs change: are the derived values still valid?
61
61
62
62
This doesn't necessarily mean that each computation downstream from the input
63
- is going to be checked, which could be costly. ` Salsa ` only needs to check each
63
+ is going to be checked, which could be costly. Salsa only needs to check each
64
64
downstream computation until it finds one that isn't changed. At that point, it
65
65
won't check other derived computations since they wouldn't need to change.
66
66
@@ -76,7 +76,7 @@ J <- B <--+
76
76
77
77
When an input ` I ` changes, the derived value ` A ` could change. The derived
78
78
value ` B ` , which does not depend on ` I ` , ` A ` , or any value derived from ` A ` or
79
- ` I ` , is not subject to change. Therefore, ` Salsa ` can reuse the computation done
79
+ ` I ` , is not subject to change. Therefore, Salsa can reuse the computation done
80
80
for ` B ` in the past, without having to compute it again.
81
81
82
82
The computation could also terminate early. Keeping the same graph as before,
@@ -86,18 +86,18 @@ computation. This leads to an "early termination", because there's no need to
86
86
check if ` C ` needs to change, since both ` C ` direct inputs, ` A ` and ` B ` ,
87
87
haven't changed.
88
88
89
- ## Key ` Salsa ` concepts
89
+ ## Key Salsa concepts
90
90
91
91
### Query
92
92
93
- A query is some value that ` Salsa ` can access in the course of computation. Each
93
+ A query is some value that Salsa can access in the course of computation. Each
94
94
query can have a number of keys (from 0 to many), and all queries have a
95
95
result, akin to functions. ` 0-key ` queries are called "input" queries.
96
96
97
97
### Database
98
98
99
99
The database is basically the context for the entire computation, it's meant to
100
- store ` Salsa ` 's internal state, all intermediate values for each query, and
100
+ store Salsa's internal state, all intermediate values for each query, and
101
101
anything else that the computation might need. The database must know all the
102
102
queries the library is going to do before it can be built, but they don't need
103
103
to be specified in the same place.
@@ -116,14 +116,14 @@ potentially invalidated.
116
116
117
117
A query group is a set of queries which have been defined together as a unit.
118
118
The database is formed by combining query groups. Query groups are akin to
119
- "` Salsa ` modules".
119
+ "Salsa modules".
120
120
121
- A set of queries in a query group are just a set of methods in a ` trait ` .
121
+ A set of queries in a query group are just a set of methods in a trait.
122
122
123
- To create a query group a ` trait ` annotated with a specific attribute
123
+ To create a query group a trait annotated with a specific attribute
124
124
(` #[salsa::query_group(...)] ` ) has to be created.
125
125
126
- An argument must also be provided to said attribute as it will be used by ` Salsa `
126
+ An argument must also be provided to said attribute as it will be used by Salsa
127
127
to create a ` struct ` to be used later when the database is created.
128
128
129
129
Example input query group:
@@ -166,7 +166,7 @@ pub trait Parser: Inputs {
166
166
167
167
When creating a derived query the implementation of said query must be defined
168
168
outside the trait. The definition must take a database parameter as an `impl
169
- Trait` (or ` dyn Trait` ), where ` Trait` is the query group that the definition
169
+ Trait` (or ` dyn Trait`), where trait is the query group that the definition
170
170
belongs to, in addition to the other keys.
171
171
172
172
``` rust,ignore
0 commit comments