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

Add a blackHole #69

Open
dabrahams opened this issue Aug 27, 2020 · 1 comment
Open

Add a blackHole #69

dabrahams opened this issue Aug 27, 2020 · 1 comment

Comments

@dabrahams
Copy link
Contributor

dabrahams commented Aug 27, 2020

The library should provide a way to ensure that code under test isn't optimized away. The only reliable way I know of to do this that doesn't cost more than a function call is:

notOptimizedAway<T>(_ x: T) { 
  withUnsafePointer(to: x) { opaqueCFunctionTakingVoidConstStar($0) }
}

(storing values into Any can be costly if they need to be boxed, and anyway if the compiler can prove the Any is never read, it may one day optimize it out).

@dabrahams
Copy link
Contributor Author

dabrahams commented Aug 27, 2020

Maybe this is better, though it uses a compiler internal:

/// Prevents the value of `x` from being discarded by the optimizer.
///
/// See https://github.com/google/swift-benchmark/issues/69
@inline(__always)
fileprivate func doNotOptimizeAway<T>(_ x: T) {
  
  @_optimize(none) 
  func assumePointeeIsRead(_ x: UnsafeRawPointer) {}
  
  withUnsafePointer(to: x) { assumePointeeIsRead($0) }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant