swift-benchmark icon indicating copy to clipboard operation
swift-benchmark copied to clipboard

Add facility for hiding values from the compiler

Open dabrahams opened this issue 4 years ago • 0 comments

/// Returns `x`, preventing its value from being statically known by the optimizer.
@inline(__always)
fileprivate func hideValue<T>(_ x: T) -> T {
  
  @_optimize(none) 
  func assumePointeeIsWritten<T>(_ x: UnsafeMutablePointer<T>) {}
  
  var copy = x
  withUnsafeMutablePointer(to: &copy) { assumePointeeIsWritten($0) }
  return copy
}

dabrahams avatar Aug 28 '20 18:08 dabrahams