Skip to content

Commit cd59716

Browse files
committed
[benchmark] PopFrontGeneric - Replace protocol with minimal implementation
_ArrayBufferProtocol's visiblity will be changed to internal. Replace with a minimal implementation for this benchmark. rdar://27261449
1 parent 9c08fbf commit cd59716

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

benchmark/single-source/PopFrontGeneric.swift

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,32 @@ let arrayCount = 1024
1717

1818
// This test case exposes rdar://17440222 which caused rdar://17974483 (popFront
1919
// being really slow).
20+
protocol MyArrayBufferProtocol : MutableCollection, RandomAccessCollection {
21+
associatedtype Element
2022

21-
func _arrayReplace<B: _ArrayBufferProtocol, C: Collection
23+
mutating func myReplace<C>(
24+
_ subRange: Range<Int>,
25+
with newValues: C
26+
) where C : Collection, C.Iterator.Element == Element
27+
}
28+
29+
extension Array : MyArrayBufferProtocol {
30+
mutating func myReplace<C>(
31+
_ subRange: Range<Int>,
32+
with newValues: C
33+
) where C : Collection, C.Iterator.Element == Element {
34+
replaceSubrange(subRange, with: newValues)
35+
}
36+
}
37+
38+
func myArrayReplace<B: MyArrayBufferProtocol, C: Collection
2239
where C.Iterator.Element == B.Element, B.Index == Int
2340
>(
2441
_ target: inout B, _ subRange: Range<Int>, _ newValues: C
2542
) {
26-
_precondition(
27-
subRange.lowerBound >= 0,
28-
"Array replace: subRange start is negative")
29-
30-
_precondition(
31-
subRange.upperBound <= target.endIndex,
32-
"Array replace: subRange extends past the end")
33-
34-
let oldCount = target.count
35-
let eraseCount = subRange.count
36-
let insertCount = numericCast(newValues.count) as Int
37-
let growth = insertCount - eraseCount
38-
39-
if target.requestUniqueMutableBackingBuffer(minimumCapacity: oldCount + growth) != nil {
40-
target.replace(subRange: subRange, with: insertCount, elementsOf: newValues)
41-
}
42-
else {
43-
_preconditionFailure("Should not get here?")
44-
}
43+
target.myReplace(subRange, with: newValues)
4544
}
4645

47-
4846
@inline(never)
4947
public func run_PopFrontArrayGeneric(_ N: Int) {
5048
let orig = Array(repeating: 1, count: arrayCount)
@@ -55,7 +53,7 @@ public func run_PopFrontArrayGeneric(_ N: Int) {
5553
a.append(contentsOf: orig)
5654
while a.count != 0 {
5755
result += a[0]
58-
_arrayReplace(&a._buffer, 0..<1, EmptyCollection())
56+
myArrayReplace(&a, 0..<1, EmptyCollection())
5957
}
6058
CheckResults(result == arrayCount, "IncorrectResults in StringInterpolation: \(result) != \(arrayCount)")
6159
}

0 commit comments

Comments
 (0)