| 1 |
# Mercurial Rust Code |
| 2 |
|
| 3 |
This directory contains various Rust code for the Mercurial project. |
| 4 |
Rust is not required to use (or build) Mercurial, but using it |
| 5 |
improves performance in some areas. |
| 6 |
|
| 7 |
There are currently four independent Rust projects: |
| 8 |
|
| 9 |
* `chg`. An implementation of `chg`, in Rust instead of C. |
| 10 |
* `hgcli`. A project that provides a (mostly) self-contained `hg` binary, |
| 11 |
for ease of deployment and a bit of speed, using PyOxidizer. See |
| 12 |
`hgcli/README.md`. |
| 13 |
* `hg-core` (and `hg-pyo3`): implementation of some |
| 14 |
functionality of mercurial in Rust, e.g. ancestry computations in |
| 15 |
revision graphs, status or pull discovery. The top-level `Cargo.toml` file |
| 16 |
defines a workspace containing these crates. |
| 17 |
* `rhg`: a pure Rust implementation of Mercurial, with a fallback mechanism for |
| 18 |
unsupported invocations. It reuses the logic `hg-core` but |
| 19 |
completely forgoes interaction with Python. See |
| 20 |
`rust/rhg/README.md` for more details. |
| 21 |
|
| 22 |
***warning: |
| 23 |
rhg should not yet be packaged for distribution without a warning that |
| 24 |
certain rough edges may be encountered, detailed in its README.*** |
| 25 |
|
| 26 |
## Using Rust code |
| 27 |
|
| 28 |
Local use (you need to clean previous build artifacts if you have |
| 29 |
built without rust previously): |
| 30 |
|
| 31 |
```shell |
| 32 |
$ make PURE=--rust local # to use ./hg |
| 33 |
$ ./tests/run-tests.py --rust # to run all tests |
| 34 |
$ ./hg debuginstall | grep -i rust # to validate rust is in use |
| 35 |
checking Rust extensions (installed) |
| 36 |
checking module policy (rust+c-allow) |
| 37 |
``` |
| 38 |
|
| 39 |
## Special features |
| 40 |
|
| 41 |
In the future, compile-time opt-ins may be added |
| 42 |
to the `features` section in `hg-pyo3/Cargo.toml`. |
| 43 |
|
| 44 |
To use features from the Makefile, use the `HG_RUST_FEATURES` environment |
| 45 |
variable: for instance `HG_RUST_FEATURES="some-feature other-feature"`. |
| 46 |
|
| 47 |
## Profiling and tracing |
| 48 |
|
| 49 |
The terminology below assumes the oversimplification of profiling being mostly |
| 50 |
sampling-based or an otherwise statistical way of looking at the performance |
| 51 |
of Mercurial, whereas tracing is the deliberate attempt at looking into all |
| 52 |
relevant events, determined by explicit tracing code. |
| 53 |
|
| 54 |
The line is blurred when using things like Intel Processor Trace, but if you're |
| 55 |
using Intel PT, you probably know. |
| 56 |
|
| 57 |
### Profiling |
| 58 |
|
| 59 |
Creating a `.cargo/config` file with the following content enables |
| 60 |
debug information in optimized builds. This make profiles more informative |
| 61 |
with source file name and line number for Rust stack frames and |
| 62 |
(in some cases) stack frames for Rust functions that have been inlined: |
| 63 |
|
| 64 |
```toml |
| 65 |
[profile.release] |
| 66 |
debug = true |
| 67 |
``` |
| 68 |
|
| 69 |
`py-spy` (https://github.com/benfred/py-spy) can be used to |
| 70 |
construct a single profile with rust functions and python functions |
| 71 |
(as opposed to `hg --profile`, which attributes time spent in rust |
| 72 |
to some unlucky python code running shortly after the rust code, and |
| 73 |
as opposed to tools for native code like `perf`, which attribute |
| 74 |
time to the python interpreter instead of python functions). |
| 75 |
|
| 76 |
Example usage: |
| 77 |
|
| 78 |
```shell |
| 79 |
$ make PURE=--rust local # Don't forget to recompile after a code change |
| 80 |
$ py-spy record --native --output /tmp/profile.svg -- ./hg ... |
| 81 |
``` |
| 82 |
|
| 83 |
### Tracing |
| 84 |
|
| 85 |
#### Simple stderr |
| 86 |
|
| 87 |
Setting the environment variable `RUST_LOG` to any valid level (`error`, |
| 88 |
`warn`, `info`, `debug` and `trace`, in ascending order of verbosity) |
| 89 |
will make hg print a few high level rust-related performance numbers to stderr. |
| 90 |
It can also indicate why the rust code cannot be used (say, using lookaround regex |
| 91 |
in `.hgignore`). `RUST_LOG` usage can be further refined, please refer to the |
| 92 |
`tracing-subscriber` rust crate for more details on `EnvFilter`. |
| 93 |
|
| 94 |
Example: |
| 95 |
|
| 96 |
```shell |
| 97 |
$ make build-rhg |
| 98 |
$ RUST_LOG=trace rust/target/release/rhg status > /dev/null |
| 99 |
2025-03-04T12:14:42.336153Z DEBUG hg::utils: Capped the rayon threadpool to 16 threads |
| 100 |
2025-03-04T12:14:42.336901Z DEBUG config_setup: rhg: close time.busy=730µs time.idle=2.56µs |
| 101 |
2025-03-04T12:14:42.338668Z DEBUG repo setup:configitems.toml: hg::config::config_items: close time.busy=1.70ms time.idle=270ns |
| 102 |
2025-03-04T12:14:42.338682Z DEBUG repo setup: rhg: close time.busy=1.77ms time.idle=471ns |
| 103 |
2025-03-04T12:14:42.338716Z DEBUG main_with_result:CLI and command setup:new_v2: hg::dirstate::dirstate_map: close time.busy=291ns time.idle=210ns |
| 104 |
2025-03-04T12:14:42.354094Z DEBUG main_with_result:CLI and command setup:blackbox: rhg: close time.busy=15.2ms time.idle=622ns |
| 105 |
2025-03-04T12:14:42.354107Z DEBUG main_with_result:CLI and command setup: rhg: close time.busy=15.4ms time.idle=270ns |
| 106 |
2025-03-04T12:14:42.356250Z DEBUG main_with_result:rhg status:status:build_regex_match:re_matcher: hg::matchers: close time.busy=961µs time.idle=541ns |
| 107 |
2025-03-04T12:14:42.356291Z DEBUG main_with_result:rhg status:status:build_regex_match: hg::matchers: close time.busy=1.69ms time.idle=420ns |
| 108 |
2025-03-04T12:14:42.374671Z DEBUG main_with_result:rhg status:status: hg::dirstate::status: close time.busy=20.5ms time.idle=532ns |
| 109 |
2025-03-04T12:14:42.374700Z DEBUG main_with_result:rhg status: rhg::commands::status: close time.busy=20.6ms time.idle=470ns |
| 110 |
2025-03-04T12:14:42.380897Z DEBUG main_with_result:blackbox: rhg: close time.busy=6.19ms time.idle=932ns |
| 111 |
2025-03-04T12:14:42.380918Z DEBUG main_with_result: rhg: close time.busy=42.2ms time.idle=211ns |
| 112 |
``` |
| 113 |
|
| 114 |
#### Full timeline view |
| 115 |
|
| 116 |
If compiled with the `full-tracing` feature, two things happen: |
| 117 |
- `RUST_LOG` writes a chrome-trace to a file instead of logging to stderr |
| 118 |
- More (maybe extremely) verbose tracing is available at the `trace` level |
| 119 |
that would otherwise get compiled out entirely. |
| 120 |
|
| 121 |
The file defaults to `./trace-{unix epoch in micros}.json`, but can be |
| 122 |
overridden via the `HG_TRACE_PATH` environment variable. |
| 123 |
|
| 124 |
Example: |
| 125 |
|
| 126 |
```shell |
| 127 |
$ HG_RUST_FEATURES="full-tracing" make local PURE=--rust |
| 128 |
$ HG_TRACE_PATH=/tmp/trace.json RUST_LOG=debug ./hg-local st > /dev/null |
| 129 |
``` |
| 130 |
|
| 131 |
In this case, opening `/tmp/trace.json` in `ui.perfetto.dev` will show a |
| 132 |
timeline of all recorded spans and events, which can be very useful for making |
| 133 |
sense of what is happening. |
| 134 |
|
| 135 |
## Developing Rust |
| 136 |
|
| 137 |
### Minimum Supported Rust Version |
| 138 |
|
| 139 |
The minimum supported rust version (MSRV) is specified in the [Clippy](https://doc.rust-lang.org/stable/clippy/) |
| 140 |
configuration file at `rust/clippy.toml`. It is set to be `1.85.1` as of |
| 141 |
this writing, but keep in mind that the authoritative value is the one |
| 142 |
from the configuration file. |
| 143 |
|
| 144 |
We bump it from time to time, with the general rule being that our |
| 145 |
MSRV should not be greater that the version of the Rust toolchain |
| 146 |
shipping with Debian testing, so that the Rust enhanced Mercurial can |
| 147 |
be eventually packaged in Debian. |
| 148 |
|
| 149 |
To ensure that you are not depending on features introduced in later |
| 150 |
versions, you can issue `rustup override set x.y.z` at the root of |
| 151 |
the repository. |
| 152 |
|
| 153 |
### Build and development |
| 154 |
|
| 155 |
Go to the `hg-pyo3` folder: |
| 156 |
|
| 157 |
```shell |
| 158 |
$ cd rust/hg-pyo3 |
| 159 |
``` |
| 160 |
|
| 161 |
Or, only the `hg-core` folder. Be careful not to break compatibility: |
| 162 |
|
| 163 |
```shell |
| 164 |
$ cd rust/hg-core |
| 165 |
``` |
| 166 |
|
| 167 |
Simply run: |
| 168 |
|
| 169 |
```shell |
| 170 |
$ cargo build --release |
| 171 |
``` |
| 172 |
|
| 173 |
It is possible to build without `--release`, but it is not |
| 174 |
recommended if performance is of any interest: there can be an order |
| 175 |
of magnitude of degradation when removing `--release`. |
| 176 |
|
| 177 |
For faster builds, you may want to skip code generation: |
| 178 |
|
| 179 |
```shell |
| 180 |
$ cargo check |
| 181 |
``` |
| 182 |
|
| 183 |
For even faster typing: |
| 184 |
|
| 185 |
```shell |
| 186 |
$ cargo c |
| 187 |
``` |
| 188 |
|
| 189 |
You can run only the rust-specific tests (as opposed to tests of |
| 190 |
mercurial as a whole) with: |
| 191 |
|
| 192 |
```shell |
| 193 |
$ cargo test --all --no-default-features |
| 194 |
``` |
| 195 |
|
| 196 |
### Formatting the code |
| 197 |
|
| 198 |
We use `rustfmt` to keep the code formatted at all times. For now, we are |
| 199 |
using the nightly version because it has been stable enough and provides |
| 200 |
comment folding. |
| 201 |
|
| 202 |
Our CI enforces that the code does not need reformatting. Before |
| 203 |
submitting your changes, please format the entire Rust workspace by running: |
| 204 |
|
| 205 |
```shell |
| 206 |
$ cargo +nightly fmt |
| 207 |
``` |
| 208 |
|
| 209 |
This requires you to have the nightly toolchain installed. |
| 210 |
|
| 211 |
### Linting: code sanity |
| 212 |
|
| 213 |
We're using [Clippy](https://doc.rust-lang.org/stable/clippy/), the standard code diagnosis tool of the Rust |
| 214 |
community. |
| 215 |
|
| 216 |
Our CI enforces that the code is free of Clippy warnings, so you might |
| 217 |
want to run it on your side before submitting your changes. Simply do: |
| 218 |
|
| 219 |
```shell |
| 220 |
$ cargo clippy |
| 221 |
``` |
| 222 |
|
| 223 |
from the top of the Rust workspace. Clippy is part of the default |
| 224 |
`rustup` install, so it should work right away. In case it would |
| 225 |
not, you can install it with `rustup component add`. |