From 49232b15540549d42ca71194b36beffa8cc4f190 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 27 Nov 2023 13:41:59 -0500 Subject: [PATCH 1/4] Improve the speed of comparing memory buffers by using a workaround to a missed compiler optimization Co-authored-by: Eric Jensen --- Sources/SnapshotTesting/Snapshotting/NSImage.swift | 10 ++++++++-- Sources/SnapshotTesting/Snapshotting/UIImage.swift | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sources/SnapshotTesting/Snapshotting/NSImage.swift b/Sources/SnapshotTesting/Snapshotting/NSImage.swift index 0f1d5b481..3e2febfd2 100644 --- a/Sources/SnapshotTesting/Snapshotting/NSImage.swift +++ b/Sources/SnapshotTesting/Snapshotting/NSImage.swift @@ -118,8 +118,14 @@ let newRep = NSBitmapImageRep(cgImage: newerCgImage).bitmapData! let byteCountThreshold = Int((1 - precision) * Float(byteCount)) var differentByteCount = 0 - for offset in 0.. Date: Mon, 27 Nov 2023 10:47:32 -0800 Subject: [PATCH 2/4] Update NSImage.swift --- Sources/SnapshotTesting/Snapshotting/NSImage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SnapshotTesting/Snapshotting/NSImage.swift b/Sources/SnapshotTesting/Snapshotting/NSImage.swift index 3e2febfd2..f09a01e4c 100644 --- a/Sources/SnapshotTesting/Snapshotting/NSImage.swift +++ b/Sources/SnapshotTesting/Snapshotting/NSImage.swift @@ -118,7 +118,7 @@ let newRep = NSBitmapImageRep(cgImage: newerCgImage).bitmapData! let byteCountThreshold = Int((1 - precision) * Float(byteCount)) var differentByteCount = 0 - // NB: We are purposely using a verbose 'while' look instead of a 'for in' loop. When the + // 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) From 33ff4293389ab07e9f3983a41022831b128eb78a Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 27 Nov 2023 10:47:37 -0800 Subject: [PATCH 3/4] Update UIImage.swift --- Sources/SnapshotTesting/Snapshotting/UIImage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SnapshotTesting/Snapshotting/UIImage.swift b/Sources/SnapshotTesting/Snapshotting/UIImage.swift index bff1a0573..e5fc4262e 100644 --- a/Sources/SnapshotTesting/Snapshotting/UIImage.swift +++ b/Sources/SnapshotTesting/Snapshotting/UIImage.swift @@ -140,7 +140,7 @@ } else { let byteCountThreshold = Int((1 - precision) * Float(byteCount)) var differentByteCount = 0 - // NB: We are purposely using a verbose 'while' look instead of a 'for in' loop. When the + // 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) From 8d8ad77aaf2aacf84cda6659c2d4d73fd80fa7e9 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 27 Nov 2023 13:53:27 -0500 Subject: [PATCH 4/4] fix --- Sources/SnapshotTesting/Snapshotting/NSImage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SnapshotTesting/Snapshotting/NSImage.swift b/Sources/SnapshotTesting/Snapshotting/NSImage.swift index f09a01e4c..be4fd7cd4 100644 --- a/Sources/SnapshotTesting/Snapshotting/NSImage.swift +++ b/Sources/SnapshotTesting/Snapshotting/NSImage.swift @@ -125,7 +125,7 @@ var index = 0 while index < byteCount { defer { index += 1 } - if oldRep[index] != newRep[offset] { + if oldRep[index] != newRep[index] { differentByteCount += 1 } }