Skip to content

Commit 6c6aa97

Browse files
committed
Remove Option from Timings.
1 parent 0df0595 commit 6c6aa97

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct JobQueue<'a, 'cfg> {
4040
is_release: bool,
4141
progress: Progress<'cfg>,
4242
next_id: u32,
43-
timings: Option<Timings<'a, 'cfg>>,
43+
timings: Timings<'a, 'cfg>,
4444
}
4545

4646
pub struct JobState<'a> {
@@ -132,10 +132,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
132132
pub fn new(bcx: &BuildContext<'a, 'cfg>, root_units: &[Unit<'a>]) -> JobQueue<'a, 'cfg> {
133133
let (tx, rx) = channel();
134134
let progress = Progress::with_style("Building", ProgressStyle::Ratio, bcx.config);
135-
let timings = match bcx.config.cli_unstable().timings {
136-
Some(..) => Some(Timings::new(bcx, root_units)),
137-
None => None,
138-
};
135+
let timings = Timings::new(bcx, root_units);
139136
JobQueue {
140137
queue: DependencyQueue::new(),
141138
tx,
@@ -322,9 +319,8 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
322319
// to the jobserver itself.
323320
tokens.truncate(self.active.len() - 1);
324321

325-
if let Some(t) = &mut self.timings {
326-
t.mark_concurrency(self.active.len(), queue.len(), self.queue.len());
327-
}
322+
self.timings
323+
.mark_concurrency(self.active.len(), queue.len(), self.queue.len());
328324

329325
// Drain all events at once to avoid displaying the progress bar
330326
// unnecessarily.
@@ -343,9 +339,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
343339
.config
344340
.shell()
345341
.verbose(|c| c.status("Running", &cmd))?;
346-
if let Some(t) = &mut self.timings {
347-
t.unit_start(id, self.active[&id]);
348-
}
342+
self.timings.unit_start(id, self.active[&id]);
349343
}
350344
Message::BuildPlanMsg(module_name, cmd, filenames) => {
351345
plan.update(&module_name, &cmd, &filenames)?;
@@ -437,9 +431,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
437431
if !cx.bcx.build_config.build_plan {
438432
cx.bcx.config.shell().status("Finished", message)?;
439433
}
440-
if let Some(t) = &mut self.timings {
441-
t.finished()?;
442-
}
434+
self.timings.finished()?;
443435
Ok(())
444436
} else {
445437
debug!("queue: {:#?}", self.queue);
@@ -535,15 +527,11 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
535527

536528
match fresh {
537529
Freshness::Fresh => {
538-
if let Some(t) = &mut self.timings {
539-
t.add_fresh();
540-
}
530+
self.timings.add_fresh();
541531
doit()
542532
}
543533
Freshness::Dirty => {
544-
if let Some(t) = &mut self.timings {
545-
t.add_dirty();
546-
}
534+
self.timings.add_dirty();
547535
scope.spawn(move |_| doit());
548536
}
549537
}
@@ -590,11 +578,9 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
590578
self.emit_warnings(None, unit, cx)?;
591579
}
592580
let unlocked = self.queue.finish(unit, &artifact);
593-
if let Some(t) = &mut self.timings {
594-
match artifact {
595-
Artifact::All => t.unit_finished(id, unlocked),
596-
Artifact::Metadata => t.unit_rmeta_finished(id, unlocked),
597-
}
581+
match artifact {
582+
Artifact::All => self.timings.unit_finished(id, unlocked),
583+
Artifact::Metadata => self.timings.unit_rmeta_finished(id, unlocked),
598584
}
599585
Ok(())
600586
}

src/cargo/core/compiler/timings.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::time::{Duration, Instant, SystemTime};
1414

1515
pub struct Timings<'a, 'cfg> {
1616
config: &'cfg Config,
17+
/// Whether or not timings should be captured.
18+
enabled: bool,
1719
/// If true, saves an HTML report to disk.
1820
report_html: bool,
1921
/// If true, reports unit completion to stderr.
@@ -80,6 +82,18 @@ struct Concurrency {
8082

8183
impl<'a, 'cfg> Timings<'a, 'cfg> {
8284
pub fn new(bcx: &BuildContext<'a, 'cfg>, root_units: &[Unit<'_>]) -> Timings<'a, 'cfg> {
85+
let has_report = |what| {
86+
bcx.config
87+
.cli_unstable()
88+
.timings
89+
.as_ref()
90+
.map_or(false, |t| t.iter().any(|opt| opt == what))
91+
};
92+
let report_html = has_report("html");
93+
let report_info = has_report("info");
94+
let report_json = has_report("json");
95+
let enabled = report_html | report_info | report_json;
96+
8397
let mut root_map: HashMap<PackageId, Vec<String>> = HashMap::new();
8498
for unit in root_units {
8599
let target_desc = unit.target.description_named();
@@ -96,13 +110,6 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
96110
})
97111
.collect();
98112
let start_str = humantime::format_rfc3339_seconds(SystemTime::now()).to_string();
99-
let has_report = |what| {
100-
bcx.config
101-
.cli_unstable()
102-
.timings
103-
.as_ref()
104-
.map_or(false, |t| t.iter().any(|opt| opt == what))
105-
};
106113
let rustc_info = render_rustc_info(bcx);
107114
let profile = if bcx.build_config.release {
108115
"release"
@@ -113,9 +120,10 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
113120

114121
Timings {
115122
config: bcx.config,
116-
report_html: has_report("html"),
117-
report_info: has_report("info"),
118-
report_json: has_report("json"),
123+
enabled,
124+
report_html,
125+
report_info,
126+
report_json,
119127
start: bcx.config.creation_time(),
120128
start_str,
121129
rustc_info,
@@ -131,6 +139,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
131139

132140
/// Mark that a unit has started running.
133141
pub fn unit_start(&mut self, id: u32, unit: Unit<'a>) {
142+
if !self.enabled {
143+
return;
144+
}
134145
let mut target = if unit.target.is_lib() && unit.mode == CompileMode::Build {
135146
// Special case for brevity, since most dependencies hit
136147
// this path.
@@ -162,6 +173,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
162173

163174
/// Mark that the `.rmeta` file as generated.
164175
pub fn unit_rmeta_finished(&mut self, id: u32, unlocked: Vec<&Unit<'a>>) {
176+
if !self.enabled {
177+
return;
178+
}
165179
// `id` may not always be active. "fresh" units unconditionally
166180
// generate `Message::Finish`, but this active map only tracks dirty
167181
// units.
@@ -175,6 +189,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
175189

176190
/// Mark that a unit has finished running.
177191
pub fn unit_finished(&mut self, id: u32, unlocked: Vec<&Unit<'a>>) {
192+
if !self.enabled {
193+
return;
194+
}
178195
// See note above in `unit_rmeta_finished`, this may not always be active.
179196
if let Some(mut unit_time) = self.active.remove(&id) {
180197
let t = d_as_f64(self.start.elapsed());
@@ -210,6 +227,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
210227

211228
/// This is called periodically to mark the concurrency of internal structures.
212229
pub fn mark_concurrency(&mut self, active: usize, waiting: usize, inactive: usize) {
230+
if !self.enabled {
231+
return;
232+
}
213233
let c = Concurrency {
214234
t: d_as_f64(self.start.elapsed()),
215235
active,
@@ -231,6 +251,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
231251

232252
/// Call this when all units are finished.
233253
pub fn finished(&mut self) -> CargoResult<()> {
254+
if !self.enabled {
255+
return Ok(());
256+
}
234257
self.mark_concurrency(0, 0, 0);
235258
self.unit_times
236259
.sort_unstable_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
@@ -241,7 +264,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
241264
}
242265

243266
/// Save HTML report to disk.
244-
pub fn report_html(&self) -> CargoResult<()> {
267+
fn report_html(&self) -> CargoResult<()> {
245268
let duration = self.start.elapsed().as_secs() as u32 + 1;
246269
let timestamp = self.start_str.replace(&['-', ':'][..], "");
247270
let filename = format!("cargo-timing-{}.html", timestamp);

0 commit comments

Comments
 (0)