From 53492ec5133c1452accea72174d8f4bbd9cace34 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 28 Dec 2015 20:05:57 +0000 Subject: [PATCH 1/3] Remove the deprecated Duration::span MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … so that Duration can be moved into libcore. The removal will hit stable in 1.7 at the soonest, while 1.6 already has the deprecation warning. --- src/libstd/time/duration.rs | 16 ---------------- src/test/bench/core-map.rs | 10 ++++++---- src/test/bench/core-set.rs | 8 +++++--- src/test/bench/core-std.rs | 8 +++++--- src/test/bench/msgsend-pipes-shared.rs | 10 ++++++---- src/test/bench/msgsend-pipes.rs | 10 ++++++---- src/test/bench/msgsend-ring-mutex-arcs.rs | 10 ++++++---- src/test/bench/shootout-pfib.rs | 10 +++++----- src/test/bench/task-perf-alloc-unwind.rs | 15 +++++++-------- 9 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index ad1be82d6d842..f1bbaa5e2ea8b 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -9,7 +9,6 @@ // except according to those terms. use ops::{Add, Sub, Mul, Div}; -use time::Instant; const NANOS_PER_SEC: u32 = 1_000_000_000; const NANOS_PER_MILLI: u32 = 1_000_000; @@ -59,21 +58,6 @@ impl Duration { Duration { secs: secs, nanos: nanos } } - /// Runs a closure, returning the duration of time it took to run the - /// closure. - #[unstable(feature = "duration_span", - reason = "unsure if this is the right API or whether it should \ - wait for a more general \"moment in time\" \ - abstraction", - issue = "27799")] - #[rustc_deprecated(reason = "use std::time::Instant instead", - since = "1.6.0")] - pub fn span(f: F) -> Duration where F: FnOnce() { - let start = Instant::now(); - f(); - start.elapsed() - } - /// Creates a new `Duration` from the specified number of seconds. #[stable(feature = "duration", since = "1.3.0")] pub fn from_secs(secs: u64) -> Duration { diff --git a/src/test/bench/core-map.rs b/src/test/bench/core-map.rs index a72e348c72018..468ce0b7a24ec 100644 --- a/src/test/bench/core-map.rs +++ b/src/test/bench/core-map.rs @@ -8,15 +8,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(std_misc, rand, duration, duration_span)] +#![feature(std_misc, rand, time2)] use std::collections::{BTreeMap, HashMap, HashSet}; use std::env; use std::__rand::{Rng, thread_rng}; -use std::time::Duration; +use std::time::Instant; -fn timed(label: &str, f: F) where F: FnMut() { - println!(" {}: {:?}", label, Duration::span(f)); +fn timed(label: &str, mut f: F) where F: FnMut() { + let start = Instant::now(); + f(); + println!(" {}: {:?}", label, start.elapsed()); } trait MutableMap { diff --git a/src/test/bench/core-set.rs b/src/test/bench/core-set.rs index 28ae990e05a2d..14e15ea1e4b25 100644 --- a/src/test/bench/core-set.rs +++ b/src/test/bench/core-set.rs @@ -10,7 +10,7 @@ // ignore-pretty very bad with line comments -#![feature(unboxed_closures, rand, std_misc, collections, duration, duration_span)] +#![feature(unboxed_closures, rand, std_misc, collections, time2)] #![feature(bitset)] extern crate collections; @@ -20,7 +20,7 @@ use std::collections::BTreeSet; use std::collections::HashSet; use std::hash::Hash; use std::env; -use std::time::Duration; +use std::time::{Duration, Instant}; struct Results { sequential_ints: Duration, @@ -33,7 +33,9 @@ struct Results { } fn timed(result: &mut Duration, op: F) where F: FnOnce() { - *result = Duration::span(op); + let start = Instant::now(); + op(); + *result = start.elapsed(); } trait MutableSet { diff --git a/src/test/bench/core-std.rs b/src/test/bench/core-std.rs index 26fb3630487f2..c34e7582e24c7 100644 --- a/src/test/bench/core-std.rs +++ b/src/test/bench/core-std.rs @@ -10,13 +10,13 @@ // Microbenchmarks for various functions in std and extra -#![feature(rand, vec_push_all, duration, duration_span)] +#![feature(rand, vec_push_all, time2)] use std::mem::swap; use std::env; use std::__rand::{thread_rng, Rng}; use std::str; -use std::time::Duration; +use std::time::Instant; fn main() { let argv: Vec = env::args().collect(); @@ -49,7 +49,9 @@ fn maybe_run_test(argv: &[String], name: String, test: F) where F: FnOnce() { return } - let dur = Duration::span(test); + let start = Instant::now(); + test(); + let dur = start.elapsed(); println!("{}:\t\t{:?}", name, dur); } diff --git a/src/test/bench/msgsend-pipes-shared.rs b/src/test/bench/msgsend-pipes-shared.rs index b210f5ac26ae0..b3ec15a989425 100644 --- a/src/test/bench/msgsend-pipes-shared.rs +++ b/src/test/bench/msgsend-pipes-shared.rs @@ -18,12 +18,12 @@ // different scalability characteristics compared to the select // version. -#![feature(duration, duration_span)] +#![feature(time2)] use std::sync::mpsc::{channel, Sender, Receiver}; use std::env; use std::thread; -use std::time::Duration; +use std::time::Instant; fn move_out(_x: T) {} @@ -60,7 +60,8 @@ fn run(args: &[String]) { let num_bytes = 100; let mut result = None; let mut p = Some((to_child, to_parent, from_parent)); - let dur = Duration::span(|| { + let start = Instant::now(); + { let (to_child, to_parent, from_parent) = p.take().unwrap(); let mut worker_results = Vec::new(); for _ in 0..workers { @@ -85,7 +86,8 @@ fn run(args: &[String]) { to_child.send(request::stop).unwrap(); move_out(to_child); result = Some(from_child.recv().unwrap()); - }); + } + let dur = start.elapsed(); let result = result.unwrap(); print!("Count is {}\n", result); print!("Test took {:?}\n", dur); diff --git a/src/test/bench/msgsend-pipes.rs b/src/test/bench/msgsend-pipes.rs index 056905f1ef691..ae2448934beb3 100644 --- a/src/test/bench/msgsend-pipes.rs +++ b/src/test/bench/msgsend-pipes.rs @@ -14,12 +14,12 @@ // // I *think* it's the same, more or less. -#![feature(duration, duration_span)] +#![feature(time2)] use std::sync::mpsc::{channel, Sender, Receiver}; use std::env; use std::thread; -use std::time::Duration; +use std::time::Instant; enum request { get_count, @@ -53,7 +53,8 @@ fn run(args: &[String]) { let num_bytes = 100; let mut result = None; let mut to_parent = Some(to_parent); - let dur = Duration::span(|| { + let start = Instant::now(); + { let to_parent = to_parent.take().unwrap(); let mut worker_results = Vec::new(); let from_parent = if workers == 1 { @@ -92,7 +93,8 @@ fn run(args: &[String]) { //to_child.send(stop); //move_out(to_child); result = Some(from_child.recv().unwrap()); - }); + } + let dur = start.elapsed(); let result = result.unwrap(); print!("Count is {}\n", result); print!("Test took {:?}\n", dur); diff --git a/src/test/bench/msgsend-ring-mutex-arcs.rs b/src/test/bench/msgsend-ring-mutex-arcs.rs index 843f49d8f0534..bac8db9601d8b 100644 --- a/src/test/bench/msgsend-ring-mutex-arcs.rs +++ b/src/test/bench/msgsend-ring-mutex-arcs.rs @@ -17,11 +17,11 @@ // no-pretty-expanded FIXME #15189 -#![feature(duration_span)] +#![feature(time2)] use std::env; use std::sync::{Arc, Mutex, Condvar}; -use std::time::Duration; +use std::time::Instant; use std::thread; // A poor man's pipe. @@ -80,7 +80,8 @@ fn main() { let (num_chan, num_port) = init(); let mut p = Some((num_chan, num_port)); - let dur = Duration::span(|| { + let start = Instant::now(); + { let (mut num_chan, num_port) = p.take().unwrap(); // create the ring @@ -104,7 +105,8 @@ fn main() { for f in futures { f.join().unwrap() } - }); + } + let dur = start.elapsed(); // all done, report stats. let num_msgs = num_tasks * msg_per_task; diff --git a/src/test/bench/shootout-pfib.rs b/src/test/bench/shootout-pfib.rs index b7553dc7c7043..da52ccd9dea92 100644 --- a/src/test/bench/shootout-pfib.rs +++ b/src/test/bench/shootout-pfib.rs @@ -18,7 +18,7 @@ */ -#![feature(duration, duration_span, rustc_private)] +#![feature(time2, rustc_private)] extern crate getopts; @@ -26,7 +26,7 @@ use std::sync::mpsc::{channel, Sender}; use std::env; use std::result::Result::{Ok, Err}; use std::thread; -use std::time::Duration; +use std::time::Instant; fn fib(n: isize) -> isize { fn pfib(tx: &Sender, n: isize) { @@ -110,9 +110,9 @@ fn main() { for n in 1..max + 1 { for _ in 0..num_trials { - let mut fibn = None; - let dur = Duration::span(|| fibn = Some(fib(n))); - let fibn = fibn.unwrap(); + let start = Instant::now(); + let fibn = fib(n); + let dur = start.elapsed(); println!("{}\t{}\t{:?}", n, fibn, dur); } diff --git a/src/test/bench/task-perf-alloc-unwind.rs b/src/test/bench/task-perf-alloc-unwind.rs index 5f34d0f630149..86180676e6597 100644 --- a/src/test/bench/task-perf-alloc-unwind.rs +++ b/src/test/bench/task-perf-alloc-unwind.rs @@ -8,11 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(box_syntax, duration, duration_span, vec_push_all)] +#![feature(box_syntax, time2, vec_push_all)] use std::env; use std::thread; -use std::time::Duration; +use std::time::Instant; #[derive(Clone)] enum List { @@ -31,12 +31,11 @@ fn main() { fn run(repeat: isize, depth: isize) { for _ in 0..repeat { - let dur = Duration::span(|| { - let _ = thread::spawn(move|| { - recurse_or_panic(depth, None) - }).join(); - }); - println!("iter: {:?}", dur); + let start = Instant::now(); + let _ = thread::spawn(move|| { + recurse_or_panic(depth, None) + }).join(); + println!("iter: {:?}", start.elapsed()); } } From ac249c14617ae6dda412965a890fe93de8c92784 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 28 Dec 2015 20:36:00 +0000 Subject: [PATCH 2/3] Move Duration to libcore. --- src/libcore/lib.rs | 1 + .../time/duration.rs => libcore/time.rs} | 82 +----------------- src/libcoretest/lib.rs | 1 + src/libcoretest/time.rs | 84 +++++++++++++++++++ src/libstd/time/mod.rs | 4 +- 5 files changed, 91 insertions(+), 81 deletions(-) rename src/{libstd/time/duration.rs => libcore/time.rs} (69%) create mode 100644 src/libcoretest/time.rs diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index e8803976937d2..1e6d5bfad56aa 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -140,6 +140,7 @@ pub mod iter; pub mod option; pub mod raw; pub mod result; +pub mod time; pub mod slice; pub mod str; diff --git a/src/libstd/time/duration.rs b/src/libcore/time.rs similarity index 69% rename from src/libstd/time/duration.rs rename to src/libcore/time.rs index f1bbaa5e2ea8b..0e06040d7af04 100644 --- a/src/libstd/time/duration.rs +++ b/src/libcore/time.rs @@ -8,6 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! Temporal quantification. + +#![stable(feature = "time", since = "1.3.0")] + use ops::{Add, Sub, Mul, Div}; const NANOS_PER_SEC: u32 = 1_000_000_000; @@ -154,81 +158,3 @@ impl Div for Duration { Duration { secs: secs, nanos: nanos } } } - -#[cfg(test)] -mod tests { - use super::Duration; - - #[test] - fn creation() { - assert!(Duration::from_secs(1) != Duration::from_secs(0)); - assert_eq!(Duration::from_secs(1) + Duration::from_secs(2), - Duration::from_secs(3)); - assert_eq!(Duration::from_millis(10) + Duration::from_secs(4), - Duration::new(4, 10 * 1_000_000)); - assert_eq!(Duration::from_millis(4000), Duration::new(4, 0)); - } - - #[test] - fn secs() { - assert_eq!(Duration::new(0, 0).as_secs(), 0); - assert_eq!(Duration::from_secs(1).as_secs(), 1); - assert_eq!(Duration::from_millis(999).as_secs(), 0); - assert_eq!(Duration::from_millis(1001).as_secs(), 1); - } - - #[test] - fn nanos() { - assert_eq!(Duration::new(0, 0).subsec_nanos(), 0); - assert_eq!(Duration::new(0, 5).subsec_nanos(), 5); - assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1); - assert_eq!(Duration::from_secs(1).subsec_nanos(), 0); - assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000); - assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000); - } - - #[test] - fn add() { - assert_eq!(Duration::new(0, 0) + Duration::new(0, 1), - Duration::new(0, 1)); - assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001), - Duration::new(1, 1)); - } - - #[test] - fn sub() { - assert_eq!(Duration::new(0, 1) - Duration::new(0, 0), - Duration::new(0, 1)); - assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000), - Duration::new(0, 1)); - assert_eq!(Duration::new(1, 0) - Duration::new(0, 1), - Duration::new(0, 999_999_999)); - } - - #[test] #[should_panic] - fn sub_bad1() { - Duration::new(0, 0) - Duration::new(0, 1); - } - - #[test] #[should_panic] - fn sub_bad2() { - Duration::new(0, 0) - Duration::new(1, 0); - } - - #[test] - fn mul() { - assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2)); - assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3)); - assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4)); - assert_eq!(Duration::new(0, 500_000_001) * 4000, - Duration::new(2000, 4000)); - } - - #[test] - fn div() { - assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0)); - assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333)); - assert_eq!(Duration::new(99, 999_999_000) / 100, - Duration::new(0, 999_999_990)); - } -} diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 88f1835d2cce4..a3b8887c0dbe7 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -71,4 +71,5 @@ mod ptr; mod result; mod slice; mod str; +mod time; mod tuple; diff --git a/src/libcoretest/time.rs b/src/libcoretest/time.rs new file mode 100644 index 0000000000000..422227d74324f --- /dev/null +++ b/src/libcoretest/time.rs @@ -0,0 +1,84 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::time::Duration; + +#[test] +fn creation() { + assert!(Duration::from_secs(1) != Duration::from_secs(0)); + assert_eq!(Duration::from_secs(1) + Duration::from_secs(2), + Duration::from_secs(3)); + assert_eq!(Duration::from_millis(10) + Duration::from_secs(4), + Duration::new(4, 10 * 1_000_000)); + assert_eq!(Duration::from_millis(4000), Duration::new(4, 0)); +} + +#[test] +fn secs() { + assert_eq!(Duration::new(0, 0).as_secs(), 0); + assert_eq!(Duration::from_secs(1).as_secs(), 1); + assert_eq!(Duration::from_millis(999).as_secs(), 0); + assert_eq!(Duration::from_millis(1001).as_secs(), 1); +} + +#[test] +fn nanos() { + assert_eq!(Duration::new(0, 0).subsec_nanos(), 0); + assert_eq!(Duration::new(0, 5).subsec_nanos(), 5); + assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1); + assert_eq!(Duration::from_secs(1).subsec_nanos(), 0); + assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000); + assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000); +} + +#[test] +fn add() { + assert_eq!(Duration::new(0, 0) + Duration::new(0, 1), + Duration::new(0, 1)); + assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001), + Duration::new(1, 1)); +} + +#[test] +fn sub() { + assert_eq!(Duration::new(0, 1) - Duration::new(0, 0), + Duration::new(0, 1)); + assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000), + Duration::new(0, 1)); + assert_eq!(Duration::new(1, 0) - Duration::new(0, 1), + Duration::new(0, 999_999_999)); +} + +#[test] #[should_panic] +fn sub_bad1() { + Duration::new(0, 0) - Duration::new(0, 1); +} + +#[test] #[should_panic] +fn sub_bad2() { + Duration::new(0, 0) - Duration::new(1, 0); +} + +#[test] +fn mul() { + assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2)); + assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3)); + assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4)); + assert_eq!(Duration::new(0, 500_000_001) * 4000, + Duration::new(2000, 4000)); +} + +#[test] +fn div() { + assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0)); + assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333)); + assert_eq!(Duration::new(99, 999_999_000) / 100, + Duration::new(0, 999_999_990)); +} diff --git a/src/libstd/time/mod.rs b/src/libstd/time/mod.rs index e3ce8e0de4b12..9aaaf1bbd6249 100644 --- a/src/libstd/time/mod.rs +++ b/src/libstd/time/mod.rs @@ -18,9 +18,7 @@ use ops::{Add, Sub}; use sys::time; #[stable(feature = "time", since = "1.3.0")] -pub use self::duration::Duration; - -mod duration; +pub use core::time::Duration; /// A measurement of a monotonically increasing clock. /// From 54d2eb78e7d2bd66b245ae61425fe8f216a5ea16 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 1 Jan 2016 20:28:21 +0100 Subject: [PATCH 3/3] Make core::time unstable (but keep the std::time::Duration reexport stable) --- src/libcore/time.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 0e06040d7af04..f6447de60b3a2 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -10,7 +10,8 @@ //! Temporal quantification. -#![stable(feature = "time", since = "1.3.0")] +#![unstable(feature = "time", reason = "recently moved to libcore", + issue = "0")] // FIXME: file tracking issue if this lands use ops::{Add, Sub, Mul, Div};