call Timer.done automatically using Drop trait. use throwaway less

This commit is contained in:
Dustin Carlino 2019-02-27 12:09:00 -08:00
parent f6a6bb888e
commit f249c6b2d9
15 changed files with 57 additions and 50 deletions

View File

@ -118,33 +118,8 @@ impl Timer {
self.warnings.push(line);
}
pub fn done(mut self) {
let stop_name = self.outermost_name.clone();
self.stop(&stop_name);
assert!(self.stack.is_empty());
println!();
for line in self.results {
println!("{}", line);
}
println!();
if !self.notes.is_empty() {
println!("{} notes:", self.notes.len());
for line in self.notes {
println!("{}", line);
}
println!();
}
notes::dump_notes();
if !self.warnings.is_empty() {
println!("{} warnings:", self.warnings.len());
for line in self.warnings {
println!("{}", line);
}
println!();
}
}
// Used to end the scope of a timer early.
pub fn done(self) {}
pub fn start(&mut self, name: &str) {
println!("{}...", name);
@ -258,6 +233,36 @@ impl Timer {
}
}
impl std::ops::Drop for Timer {
fn drop(&mut self) {
let stop_name = self.outermost_name.clone();
self.stop(&stop_name);
assert!(self.stack.is_empty());
println!();
for line in &self.results {
println!("{}", line);
}
println!();
if !self.notes.is_empty() {
println!("{} notes:", self.notes.len());
for line in &self.notes {
println!("{}", line);
}
println!();
}
notes::dump_notes();
if !self.warnings.is_empty() {
println!("{} warnings:", self.warnings.len());
for line in &self.warnings {
println!("{}", line);
}
println!();
}
}
}
// For repeated things
// TODO Why does the PartialEq derivation in sim require this?
#[derive(Default)]

View File

@ -9,5 +9,4 @@ fn main() {
timer.start("saving map");
abstutil::write_binary(&flags.output, &map).expect("serializing map failed");
timer.stop("saving map");
timer.done();
}

View File

@ -111,7 +111,8 @@ impl viewer::ObjectID for ID {
}
fn load_initial_map(filename: &str, canvas: &mut Canvas, prerender: &Prerender) -> World<ID> {
let data: raw_data::InitialMap = read_binary(filename, &mut Timer::throwaway()).unwrap();
let data: raw_data::InitialMap =
read_binary(filename, &mut Timer::new("load InitialMap")).unwrap();
let mut w = World::new(&data.bounds);

View File

@ -39,10 +39,14 @@ impl DebugPolygon {
.contextual_action(Key::F2, "debug sidewalk corners")
{
return Some(DebugPolygon {
items: calculate_corners(i, &ctx.primary.map, &mut Timer::throwaway())
.into_iter()
.map(Item::Polygon)
.collect(),
items: calculate_corners(
i,
&ctx.primary.map,
&mut Timer::new("calculate corners"),
)
.into_iter()
.map(Item::Polygon)
.collect(),
current: 0,
center: None,
});

View File

@ -194,7 +194,7 @@ impl BlockingPlugin for SpawnAgent {
}
}
};
sim.spawn_all_trips(map, &mut Timer::throwaway());
sim.spawn_all_trips(map, &mut Timer::new("spawn trip"));
return false;
}

View File

@ -47,14 +47,12 @@ impl BlockingPlugin for ScenarioManager {
} else if ctx.input.modal_action("edit") {
*self = ScenarioManager::EditScenario(scenario.clone(), Wizard::new());
} else if ctx.input.modal_action("instantiate") {
let mut timer = Timer::new("instantiate scenario");
scenario.instantiate(
&mut ctx.primary.sim,
&ctx.primary.map,
&mut ctx.primary.current_flags.sim_flags.make_rng(),
&mut timer,
&mut Timer::new("instantiate scenario"),
);
timer.done();
return false;
} else if scroller.event(&mut ctx.input) {
return false;

View File

@ -60,7 +60,7 @@ impl BlockingPlugin for StopSignEditor {
} else if input.modal_action("quit") {
return false;
} else if input.modal_action("reset to default") {
let sign = ControlStopSign::new(map, self.i, &mut Timer::throwaway());
let sign = ControlStopSign::new(map, self.i, &mut Timer::new("reset ControlStopSign"));
map.edit_stop_sign(sign);
}
true

View File

@ -144,7 +144,7 @@ impl AmbientPluginWithPrimaryPlugins for SimControls {
&mut ctx.primary.sim,
&ctx.primary.map,
&mut ctx.primary.current_flags.sim_flags.make_rng(),
&mut Timer::throwaway(),
&mut Timer::new("seed sim"),
);
*ctx.recalculate_current_selection = true;
}

View File

@ -291,7 +291,7 @@ impl DrawMap {
true,
cs,
prerender,
&mut Timer::throwaway(),
&mut Timer::new("recalculate DrawLane"),
);
}

View File

@ -484,7 +484,6 @@ impl PerMapUI {
current_flags: flags.clone(),
};
let plugins = PluginsPerMap::new(&state, &mut timer, enable_debug_controls);
timer.done();
(state, plugins)
}
}

View File

@ -160,6 +160,6 @@ fn setup_scenario(primary: &mut PerMapUI) {
&mut primary.sim,
map,
&mut primary.current_flags.sim_flags.make_rng(),
&mut Timer::throwaway(),
&mut Timer::new("setup tutorial"),
);
}

View File

@ -31,11 +31,11 @@ impl UI {
Map::new(
&flags.load_map,
MapEdits::new("map name"),
&mut Timer::throwaway(),
&mut Timer::new("load map"),
)
.unwrap()
} else {
abstutil::read_binary(&flags.load_map, &mut Timer::throwaway()).unwrap()
abstutil::read_binary(&flags.load_map, &mut Timer::new("load map")).unwrap()
};
UI {

View File

@ -4,16 +4,18 @@ use structopt::StructOpt;
fn main() {
let flags = kml::Flags::from_args();
let mut timer = abstutil::Timer::new("extracting shapes from KML");
// TODO don't hardcode
let mut bounds = GPSBounds::new();
bounds.update(LonLat::new(-122.4416, 47.5793));
bounds.update(LonLat::new(-122.2421, 47.7155));
let shapes = kml::load(&flags.input, &bounds, &mut timer).unwrap();
let shapes = kml::load(
&flags.input,
&bounds,
&mut abstutil::Timer::new("extracting shapes from KML"),
)
.unwrap();
println!("Writing to {}", flags.output);
abstutil::write_binary(&flags.output, &shapes).unwrap();
timer.done();
}

View File

@ -43,5 +43,4 @@ fn main() {
timer.start("save map");
map.save();
timer.stop("save map");
timer.done();
}

View File

@ -385,7 +385,7 @@ impl Model {
// TODO Directly use raw_data and get rid of Model? Might be more maintainable long-term.
pub fn import(path: &str) -> (Model, QuadTree<ID>) {
let data: raw_data::Map = read_binary(path, &mut Timer::throwaway()).unwrap();
let data: raw_data::Map = read_binary(path, &mut Timer::new("load map")).unwrap();
let gps_bounds = data.get_gps_bounds();
let mut m = Model::new();