Skip to content

Commit

Permalink
Return passed value from black_box
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amaranth committed Jan 13, 2015
1 parent 3d5fbae commit 2cc81ac
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libtest/lib.rs
Expand Up @@ -1332,10 +1332,11 @@ impl MetricMap {
/// elimination.
///
/// This function is a no-op, and does not even read from `dummy`.
pub fn black_box<T>(dummy: T) {
pub fn black_box<T>(dummy: T) -> T {
// we need to "use" the argument in some way LLVM can't
// introspect.
unsafe {asm!("" : : "r"(&dummy))}
dummy
}


Expand Down

0 comments on commit 2cc81ac

Please sign in to comment.