| 1 |
# Revset Evaluation |
| 2 |
|
| 3 |
Whenever a revset is evaluated, it must pass through several stages. These |
| 4 |
stages gradually transform the revset to a lower level representation that can |
| 5 |
be efficiently evaluated. |
| 6 |
|
| 7 |
*Note: We often use a pseudo-revset syntax to make this documentation easier to |
| 8 |
follow. For instance, some operations described in this document are only used |
| 9 |
internally and aren't available in the revset language exposed to users.* |
| 10 |
|
| 11 |
## Parsing and Alias Expansion |
| 12 |
|
| 13 |
As a revset is parsed, any uses of revset aliases are substituted with their |
| 14 |
definitions. For instance, given the following aliases: |
| 15 |
|
| 16 |
```toml |
| 17 |
[revset-aliases] |
| 18 |
"trunk()" = "main@origin" |
| 19 |
"f(x, n)" = "x | ancestors(x.., n)" |
| 20 |
``` |
| 21 |
|
| 22 |
Revsets are expanded as follows: |
| 23 |
|
| 24 |
* `trunk()` expands to `main@origin` |
| 25 |
* `f(trunk(), 2)` expands to `main@origin | ancestors(main@origin.., 2)` |
| 26 |
* `f(main | tags(), 2)` expands to `(main | tags()) | ancestors((main | tags()).., 2)` |
| 27 |
|
| 28 |
Notably, revset expansion works similarly to macro expansion in other languages; |
| 29 |
if a revset alias uses a parameter twice, then the entire argument is |
| 30 |
substituted into the result twice. This can lead to inefficient evaluations if |
| 31 |
an argument is expensive to evaluate. |
| 32 |
|
| 33 |
## Symbol Resolution |
| 34 |
|
| 35 |
Revsets are eventually evaluated by the `Index` implementation, but the `Index` |
| 36 |
only stores information about commits and change IDs; it doesn't know about the |
| 37 |
current state of the `Repo`. Therefore, any revsets relying on state from the |
| 38 |
`Repo` must be resolved early. |
| 39 |
|
| 40 |
Some examples include: |
| 41 |
|
| 42 |
* Working copies (e.g. `@`, `workspace@`) |
| 43 |
* Commit ID prefixes (e.g. `123`) |
| 44 |
* Change ID prefixes (e.g. `xyz`, `xyz/1`) |
| 45 |
* Bookmark/tag names (e.g. `v1.0.0`, `main@origin`) |
| 46 |
* `bookmarks()`/`tags()` |
| 47 |
* `remote_bookmarks()`/`remote_tags()` |
| 48 |
|
| 49 |
If any of these revsets fail to resolve, a symbol resolution error will be |
| 50 |
emitted immediately. `present()` can be used to suppress these errors in cases |
| 51 |
where it is expected that a symbol might not exist (e.g. when `main` doesn't |
| 52 |
exist, `present(main)` will return `none()` instead of failing). |
| 53 |
|
| 54 |
This stage also handles `at_operation()`, and it also inserts `WithinVisibility` |
| 55 |
nodes to keep track of the commit IDs in `visible_heads()`. |
| 56 |
|
| 57 |
Symbol resolution substitutes these revset functions with a list of commit IDs, |
| 58 |
meaning the `Index` doesn't need to handle these functions while evaluating a |
| 59 |
revset. It is also possible to extend `jj` with custom symbol resolvers which |
| 60 |
are also handled by the symbol resolution stage. |
| 61 |
|
| 62 |
*Note: Although the `Index` does store information about commit ID prefixes |
| 63 |
and change ID prefixes, we still need to resolve these symbols during this |
| 64 |
stage because resolving unique prefixes also requires information about |
| 65 |
visible commits and the `revsets.short-prefixes` setting.* |
| 66 |
|
| 67 |
## Optimization |
| 68 |
|
| 69 |
Revset optimization consists of a series of passes that recursively rewrite |
| 70 |
revset expressions. The ordering of these passes is important, since one pass |
| 71 |
may enable further optimizations in a later pass. These are the current passes: |
| 72 |
|
| 73 |
1. Resolve referenced commits |
| 74 |
2. Unfold ranges and differences |
| 75 |
3. Fold redundant expressions |
| 76 |
4. Fold generations |
| 77 |
5. Flatten intersections |
| 78 |
6. Sort negations and ancestors |
| 79 |
7. Fold ancestors union |
| 80 |
8. Internalize filters |
| 81 |
9. Fold heads range |
| 82 |
10. Fold ranges and differences |
| 83 |
11. Fold negated ancestors |
| 84 |
|
| 85 |
### 1. Resolve referenced commits |
| 86 |
|
| 87 |
This pass finds all referenced commit IDs and inserts `WithinReference` nodes to |
| 88 |
keep track of referenced commit IDs. This information will be used when lowering |
| 89 |
revsets to backend expressions later, since `all()` must also include all |
| 90 |
ancestors of commits referenced in the revset in addition to ancestors of |
| 91 |
`visible_heads()`. This pass comes first because later passes may remove |
| 92 |
redundant expressions. |
| 93 |
|
| 94 |
### 2. Unfold ranges and differences |
| 95 |
|
| 96 |
This pass unfolds range (`x..y`) and difference (`x ~ y`) operations into |
| 97 |
intersections. This enables further optimizations in later passes. |
| 98 |
|
| 99 |
Rules: |
| 100 |
|
| 101 |
* `x..y` => `::y & ~(::x)` |
| 102 |
* `x ~ y` => `x & ~y` |
| 103 |
|
| 104 |
*Note: During parsing, `x..` is already converted to `~::x`, `..x` is converted |
| 105 |
to `root()..x`, and `..` is converted to `~root()`.* |
| 106 |
|
| 107 |
### 3. Fold redundant expressions |
| 108 |
|
| 109 |
This pass optimizes intersections and unions with `all()` and `none()`, as well |
| 110 |
as negations. |
| 111 |
|
| 112 |
Rules: |
| 113 |
|
| 114 |
* `~(~x)` => `x` |
| 115 |
* `~none()` => `all()` |
| 116 |
* `~all()` => `none()` |
| 117 |
* `none() | x` => `x` |
| 118 |
* `x | none()` => `x` |
| 119 |
* `all() | x` => `all()` |
| 120 |
* `x | all()` => `all()` |
| 121 |
* `none() & x` => `none()` |
| 122 |
* `x & none()` => `none()` |
| 123 |
* `all() & x` => `x` |
| 124 |
* `x & all()` => `x` |
| 125 |
|
| 126 |
### 4. Fold generations |
| 127 |
|
| 128 |
The internal representation of ancestors/parents/descendants/children revsets is |
| 129 |
slightly different from the normal revset syntax. There are two basic |
| 130 |
operations: |
| 131 |
|
| 132 |
* `ancestors(heads, generation)` |
| 133 |
* `descendants(roots, generation)` |
| 134 |
|
| 135 |
In this representation, `generation` is an exclusive range that specifies how |
| 136 |
far to walk backwards/forwards from the `heads`/`roots` (unlike the standard |
| 137 |
revset syntax, where `generation` is a single number). |
| 138 |
|
| 139 |
For example, this is how the following revsets are represented internally: |
| 140 |
|
| 141 |
* `ancestors(x)` => `ancestors(x, 0..MAX)` |
| 142 |
* `ancestors(x, n)` => `ancestors(x, 0..n)` |
| 143 |
* `parents(x)` => `ancestors(x, 1..2)` |
| 144 |
* `parents(x, n)` => `ancestors(x, n..n+1)` |
| 145 |
* `descendants(x)` => `descendants(x, 0..MAX)` |
| 146 |
* `descendants(x, n)` => `descendants(x, 0..n)` |
| 147 |
* `children(x)` => `descendants(x, 1..2)` |
| 148 |
* `children(x, n)` => `descendants(x, n..n+1)` |
| 149 |
|
| 150 |
This optimization pass folds nested `ancestors()`/`descendants()` operations |
| 151 |
into a single operation using the following rules: |
| 152 |
|
| 153 |
* `ancestors(ancestors(x, a..b), c..d)` => `ancestors(x, (a..b) + (c..d))` |
| 154 |
* `descendants(descendants(x, a..b), c..d)` => `descendants(x, (a..b) + (c..d)) |
| 155 |
|
| 156 |
Where the addition of non-empty ranges `a..b` and `c..d` is defined as |
| 157 |
`(a + c)..(b + d - 1)`. |
| 158 |
|
| 159 |
Therefore, this pass allows these types of optimizations: |
| 160 |
|
| 161 |
* `x--` => `ancestors(x, 2..3)` |
| 162 |
* `::(x-)` => `ancestors(x, 1..MAX)` |
| 163 |
* `(::x)-` => `ancestors(x, 1..MAX)` |
| 164 |
* `ancestors(x---, 5)` => `ancestors(x, 3..8)` |
| 165 |
|
| 166 |
There is also some additional logic for handling `first_parent()` and |
| 167 |
`first_ancestors()` correctly. |
| 168 |
|
| 169 |
### 5. Flatten intersections |
| 170 |
|
| 171 |
This pass flattens intersections to be left-associtive. This makes later |
| 172 |
optimizations easier to implement. For example: |
| 173 |
|
| 174 |
* `x & (y & z)` => `(x & y) & z` |
| 175 |
* `(w & (x & y)) & z` => `((w & x) & y) & z` |
| 176 |
|
| 177 |
### 6. Sort negations and ancestors |
| 178 |
|
| 179 |
This pass sorts the elements in an intersection based on 4 categories: |
| 180 |
|
| 181 |
1. Negated ancestors (`~ancestors(x, n..MAX)`) |
| 182 |
2. Ancestors (`ancestors(x, n..MAX)`) |
| 183 |
3. Other non-negated expressions |
| 184 |
4. Other negated expressions (`~x`) |
| 185 |
|
| 186 |
Negated ancestors are moved to the left to enable the "fold ancestors union" |
| 187 |
pass. Placing the ancestors next to the negated ancestors next allows them to be |
| 188 |
folded into a range easily in a later pass, and putting other negated |
| 189 |
expressions at the end allows them to be folded into a difference easily in a |
| 190 |
later pass as well. |
| 191 |
|
| 192 |
Examples: |
| 193 |
|
| 194 |
* `~w & x & ~::y & ::z` => `~::y & ::z & x & ~w` |
| 195 |
* `::w & ~::x & ::y & ~::z` => `~::x & ~::z & ::w & ::y` |
| 196 |
|
| 197 |
### 7. Fold ancestors union |
| 198 |
|
| 199 |
This pass folds unions of ancestors into ancestors of unions. The main purpose |
| 200 |
of this optimization is to allow combining intersections of ranges into a single |
| 201 |
range in later passes (e.g. `a..b & c..` can be optimized to `(a | c)..b`). |
| 202 |
|
| 203 |
Rules: |
| 204 |
|
| 205 |
* `::x | ::y` => `::(x | y)` |
| 206 |
* `~::x & ~::y` => `~::(x | y)` |
| 207 |
|
| 208 |
### 8. Internalize filters |
| 209 |
|
| 210 |
Some revsets are implemented as filters. This means that the `Index` can't |
| 211 |
directly query for these revsets. Instead, it must check the filter against each |
| 212 |
commit individually to test whether it matches. |
| 213 |
|
| 214 |
Examples of filter revsets: |
| 215 |
|
| 216 |
* `merges()` |
| 217 |
* `empty()`/`files()` |
| 218 |
* `description()`/`subject()` |
| 219 |
* `mine()`/`author_*()`/`committer_*()` |
| 220 |
* `diff_lines()` |
| 221 |
* `conflicts()` |
| 222 |
* `signed()` |
| 223 |
* `divergent()` |
| 224 |
|
| 225 |
For instance, to evaluate `x..y & conflicts()`, the `Index` implementation first |
| 226 |
evaluates `x..y`, then it checks every commit in that range to see whether it |
| 227 |
has a conflict or not. If there is no base set of commits, the `Index` |
| 228 |
implementation will instead have to check the filter against every commit in |
| 229 |
`all()`, which can be expensive on large repos. |
| 230 |
|
| 231 |
The purpose of this optimization pass is to group all filters together at the |
| 232 |
end of an intersection, meaning that all non-filter expressions can be |
| 233 |
intersected before checking the filter. This reduces the amount of commits that |
| 234 |
need to be checked. |
| 235 |
|
| 236 |
Rules: |
| 237 |
|
| 238 |
* `~filter(x)` => `filter(~x)` |
| 239 |
* `filter(x) | filter(y) => filter(x | y)` |
| 240 |
* `filter(x) | y` => `filter(x | y)` |
| 241 |
* `x | filter(y)` => `filter(x | y)` |
| 242 |
* `filter(x) & filter(y) => filter(x & y)` |
| 243 |
* `filter(x) & y` => `y & filter(x)` |
| 244 |
* `(x & filter(y)) & filter(z)` => `x & filter(y & z)` |
| 245 |
* `(x & filter(y)) & z` => `(x & z) & filter(y)` |
| 246 |
|
| 247 |
Examples: |
| 248 |
|
| 249 |
* `(::y | empty()) & ::x` => `::x & filter(::y | empty())` |
| 250 |
* `conflicts() & ::x & empty()` => `::x & filter(conflicts() & empty())` |
| 251 |
|
| 252 |
### 9. Fold heads range |
| 253 |
|
| 254 |
Often, `heads()` is used to find the most recent commit satisfying some |
| 255 |
property. For instance, `heads(::@ ~ empty())` can be used to find the most |
| 256 |
recent non-empty ancestors of `@`. If we were to naively evaluate this revset, |
| 257 |
we would first have to find every non-empty commit which is an ancestor of `@`, |
| 258 |
and then take the `heads()` of that set. This would be very slow, since |
| 259 |
`empty()` is an expensive filter to check. |
| 260 |
|
| 261 |
This pass converts `heads()` on a filtered range of commits into a special |
| 262 |
`heads_range(roots, heads, [filter])` operation which can be evaluated more |
| 263 |
efficiently because it is able to stop scanning ancestor commits whenever a head |
| 264 |
is found. |
| 265 |
|
| 266 |
Examples: |
| 267 |
|
| 268 |
* `heads(x..y)` => `heads_range(x, y)` |
| 269 |
* `heads(x..y & conflicts())` => `heads_range(x, y, conflicts())` |
| 270 |
* `heads(::x)` => `heads_range(none(), x)` |
| 271 |
* `heads(~::x)` => `heads_range(x, visible_heads_or_referenced())` |
| 272 |
* `heads(filter(x))` => `heads_range(none(), visible_heads_or_referenced(), x)` |
| 273 |
|
| 274 |
*Note: When there's no obvious `heads` for the filtered range, we can use |
| 275 |
`visible_heads_or_referenced()` instead, which represents a union of |
| 276 |
`visible_heads()` and all explicitly referenced commits in the revset |
| 277 |
expression. Since revsets can only return visible commits and ancestors of |
| 278 |
explicitly referenced commits, if we start at `visible_heads_or_referenced()` |
| 279 |
and walk backwards through the commit graph, we will eventually see every commit |
| 280 |
which could possibly be returned (and as we'll see later, this is actually how |
| 281 |
the `all()` revset is evaluated).* |
| 282 |
|
| 283 |
Since `::x` is equivalent to `::heads(x)`, this pass also inserts |
| 284 |
`heads_range()` operations inside of `ancestors()` when possible: |
| 285 |
|
| 286 |
* `::(x..y)` => `::heads_range(x, y)` |
| 287 |
* `::(~mine())` => `::heads_range(none(), visible_heads_or_referenced(), ~mine())` |
| 288 |
|
| 289 |
### 10. Fold ranges and differences |
| 290 |
|
| 291 |
This pass eliminates negated expressions by folding them into ranges and |
| 292 |
differences when possible. Negated expressions like `~x` are expensive to |
| 293 |
evaluate because they will be converted into `all() ~ x`, meaning they require |
| 294 |
iterating over every commit in the repo. Therefore, we want to eliminate as many |
| 295 |
negations as possible. |
| 296 |
|
| 297 |
Rules: |
| 298 |
|
| 299 |
* `::x & ~::y` => `y..x` |
| 300 |
* `~::x & ::y` => `x..y` |
| 301 |
* `x & ~y` => `x ~ y` |
| 302 |
* `~x & y` => `y ~ x` (if `y` isn't a filter) |
| 303 |
|
| 304 |
### 11. Fold negated ancestors |
| 305 |
|
| 306 |
This pass eliminates any remaining negated ancestors expressions by converting |
| 307 |
them to ranges. The heads of the range are all visible heads and all referenced |
| 308 |
commits in the revset. |
| 309 |
|
| 310 |
Rules: |
| 311 |
|
| 312 |
* `~::x` => `x..visible_heads_or_referenced()` |
| 313 |
|
| 314 |
## Visibility Resolution |
| 315 |
|
| 316 |
After optimization, the revset is lowered to a `ResolvedExpression` that the |
| 317 |
`Index` implementation can evaluate directly without being aware of commit |
| 318 |
visibility. The main differences between a revset expression and a |
| 319 |
`ResolvedExpression` are: |
| 320 |
|
| 321 |
* `all()` is converted to `::visible_heads_or_referenced()`. |
| 322 |
|
| 323 |
* Negated expressions like `~x` are converted to |
| 324 |
`::visible_heads_or_referenced() ~ x`. |
| 325 |
|
| 326 |
* Filters are represented by a separate `ResolvedPredicateExpression` type which |
| 327 |
can only appear in certain contexts. For instance, there is a separate |
| 328 |
`FilterWithin` operation to handle intersections with filters. Filters outside |
| 329 |
of intersections are handled as `::visible_heads_or_referenced() & filter(f)`. |
| 330 |
|
| 331 |
* All visibility information is removed from the expression, so |
| 332 |
`visible_heads()` and `visible_heads_or_referenced()` are replaced by lists of |
| 333 |
commit IDs. |
| 334 |
|
| 335 |
## Evaluation by Index |
| 336 |
|
| 337 |
Finally, the `ResolvedExpression` is passed to the `Index` implementation to be |
| 338 |
evaluated into a `Revset` value. There is a built-in default index |
| 339 |
implementation, but `jj` can be extended with alternative implementations. |
| 340 |
|
| 341 |
A `Revset` can then be converted into a lazy stream of commits in topological |
| 342 |
order (with children before parents) or into a function that tests whether a |
| 343 |
`CommitId` is included in the revset. |