2014-02-24 21:45:20 -06:00
|
|
|
//! Calculation and management of a Strict Version Hash for crates
|
|
|
|
//!
|
2016-05-06 04:01:09 -05:00
|
|
|
//! The SVH is used for incremental compilation to track when HIR
|
|
|
|
//! nodes have changed between compilations, and also to detect
|
|
|
|
//! mismatches where we have two versions of the same crate that were
|
|
|
|
//! compiled from distinct sources.
|
2014-02-24 21:45:20 -06:00
|
|
|
|
2019-07-23 10:50:47 -05:00
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2014-02-24 21:45:20 -06:00
|
|
|
use std::fmt;
|
2016-05-06 04:01:09 -05:00
|
|
|
use std::hash::{Hash, Hasher};
|
2014-02-24 21:45:20 -06:00
|
|
|
|
2019-02-08 10:36:22 -06:00
|
|
|
use crate::stable_hasher;
|
2018-08-03 13:22:22 -05:00
|
|
|
|
2016-05-06 04:01:09 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2014-02-24 21:45:20 -06:00
|
|
|
pub struct Svh {
|
2016-05-06 04:01:09 -05:00
|
|
|
hash: u64,
|
2014-02-24 21:45:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Svh {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Creates a new `Svh` given the hash. If you actually want to
|
2016-03-28 16:36:56 -05:00
|
|
|
/// compute the SVH from some HIR, you want the `calculate_svh`
|
2021-04-07 14:47:01 -05:00
|
|
|
/// function found in `rustc_incremental`.
|
2016-05-06 04:01:09 -05:00
|
|
|
pub fn new(hash: u64) -> Svh {
|
2018-11-06 14:05:44 -06:00
|
|
|
Svh { hash }
|
2014-02-24 21:45:20 -06:00
|
|
|
}
|
|
|
|
|
2016-05-06 04:01:09 -05:00
|
|
|
pub fn as_u64(&self) -> u64 {
|
|
|
|
self.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(&self) -> String {
|
2016-05-12 09:12:58 -05:00
|
|
|
format!("{:016x}", self.hash)
|
2014-02-24 21:45:20 -06:00
|
|
|
}
|
2016-05-06 04:01:09 -05:00
|
|
|
}
|
2016-03-28 16:36:56 -05:00
|
|
|
|
2016-05-06 04:01:09 -05:00
|
|
|
impl Hash for Svh {
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: Hasher,
|
|
|
|
{
|
|
|
|
self.hash.to_le().hash(state);
|
2016-03-28 16:36:56 -05:00
|
|
|
}
|
2014-02-24 21:45:20 -06:00
|
|
|
}
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
impl fmt::Display for Svh {
|
2019-02-08 10:36:22 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-05-06 04:01:09 -05:00
|
|
|
f.pad(&self.to_string())
|
2014-02-24 21:45:20 -06:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 18:02:39 -05:00
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<S: Encoder> Encodable<S> for Svh {
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-06 22:30:45 -05:00
|
|
|
fn encode(&self, s: &mut S) {
|
|
|
|
s.emit_u64(self.as_u64().to_le());
|
2016-08-11 18:02:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
impl<D: Decoder> Decodable<D> for Svh {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-17 20:22:50 -06:00
|
|
|
fn decode(d: &mut D) -> Svh {
|
|
|
|
Svh::new(u64::from_le(d.read_u64()))
|
2016-08-11 18:02:39 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-27 09:12:57 -05:00
|
|
|
|
2018-08-03 13:22:22 -05:00
|
|
|
impl<T> stable_hasher::HashStable<T> for Svh {
|
|
|
|
#[inline]
|
2019-09-26 17:54:39 -05:00
|
|
|
fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
|
2018-08-03 13:22:22 -05:00
|
|
|
let Svh { hash } = *self;
|
|
|
|
hash.hash_stable(ctx, hasher);
|
|
|
|
}
|
|
|
|
}
|