Skip to content

Commit 821d8ea

Browse files
committed
Avoid constructing an anyhow::Error when not necessary
anyhow::Error always captures a backtrace when created, which is expensive
1 parent d5556ae commit 821d8ea

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ use std::cell::Cell;
5353
use std::collections::{BTreeMap, HashMap, HashSet};
5454
use std::io;
5555
use std::marker;
56-
use std::mem;
5756
use std::sync::Arc;
5857
use std::time::Duration;
5958

@@ -838,9 +837,10 @@ impl<'cfg> DrainState<'cfg> {
838837
let mut sender = FinishOnDrop {
839838
messages: &messages,
840839
id,
841-
result: Err(format_err!("worker panicked")),
840+
ok: false,
842841
};
843-
sender.result = job.run(&state);
842+
let result = job.run(&state);
843+
sender.ok = true;
844844

845845
// If the `rmeta_required` wasn't consumed but it was set
846846
// previously, then we either have:
@@ -854,25 +854,31 @@ impl<'cfg> DrainState<'cfg> {
854854
// we'll just naturally abort the compilation operation but for 1
855855
// we need to make sure that the metadata is flagged as produced so
856856
// send a synthetic message here.
857-
if state.rmeta_required.get() && sender.result.is_ok() {
857+
if state.rmeta_required.get() && result.is_ok() {
858858
messages.push(Message::Finish(id, Artifact::Metadata, Ok(())));
859859
}
860860

861+
messages.push(Message::Finish(id, Artifact::All, result));
862+
861863
// Use a helper struct with a `Drop` implementation to guarantee
862864
// that a `Finish` message is sent even if our job panics. We
863865
// shouldn't panic unless there's a bug in Cargo, so we just need
864866
// to make sure nothing hangs by accident.
865867
struct FinishOnDrop<'a> {
866868
messages: &'a Queue<Message>,
867869
id: JobId,
868-
result: CargoResult<()>,
870+
ok: bool,
869871
}
870872

871873
impl Drop for FinishOnDrop<'_> {
872874
fn drop(&mut self) {
873-
let msg = mem::replace(&mut self.result, Ok(()));
874-
self.messages
875-
.push(Message::Finish(self.id, Artifact::All, msg));
875+
if !self.ok {
876+
self.messages.push(Message::Finish(
877+
self.id,
878+
Artifact::All,
879+
Err(format_err!("worker panicked")),
880+
));
881+
}
876882
}
877883
}
878884
};

0 commit comments

Comments
 (0)