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 (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
1515> extensively in [ ` rust-analyzer ` ] , the official implementation of the language
1616> server protocol for Rust, but there are no medium or long-term concrete
1717> plans to integrate it into the compiler.
@@ -21,46 +21,46 @@ Matsakis.
2121
2222## What is Salsa?
2323
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
2525computations that were already done in the past to increase the efficiency
2626of future computations.
2727
28- The objectives of ` Salsa ` are:
28+ The objectives of Salsa are:
2929 * Provide that functionality in an automatic way, so reusing old computations
3030 is done automatically by the library.
3131 * Doing so in a "sound", or "correct", way, therefore leading to the same
3232 results as if it had been done from scratch.
3333
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
3636the inputs could be manifests (` Cargo.toml ` , ` rust-toolchain.toml ` ), entire
3737source files (` foo.rs ` ), snippets and so on. The outputs of such an integration
3838could range from a binary executable, to lints, types (for example, if a user
3939selects a certain variable and wishes to see its type), completions, etc.
4040
4141## How does it work?
4242
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
4444are not something computed but given as input.
4545
46- Then ` Salsa ` has to also identify intermediate, "derived" values, which are
46+ Then Salsa has to also identify intermediate, "derived" values, which are
4747something that the library produces, but, for each derived value there's a
4848"pure" function that computes the derived value.
4949
5050For example, there might be a function ` ast(x: Path) -> AST ` . The produced
5151Abstract Syntax Tree (` AST ` ) isn't a final value, it's an intermediate value
5252that the library would use for the computation.
5353
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
5555compute various derived values, and eventually read the input and produce the
5656result for the asked computation.
5757
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
5959values are derived. This information is used to determine what's going to
6060happen when the inputs change: are the derived values still valid?
6161
6262This 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
6464downstream computation until it finds one that isn't changed. At that point, it
6565won't check other derived computations since they wouldn't need to change.
6666
@@ -76,7 +76,7 @@ J <- B <--+
7676
7777When an input ` I ` changes, the derived value ` A ` could change. The derived
7878value ` 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
8080for ` B ` in the past, without having to compute it again.
8181
8282The 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
8686check if ` C ` needs to change, since both ` C ` direct inputs, ` A ` and ` B ` ,
8787haven't changed.
8888
89- ## Key ` Salsa ` concepts
89+ ## Key Salsa concepts
9090
9191### Query
9292
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
9494query can have a number of keys (from 0 to many), and all queries have a
9595result, akin to functions. ` 0-key ` queries are called "input" queries.
9696
9797### Database
9898
9999The 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
101101anything else that the computation might need. The database must know all the
102102queries the library is going to do before it can be built, but they don't need
103103to be specified in the same place.
@@ -116,14 +116,14 @@ potentially invalidated.
116116
117117A query group is a set of queries which have been defined together as a unit.
118118The database is formed by combining query groups. Query groups are akin to
119- "` Salsa ` modules".
119+ "Salsa modules".
120120
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.
122122
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
124124(` #[salsa::query_group(...)] ` ) has to be created.
125125
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
127127to create a ` struct ` to be used later when the database is created.
128128
129129Example input query group:
@@ -166,7 +166,7 @@ pub trait Parser: Inputs {
166166
167167When creating a derived query the implementation of said query must be defined
168168outside 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
170170belongs to, in addition to the other keys.
171171
172172``` rust,ignore
0 commit comments