From 2cc81ac531c10a678ee25128a36fdc56ba00aaa3 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sat, 3 Jan 2015 02:49:42 -0600 Subject: [PATCH 1/2] Return passed value from black_box By returning the passed value black_box can be used on data being passed to a function being benchmarked. This ensures the compiler does not optimize the function for the input which could result in the entire function being optimized away. --- src/libtest/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index c4cb53d6cb7b6..c00246598c303 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -1332,10 +1332,11 @@ impl MetricMap { /// elimination. /// /// This function is a no-op, and does not even read from `dummy`. -pub fn black_box(dummy: T) { +pub fn black_box(dummy: T) -> T { // we need to "use" the argument in some way LLVM can't // introspect. unsafe {asm!("" : : "r"(&dummy))} + dummy } From 9f5fc563d3d592d5e44ab71759ffa26d326b3daa Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Tue, 13 Jan 2015 05:43:30 -0600 Subject: [PATCH 2/2] Fix test failures --- src/libcollections/bit.rs | 4 ++-- src/libcollections/str.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index c1b34c52fcc15..82e5af67ae316 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -2526,7 +2526,7 @@ mod bitv_bench { for _ in range(0u, 100) { bitv |= 1 << ((r.next_u32() as uint) % u32::BITS); } - black_box(&bitv) + black_box(&bitv); }); } @@ -2538,7 +2538,7 @@ mod bitv_bench { for _ in range(0u, 100) { bitv.set((r.next_u32() as uint) % BENCH_BITS, true); } - black_box(&bitv) + black_box(&bitv); }); } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index ccf654ac0a048..7aa87ffaae260 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -2841,7 +2841,7 @@ mod bench { let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb"; b.iter(|| { - for ch in s.chars() { black_box(ch) } + for ch in s.chars() { black_box(ch); } }); } @@ -2869,7 +2869,7 @@ mod bench { let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb"; b.iter(|| { - for ch in s.chars().rev() { black_box(ch) } + for ch in s.chars().rev() { black_box(ch); } }); }