44
55This chapter is based on the explanation given by Niko Matsakis in this
66[ 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
99Depth] ( https://www.youtube.com/watch?v=i_IhACacPRY ) , also by Niko
1010Matsakis.
1111
12- > As of <!-- date-check --> November 2022, although Salsa is inspired by
13- > (among other things) rustc's query system, it is not used directly in rustc.
14- > It _ is_ used in [ chalk] , an implementation of Rust's trait system, and extensively in
15- > [ ` rust-analyzer ` ] , the official implementation of the language server protocol for Rust, but
16- > there are no medium or long-term concrete plans to integrate it into the
17- > compiler.
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+ > extensively in [ ` rust-analyzer ` ] , the official implementation of the language
16+ > server protocol for Rust, but there are no medium or long-term concrete
17+ > plans to integrate it into the compiler.
1818
1919
2020[ `rust-analyzer` ] : https://rust-analyzer.github.io/
2121[ chalk ] : https://rust-lang.github.io/chalk/book/what_is_chalk.html
2222
2323## What is Salsa?
2424
25- Salsa is a library for incremental recomputation. This means it allows reusing
25+ ` Salsa ` is a library for incremental recomputation. This means it allows reusing
2626computations that were already done in the past to increase the efficiency
2727of future computations.
2828
29- The objectives of Salsa are:
29+ The objectives of ` Salsa ` are:
3030 * Provide that functionality in an automatic way, so reusing old computations
31- is done automatically by the library
31+ is done automatically by the library.
3232 * Doing so in a "sound", or "correct", way, therefore leading to the same
33- results as if it had been done from scratch
33+ results as if it had been done from scratch.
3434
35- Salsa's actual model is much richer, allowing many kinds of inputs and many
35+ ` Salsa ` 's actual model is much richer, allowing many kinds of inputs and many
3636different outputs.
37- For example, integrating Salsa with an IDE could mean that the inputs could be
37+ For example, integrating ` Salsa ` with an IDE could mean that the inputs could be
3838the manifest (` Cargo.toml ` ), entire source files (` foo.rs ` ), snippets and so
3939on; the outputs of such an integration could range from a binary executable, to
4040lints, types (for example, if a user selects a certain variable and wishes to
4141see its type), completions, etc.
4242
4343## How does it work?
4444
45- The first thing that Salsa has to do is identify the "base inputs" that
45+ The first thing that ` Salsa ` has to do is identify the "base inputs" that
4646are not something computed but given as input.
4747
48- Then Salsa has to also identify intermediate, "derived" values, which are
48+ Then ` Salsa ` has to also identify intermediate, "derived" values, which are
4949something that the library produces, but, for each derived value there's a
5050"pure" function that computes the derived value.
5151
5252For example, there might be a function ` ast(x: Path) -> AST ` . The produced
5353` AST ` isn't a final value, it's an intermediate value that the library would
5454use for the computation.
5555
56- This means that when you try to compute with the library, Salsa is going to
56+ This means that when you try to compute with the library, ` Salsa ` is going to
5757compute various derived values, and eventually read the input and produce the
5858result for the asked computation.
5959
60- In the course of computing, Salsa tracks which inputs were accessed and which
60+ In the course of computing, ` Salsa ` tracks which inputs were accessed and which
6161values are derived. This information is used to determine what's going to
6262happen when the inputs change: are the derived values still valid?
6363
6464This doesn't necessarily mean that each computation downstream from the input
65- is going to be checked, which could be costly. Salsa only needs to check each
65+ is going to be checked, which could be costly. ` Salsa ` only needs to check each
6666downstream computation until it finds one that isn't changed. At that point, it
6767won't check other derived computations since they wouldn't need to change.
6868
@@ -78,7 +78,7 @@ J <- B <--+
7878
7979When an input ` I ` changes, the derived value ` A ` could change. The derived
8080value ` B ` , which does not depend on ` I ` , ` A ` , or any value derived from ` A ` or
81- ` I ` , is not subject to change. Therefore, Salsa can reuse the computation done
81+ ` I ` , is not subject to change. Therefore, ` Salsa ` can reuse the computation done
8282for ` B ` in the past, without having to compute it again.
8383
8484The computation could also terminate early. Keeping the same graph as before,
@@ -88,37 +88,37 @@ computation. This leads to an "early termination", because there's no need to
8888check if ` C ` needs to change, since both ` C ` direct inputs, ` A ` and ` B ` ,
8989haven't changed.
9090
91- ## Key Salsa concepts
91+ ## Key ` Salsa ` concepts
9292
9393### Query
9494
95- A query is some value that Salsa can access in the course of computation. Each
95+ A query is some value that ` Salsa ` can access in the course of computation. Each
9696query can have a number of keys (from 0 to many), and all queries have a
97- result, akin to functions. 0-key queries are called "input" queries.
97+ result, akin to functions. ` 0-key ` queries are called "input" queries.
9898
9999### Database
100100
101101The database is basically the context for the entire computation, it's meant to
102- store Salsa's internal state, all intermediate values for each query, and
103- anything else that the computation might need. The database must know all the
104- queries that the library is going to do before it can be built, but they don't
105- need to be specified in the same place.
102+ store ` Salsa ` 's internal state, all intermediate values for each query, and
103+ anything else that the computation might need. The database must know all the
104+ queries the library is going to do before it can be built, but they don't need
105+ to be specified in the same place.
106106
107107After the database is formed, it can be accessed with queries that are very
108- similar to functions. Since each query's result is stored in the database,
109- when a query is invoked N times, it will return N ** cloned** results, without
110- having to recompute the query (unless the input has changed in such a way that
111- it warrants recomputation).
108+ similar to functions. Since each query's result is stored in the database, when
109+ a query is invoked ` N ` - times, it will return ` N ` - ** cloned** results, without having
110+ to recompute the query (unless the input has changed in such a way that it
111+ warrants recomputation).
112112
113- For each input query (0-key), a "set" method is generated, allowing the user to
113+ For each input query (` 0-key ` ), a "set" method is generated, allowing the user to
114114change the output of such query, and trigger previous memoized values to be
115115potentially invalidated.
116116
117117### Query Groups
118118
119119A query group is a set of queries which have been defined together as a unit.
120120The database is formed by combining query groups. Query groups are akin to
121- "Salsa modules".
121+ "` Salsa ` modules".
122122
123123A set of queries in a query group are just a set of methods in a trait.
124124
@@ -132,7 +132,7 @@ Example input query group:
132132
133133``` rust,ignore
134134/// This attribute will process this tree, produce this tree as output, and produce
135- /// a bunch of intermediate stuff that Salsa also uses. One of these things is a
135+ /// a bunch of intermediate stuff that Salsa also uses. One of these things is a
136136/// "StorageStruct", whose name we have specified in the attribute.
137137///
138138/// This query group is a bunch of **input** queries, that do not rely on any
@@ -154,9 +154,9 @@ this one depends on by specifying them as supertraits, as seen in the following
154154example:
155155
156156``` rust,ignore
157- /// This query group is going to contain queries that depend on derived values. A
158- /// query group can access another query group's queries by specifying the
159- /// dependency as a super trait . Query groups can be stacked as much as needed using
157+ /// This query group is going to contain queries that depend on derived values.
158+ /// A query group can access another query group's queries by specifying the
159+ /// dependency as a supertrait . Query groups can be stacked as much as needed using
160160/// that pattern.
161161#[salsa::query_group(ParserStorage)]
162162pub trait Parser: Inputs {
@@ -172,10 +172,11 @@ Trait` (or `dyn Trait`), where `Trait` is the query group that the definition
172172belongs to, in addition to the other keys.
173173
174174``` rust,ignore
175- ///This is going to be the definition of the `ast` query in the `Parser` trait.
176- ///So, when the query `ast` is invoked, and it needs to be recomputed, Salsa is going to call this function
177- ///and it's going to give it the database as `impl Parser`.
178- ///The function doesn't need to be aware of all the queries of all the query groups
175+ /// This is going to be the definition of the `ast` query in the `Parser` trait.
176+ /// So, when the query `ast` is invoked, and it needs to be recomputed, Salsa is
177+ /// going to call this function and it's going to give it the database as `impl
178+ /// Parser`. The function doesn't need to be aware of all the queries of all the
179+ /// query groups
179180fn ast(db: &impl Parser, name: String) -> String {
180181 //! Note, `impl Parser` is used here but `dyn Parser` works just as well
181182 /* code */
0 commit comments