1
Fork 0

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`.
This commit is contained in:
Nicholas Nethercote 2022-06-07 13:30:45 +10:00
parent 582b9cbc45
commit 1acbe7573d
45 changed files with 611 additions and 682 deletions

View file

@ -842,7 +842,7 @@ impl<K: DepKind> DepGraph<K> {
if let Some(data) = &self.data {
data.current.encoder.steal().finish(profiler)
} else {
Ok(())
Ok(0)
}
}

View file

@ -20,7 +20,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::Lock;
use rustc_index::vec::{Idx, IndexVec};
use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize};
use rustc_serialize::{Decodable, Decoder, Encodable};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use smallvec::SmallVec;
use std::convert::TryInto;
@ -166,7 +166,6 @@ struct EncoderState<K: DepKind> {
encoder: FileEncoder,
total_node_count: usize,
total_edge_count: usize,
result: FileEncodeResult,
stats: Option<FxHashMap<K, Stat<K>>>,
}
@ -176,7 +175,6 @@ impl<K: DepKind> EncoderState<K> {
encoder,
total_edge_count: 0,
total_node_count: 0,
result: Ok(()),
stats: record_stats.then(FxHashMap::default),
}
}
@ -208,29 +206,28 @@ impl<K: DepKind> EncoderState<K> {
}
let encoder = &mut self.encoder;
if self.result.is_ok() {
self.result = node.encode(encoder);
}
node.encode(encoder);
index
}
fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
let Self { mut encoder, total_node_count, total_edge_count, result, stats: _ } = self;
let () = result?;
let Self { mut encoder, total_node_count, total_edge_count, stats: _ } = self;
let node_count = total_node_count.try_into().unwrap();
let edge_count = total_edge_count.try_into().unwrap();
debug!(?node_count, ?edge_count);
debug!("position: {:?}", encoder.position());
IntEncodedWithFixedSize(node_count).encode(&mut encoder)?;
IntEncodedWithFixedSize(edge_count).encode(&mut encoder)?;
IntEncodedWithFixedSize(node_count).encode(&mut encoder);
IntEncodedWithFixedSize(edge_count).encode(&mut encoder);
debug!("position: {:?}", encoder.position());
// Drop the encoder so that nothing is written after the counts.
let result = encoder.flush();
// FIXME(rylev): we hardcode the dep graph file name so we don't need a dependency on
// rustc_incremental just for that.
profiler.artifact_size("dep_graph", "dep-graph.bin", encoder.position() as u64);
let result = encoder.finish();
if let Ok(position) = result {
// FIXME(rylev): we hardcode the dep graph file name so we
// don't need a dependency on rustc_incremental just for that.
profiler.artifact_size("dep_graph", "dep-graph.bin", position as u64);
}
result
}
}