mz_ore/
hint.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Portions of this file are derived from the criterion project.
17// The original source code was retrieved on February 19, 2020 from:
18//
19//     https://github.com/bheisler/criterion.rs/blob/76061c756347c0575bcfd044a9027dcf66f85a3e/src/lib.rs
20//
21// The original source code is dual-licensed under the Apache 2.0 and MIT
22// licenses, copies of which can be found in the LICENSE file at the root of
23// this repository.
24
25//! Extensions to `std::hint`.
26
27/// A function that is opaque to the optimizer, used to prevent the compiler
28/// from optimizing away computations in a benchmark.
29///
30/// This variant is stable-compatible, but it may cause some performance
31/// overhead or fail to prevent code from being eliminated.
32///
33/// When `std::hint::black_box` is stabilized, this function can be removed.
34pub fn black_box<T>(dummy: T) -> T {
35    unsafe {
36        let ret = std::ptr::read_volatile(&dummy);
37        std::mem::forget(dummy);
38        ret
39    }
40}