about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-02-08 18:46:27 -0800
committerbors <bors@rust-lang.org>2014-02-08 18:46:27 -0800
commitfddc18ec4b8a9c261ae732dde1120d663599ef64 (patch)
tree0cd1d941d571c4347debfdd6a140ff4f5accadad
parenta2290db7971a1e14e4d15316cecaf5c33cc6b0ba (diff)
parent38447344f1f1dc3c3bb0cbcf361deee49e2f9828 (diff)
auto merge of #12105 : huonw/rust/bench-black-box, r=alexcrichton
This allows a result to be marked as "used" by passing it to a function
LLVM cannot see inside (unless LTO is enabled).

Closes #8261.
-rw-r--r--src/libarena/lib.rs18
-rw-r--r--src/libextra/lib.rs2
-rw-r--r--src/libextra/test.rs16
3 files changed, 24 insertions, 12 deletions
diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs
index 856540989df..3e3ecdda3b0 100644
--- a/src/libarena/lib.rs
+++ b/src/libarena/lib.rs
@@ -536,18 +536,18 @@ mod test {
                 x: 1,
                 y: 2,
                 z: 3,
-            });
+            })
         })
     }
 
     #[bench]
     pub fn bench_pod_nonarena(bh: &mut BenchHarness) {
         bh.iter(|| {
-            let _ = ~Point {
+            ~Point {
                 x: 1,
                 y: 2,
                 z: 3,
-            };
+            }
         })
     }
 
@@ -561,7 +561,7 @@ mod test {
                     y: 2,
                     z: 3,
                 }
-            });
+            })
         })
     }
 
@@ -588,17 +588,17 @@ mod test {
             arena.alloc(Nonpod {
                 string: ~"hello world",
                 array: ~[ 1, 2, 3, 4, 5 ],
-            });
+            })
         })
     }
 
     #[bench]
     pub fn bench_nonpod_nonarena(bh: &mut BenchHarness) {
         bh.iter(|| {
-            let _ = ~Nonpod {
+            ~Nonpod {
                 string: ~"hello world",
                 array: ~[ 1, 2, 3, 4, 5 ],
-            };
+            }
         })
     }
 
@@ -606,10 +606,10 @@ mod test {
     pub fn bench_nonpod_old_arena(bh: &mut BenchHarness) {
         let arena = Arena::new();
         bh.iter(|| {
-            let _ = arena.alloc(|| Nonpod {
+            arena.alloc(|| Nonpod {
                 string: ~"hello world",
                 array: ~[ 1, 2, 3, 4, 5 ],
-            });
+            })
         })
     }
 }
diff --git a/src/libextra/lib.rs b/src/libextra/lib.rs
index 519192fd177..109bf2e489b 100644
--- a/src/libextra/lib.rs
+++ b/src/libextra/lib.rs
@@ -29,7 +29,7 @@ Rust extras are part of the standard Rust distribution.
       html_favicon_url = "http://www.rust-lang.org/favicon.ico",
       html_root_url = "http://static.rust-lang.org/doc/master")];
 
-#[feature(macro_rules, globs, managed_boxes)];
+#[feature(macro_rules, globs, managed_boxes, asm)];
 
 #[deny(non_camel_case_types)];
 #[deny(missing_doc)];
diff --git a/src/libextra/test.rs b/src/libextra/test.rs
index 2d52a59c836..d207bd2298b 100644
--- a/src/libextra/test.rs
+++ b/src/libextra/test.rs
@@ -1091,13 +1091,25 @@ impl MetricMap {
 
 // Benchmarking
 
+/// A function that is opaque to the optimiser, to allow benchmarks to
+/// pretend to use outputs to assist in avoiding dead-code
+/// elimination.
+///
+/// This function is a no-op, and does not even read from `dummy`.
+pub fn black_box<T>(dummy: T) {
+    // we need to "use" the argument in some way LLVM can't
+    // introspect.
+    unsafe {asm!("" : : "r"(&dummy))}
+}
+
+
 impl BenchHarness {
     /// Callback for benchmark functions to run in their body.
-    pub fn iter(&mut self, inner: ||) {
+    pub fn iter<T>(&mut self, inner: || -> T) {
         self.ns_start = precise_time_ns();
         let k = self.iterations;
         for _ in range(0u64, k) {
-            inner();
+            black_box(inner());
         }
         self.ns_end = precise_time_ns();
     }