Skip to content

Commit ebfb504

Browse files
committed
Don't use generators in array equality comparisons
This is easier to read and faster if we don't specialize away the generator code.
1 parent ae60159 commit ebfb504

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,8 @@ public func == <Element : Equatable>(
12701270
return true
12711271
}
12721272

1273+
%if Self == 'ArraySlice':
1274+
12731275
var streamLHS = lhs.generate()
12741276
var streamRHS = rhs.generate()
12751277

@@ -1282,6 +1284,19 @@ public func == <Element : Equatable>(
12821284
nextLHS = streamLHS.next()
12831285
}
12841286

1287+
%else:
1288+
1289+
_sanityCheck(lhs.startIndex == 0 && rhs.startIndex == 0)
1290+
_sanityCheck(lhs.endIndex == lhsCount && rhs.endIndex == lhsCount)
1291+
1292+
// We know that lhs.count == rhs.count, compare element wise.
1293+
for idx in 0..<lhsCount {
1294+
if lhs[idx] != rhs[idx] {
1295+
return false
1296+
}
1297+
}
1298+
%end
1299+
12851300
return true
12861301
}
12871302

0 commit comments

Comments
 (0)