Skip to content

Commit

Permalink
add precondition to exit the loops
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaPuchkaTW committed Nov 25, 2022
1 parent ad051b1 commit 8f87304
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions Sources/SnapshotTesting/Snapshotting/NSImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ private func compare(_ old: NSImage, _ new: NSImage, precision: Float, perceptua
if oldRep[offset] != newRep[offset] {
differentByteCount += 1
}
return differentByteCount <= byteCountThreshold
}
if differentByteCount > byteCountThreshold {
let actualPrecision = 1 - Float(differentByteCount) / Float(byteCount)
Expand Down
10 changes: 8 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private func compare(_ old: UIImage, _ new: UIImage, precision: Float, perceptua
if oldBytes[offset] != newerBytes[offset] {
differentByteCount += 1
}
return differentByteCount <= byteCountThreshold
}
if differentByteCount > byteCountThreshold {
let actualPrecision = 1 - Float(differentByteCount) / Float(byteCount)
Expand Down Expand Up @@ -186,6 +187,7 @@ func perceptuallyCompare(_ old: CIImage, _ new: CIImage, pixelPrecision: Float,
let deltaThreshold = (1 - perceptualPrecision) * 100
var failingPixelCount: Int = 0
var maximumDeltaE: Float = 0
let maxFailingPixelCountAllowed = Float(1 - pixelPrecision) * Float(deltaOutputImage.extent.width * deltaOutputImage.extent.height)
// rowBytes must be a multiple of 8, so vImage_Buffer pads the end of each row with bytes to meet the multiple of 0 requirement.
// We must do 2D iteration of the vImage_Buffer in order to avoid loading the padding garbage bytes at the end of each row.
fastForEach(in: 0..<Int(buffer.height)) { line in
Expand All @@ -197,7 +199,9 @@ func perceptuallyCompare(_ old: CIImage, _ new: CIImage, pixelPrecision: Float,
failingPixelCount += 1
}
maximumDeltaE = max(maximumDeltaE, deltaE)
return Float(failingPixelCount) <= maxFailingPixelCountAllowed
}
return Float(failingPixelCount) <= maxFailingPixelCountAllowed
}
let failingPixelPercent = Float(failingPixelCount) / Float(deltaOutputImage.extent.width * deltaOutputImage.extent.height)
let actualPixelPrecision = 1 - failingPixelPercent
Expand Down Expand Up @@ -236,10 +240,12 @@ extension CIImage {

/// When the compiler doesn't have optimizations enabled, like in test targets, a `while` loop is significantly faster than a `for` loop
/// for iterating through the elements of a memory buffer. Details can be found in [SR-6983](https://github.com/apple/swift/issues/49531#issuecomment-1108286654)
func fastForEach(in range: Range<Int>, _ body: (Int) -> Void) {
func fastForEach(in range: Range<Int>, _ body: (Int) -> Bool) {
var index = range.lowerBound
while index < range.upperBound {
body(index)
guard body(index) else {
return
}
index += 1
}
}

0 comments on commit 8f87304

Please sign in to comment.