about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPazzaz <pazzaz.sundqvist@gmail.com>2018-06-16 20:19:19 +0200
committerPazzaz <pazzaz.sundqvist@gmail.com>2018-06-16 20:56:17 +0200
commitd22ad76ca83acda1428829173451eee0221f685a (patch)
tree5180e42bd753dbcbe9583e7f7b95ced01b51cd91
parent253205658edc477a0b429f3ce25a92099dc7ddc4 (diff)
downloadrust-d22ad76ca83acda1428829173451eee0221f685a.tar.gz
rust-d22ad76ca83acda1428829173451eee0221f685a.zip
Optimize sum of Durations by using custom function
-rw-r--r--src/libcore/tests/time.rs14
-rw-r--r--src/libcore/time.rs34
2 files changed, 46 insertions, 2 deletions
diff --git a/src/libcore/tests/time.rs b/src/libcore/tests/time.rs
index df139965753..466f28f0ef0 100644
--- a/src/libcore/tests/time.rs
+++ b/src/libcore/tests/time.rs
@@ -162,6 +162,20 @@ fn checked_div() {
 }
 
 #[test]
+fn correct_sum() {
+    let durations = [
+        Duration::new(1, 999_999_999),
+        Duration::new(2, 999_999_999),
+        Duration::new(0, 999_999_999),
+        Duration::new(0, 999_999_999),
+        Duration::new(0, 999_999_999),
+        Duration::new(5, 0),
+    ];
+    let sum = durations.iter().sum::<Duration>();
+    assert_eq!(sum, Duration::new(1+2+5+4, 1_000_000_000 - 5));
+}
+
+#[test]
 fn debug_formatting_extreme_values() {
     assert_eq!(
         format!("{:?}", Duration::new(18_446_744_073_709_551_615, 123_456_789)),
diff --git a/src/libcore/time.rs b/src/libcore/time.rs
index 563eea0066d..25721b7fcec 100644
--- a/src/libcore/time.rs
+++ b/src/libcore/time.rs
@@ -524,17 +524,47 @@ impl DivAssign<u32> for Duration {
     }
 }
 
+macro_rules! sum_durations {
+    ($iter:expr) => {{
+        let mut total_secs: u64 = 0;
+        let mut total_nanos: u64 = 0;
+
+        for entry in $iter {
+            total_secs = total_secs
+                .checked_add(entry.secs)
+                .expect("overflow in iter::sum over durations");
+            total_nanos = match total_nanos.checked_add(entry.nanos as u64) {
+                Some(n) => n,
+                None => {
+                    total_secs = total_secs
+                        .checked_add(total_nanos / NANOS_PER_SEC as u64)
+                        .expect("overflow in iter::sum over durations");
+                    (total_nanos % NANOS_PER_SEC as u64) + entry.nanos as u64
+                }
+            };
+        }
+        total_secs = total_secs
+            .checked_add(total_nanos / NANOS_PER_SEC as u64)
+            .expect("overflow in iter::sum over durations");
+        total_nanos = total_nanos % NANOS_PER_SEC as u64;
+        Duration {
+            secs: total_secs,
+            nanos: total_nanos as u32,
+        }
+    }};
+}
+
 #[stable(feature = "duration_sum", since = "1.16.0")]
 impl Sum for Duration {
     fn sum<I: Iterator<Item=Duration>>(iter: I) -> Duration {
-        iter.fold(Duration::new(0, 0), |a, b| a + b)
+        sum_durations!(iter)
     }
 }
 
 #[stable(feature = "duration_sum", since = "1.16.0")]
 impl<'a> Sum<&'a Duration> for Duration {
     fn sum<I: Iterator<Item=&'a Duration>>(iter: I) -> Duration {
-        iter.fold(Duration::new(0, 0), |a, b| a + *b)
+        sum_durations!(iter)
     }
 }