Skip to content

Commit 3cecd50

Browse files
committed
For collections with fewer than 8 elements, use the Sequence-based implementation
This constant was determined using benchmarking. More information: apple#152 (comment)
1 parent d3617cf commit 3cecd50

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/Algorithms/Partition.swift

+15
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ extension Sequence {
240240
@inlinable
241241
public func partitioned(
242242
_ belongsInSecondCollection: (Element) throws -> Bool
243+
) rethrows -> ([Element], [Element]) {
244+
return try _partitioned(belongsInSecondCollection)
245+
}
246+
247+
@inlinable
248+
public func _partitioned(
249+
_ belongsInSecondCollection: (Element) throws -> Bool
243250
) rethrows -> ([Element], [Element]) {
244251
var lhs = ContiguousArray<Element>()
245252
var rhs = ContiguousArray<Element>()
@@ -276,6 +283,14 @@ extension Collection {
276283

277284
let count = self.count
278285

286+
// The overhead of this implementation isn’t worth it for collections fewer
287+
// than 8 elements. Use the simple `Sequence`-based implementation instead.
288+
// This constant was determined using benchmarking. More information:
289+
// https://github.com/apple/swift-algorithms/pull/152#issuecomment-887130149
290+
if count < 8 {
291+
return try _partitioned(belongsInSecondCollection)
292+
}
293+
279294
// Inside of the `initializer` closure, we set what the actual mid-point is.
280295
// We will use this to partitioned the single array into two in constant time.
281296
var midPoint: Int = 0

0 commit comments

Comments
 (0)