Skip to content

Commit

Permalink
Improve the speed of comparing memory buffers by using a workaround t… (
Browse files Browse the repository at this point in the history
pointfreeco#812)

* Improve the speed of comparing memory buffers by using a workaround to a missed compiler optimization

Co-authored-by: Eric Jensen <[email protected]>

* Update NSImage.swift

* Update UIImage.swift

* fix

---------

Co-authored-by: Eric Jensen <[email protected]>
Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2023
1 parent 4862d48 commit 59b663f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/NSImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@
let newRep = NSBitmapImageRep(cgImage: newerCgImage).bitmapData!
let byteCountThreshold = Int((1 - precision) * Float(byteCount))
var differentByteCount = 0
for offset in 0..<byteCount {
if oldRep[offset] != newRep[offset] {
// NB: We are purposely using a verbose 'while' loop instead of a 'for in' loop. 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)
var index = 0
while index < byteCount {
defer { index += 1 }
if oldRep[index] != newRep[index] {
differentByteCount += 1
}
}
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 @@ -140,8 +140,14 @@
} else {
let byteCountThreshold = Int((1 - precision) * Float(byteCount))
var differentByteCount = 0
for offset in 0..<byteCount {
if oldBytes[offset] != newerBytes[offset] {
// NB: We are purposely using a verbose 'while' loop instead of a 'for in' loop. 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)
var index = 0
while index < byteCount {
defer { index += 1 }
if oldBytes[index] != newerBytes[index] {
differentByteCount += 1
}
}
Expand Down

0 comments on commit 59b663f

Please sign in to comment.