Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reexport std::hint::black_box #701

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -18,6 +18,7 @@ keywords = ["criterion", "benchmark"]
categories = ["development-tools::profiling"]
license = "Apache-2.0 OR MIT"
exclude = ["book/*"]
rust-version = "1.66"

[dependencies]
anes = "0.1.4"
Expand Down Expand Up @@ -71,7 +72,7 @@ stable = [
"async_tokio",
"async_std",
]
default = ["rayon", "plotters", "cargo_bench_support"]
default = ["rayon", "plotters", "cargo_bench_support", "real_blackbox"]

# Enable use of the nightly-only test::black_box function to discourage compiler optimizations.
real_blackbox = []
Expand Down
25 changes: 4 additions & 21 deletions src/lib.rs
Expand Up @@ -17,7 +17,6 @@

#![warn(missing_docs)]
#![warn(bare_trait_objects)]
#![cfg_attr(feature = "real_blackbox", feature(test))]
#![allow(
clippy::just_underscores_and_digits, // Used in the stats code
clippy::transmute_ptr_to_ptr, // Used in the stats code
Expand All @@ -35,9 +34,6 @@ extern crate quickcheck;

use regex::Regex;

#[cfg(feature = "real_blackbox")]
extern crate test;

#[macro_use]
extern crate serde_derive;

Expand Down Expand Up @@ -72,6 +68,7 @@ mod stats;
use std::cell::RefCell;
use std::collections::HashSet;
use std::env;
use std::hint;
use std::io::{stdout, IsTerminal};
use std::net::TcpStream;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -147,24 +144,10 @@ fn debug_enabled() -> bool {
/// A function that is opaque to the optimizer, used to prevent the compiler from
/// optimizing away computations in a benchmark.
///
/// This variant is backed by the (unstable) test::black_box function.
#[cfg(feature = "real_blackbox")]
pub fn black_box<T>(dummy: T) -> T {
test::black_box(dummy)
}

/// A function that is opaque to the optimizer, used to prevent the compiler from
/// optimizing away computations in a benchmark.
///
/// This variant is stable-compatible, but it may cause some performance overhead
/// or fail to prevent code from being eliminated.
#[cfg(not(feature = "real_blackbox"))]
/// This function is deprecated in favour of `std::hint::black_box`.
#[deprecated(note = "use `std::hint::black_box` instead")]
pub fn black_box<T>(dummy: T) -> T {
unsafe {
let ret = std::ptr::read_volatile(&dummy);
std::mem::forget(dummy);
ret
}
hint::black_box(dummy)
}

/// Argument to [`Bencher::iter_batched`] and [`Bencher::iter_batched_ref`] which controls the
Expand Down