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

Improve the speed of comparing memory buffers by using a workaround t… #812

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' look instead of a 'for in' loop. When the
stephencelis marked this conversation as resolved.
Show resolved Hide resolved
// 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[offset] {
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' look instead of a 'for in' loop. When the
stephencelis marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading