Skip to content

Commit cd6161d

Browse files
committed
Try to reduce the up-front overhead of diffing on ==
1 parent e20c954 commit cd6161d

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

Sources/Testing/Expectations/ExpectationContext.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class __ExpectationContext<Output> where Output: ~Copyable {
5050
/// are evaluated, much like ``runtimeValues``.
5151
///
5252
/// This value is optional because, in the common case, an expectation does
53-
/// not produce a difference.
53+
/// not produce a difference value.
5454
var differences: [(__ExpressionID, () -> CollectionDifference<Any>?)]?
5555

5656
init(
@@ -272,13 +272,16 @@ extension __ExpectationContext where Output: ~Copyable {
272272
/// compile-time pressure on the type checker that we don't want.
273273
func captureDifferences<T, U>(_ lhs: T, _ rhs: U, _ opID: __ExpressionID) {
274274
#if !hasFeature(Embedded) // no existentials
275-
var difference: (() -> CollectionDifference<Any>?)?
276-
if let lhs = lhs as? any StringProtocol {
277-
func open<V>(_ lhs: V, _ rhs: U) where V: StringProtocol {
278-
guard let rhs = rhs as? V else {
279-
return
280-
}
281-
difference = {
275+
guard let lhs = lhs as? any BidirectionalCollection else {
276+
return
277+
}
278+
279+
func difference() -> CollectionDifference<Any>? {
280+
if let lhs = lhs as? any StringProtocol {
281+
func open<V>(_ lhs: V, _ rhs: U) -> CollectionDifference<Any>? where V: StringProtocol {
282+
guard let rhs = rhs as? V else {
283+
return nil
284+
}
282285
// Compare strings by line, not by character.
283286
let lhsLines = String(lhs).split(whereSeparator: \.isNewline)
284287
let rhsLines = String(rhs).split(whereSeparator: \.isNewline)
@@ -298,36 +301,33 @@ extension __ExpectationContext where Output: ~Copyable {
298301

299302
return CollectionDifference<Any>(diff)
300303
}
301-
}
302-
open(lhs, rhs)
303-
} else if lhs is any RangeExpression {
304-
// Do _not_ perform a diffing operation on `lhs` and `rhs`. Range
305-
// expressions are not usefully diffable the way other kinds of
306-
// collections are. SEE: https://github.com/swiftlang/swift-testing/issues/639
307-
} else if let lhs = lhs as? any BidirectionalCollection {
308-
func open<V>(_ lhs: V, _ rhs: U) where V: BidirectionalCollection {
309-
guard let rhs = rhs as? V,
310-
let elementType = V.Element.self as? any Equatable.Type else {
311-
return
312-
}
313-
difference = {
304+
return open(lhs, rhs)
305+
} else if lhs is any RangeExpression {
306+
// Do _not_ perform a diffing operation on `lhs` and `rhs`. Range
307+
// expressions are not usefully diffable the way other kinds of
308+
// collections are. SEE: https://github.com/swiftlang/swift-testing/issues/639
309+
return nil
310+
} else {
311+
func open<V>(_ lhs: V, _ rhs: U) -> CollectionDifference<Any>? where V: BidirectionalCollection {
312+
guard let rhs = rhs as? V,
313+
let elementType = V.Element.self as? any Equatable.Type else {
314+
return nil
315+
}
314316
func open<E>(_: E.Type) -> CollectionDifference<Any> where E: Equatable {
315317
let lhs: some BidirectionalCollection<E> = lhs.lazy.map { $0 as! E }
316318
let rhs: some BidirectionalCollection<E> = rhs.lazy.map { $0 as! E }
317319
return CollectionDifference<Any>(lhs.difference(from: rhs))
318320
}
319321
return open(elementType)
320322
}
323+
return open(lhs, rhs)
321324
}
322-
open(lhs, rhs)
323325
}
324326

325-
if let difference {
326-
if differences == nil {
327-
differences = [(opID, difference)]
328-
} else {
329-
differences?.append((opID, difference))
330-
}
327+
if differences == nil {
328+
differences = [(opID, difference)]
329+
} else {
330+
differences?.append((opID, difference))
331331
}
332332
#endif
333333
}

0 commit comments

Comments
 (0)