File tree 1 file changed +15
-0
lines changed
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -240,6 +240,13 @@ extension Sequence {
240
240
@inlinable
241
241
public func partitioned(
242
242
_ 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
243
250
) rethrows -> ( [ Element ] , [ Element ] ) {
244
251
var lhs = ContiguousArray < Element > ( )
245
252
var rhs = ContiguousArray < Element > ( )
@@ -276,6 +283,14 @@ extension Collection {
276
283
277
284
let count = self . count
278
285
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
+
279
294
// Inside of the `initializer` closure, we set what the actual mid-point is.
280
295
// We will use this to partitioned the single array into two in constant time.
281
296
var midPoint : Int = 0
You can’t perform that action at this time.
0 commit comments