Skip to content

Commit e9a2e1e

Browse files
committed
Eliminate all of the uses of ++/-- from stdlib/public/core.
At DaveA's suggestion, I took a mostly mechanical approach to this: pointers and numeric types start using += 1, and indexes use i = i.successor(). The index model is likely to be revised in Swift 3 anyway, so micro-optimizing this code syntactically isn't super important. There is some performance concern of this patch, since some in-place succesor operations are more efficient than i = i.successor(). The one that seems particularly at issue is the instance in the implementation of partition(), which I changed to use i._successorInPlace(). If other instances lead to a perf issue, they can be changed to use that as well.
1 parent 46aef83 commit e9a2e1e

15 files changed

+73
-36
lines changed

stdlib/public/core/ArrayBufferType.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ extension _ArrayBufferType {
159159
// Assign over the original subRange
160160
var i = newValues.startIndex
161161
for j in subRange {
162-
elements[j] = newValues[i++]
162+
elements[j] = newValues[i]
163+
i = i.successor()
163164
}
164165
// Initialize the hole left by sliding the tail forward
165166
for j in oldTailIndex..<newTailIndex {
166-
(elements + j).initialize(newValues[i++])
167+
(elements + j).initialize(newValues[i])
168+
i = i.successor()
167169
}
168170
_expectEnd(i, newValues)
169171
}
@@ -172,7 +174,9 @@ extension _ArrayBufferType {
172174
var i = subRange.startIndex
173175
var j = newValues.startIndex
174176
for _ in 0..<newCount {
175-
elements[i++] = newValues[j++]
177+
elements[i] = newValues[j]
178+
i = i.successor()
179+
j = j.successor()
176180
}
177181
_expectEnd(j, newValues)
178182

stdlib/public/core/ArrayCast.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ public func _arrayForceCast<SourceElement, TargetElement>(
8282
let bridged: AnyObject? = _bridgeToObjectiveC(value)
8383
_precondition(
8484
bridged != nil, "array element cannot be bridged to Objective-C")
85-
// FIXME: should be an unsafeDowncast, but for <rdar://problem/18638230>
86-
p++.initialize(unsafeBitCast(bridged!, TargetElement.self))
85+
// FIXME: should be an unsafeDowncast.
86+
p.initialize(unsafeBitCast(bridged!, TargetElement.self))
87+
p += 1
8788
}
8889
}
8990
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
@@ -162,7 +163,8 @@ ElementwiseBridging:
162163
if _slowPath(value == nil) {
163164
break ElementwiseBridging
164165
}
165-
p++.initialize(value!)
166+
p.initialize(value!)
167+
p += 1
166168
}
167169
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
168170
}

stdlib/public/core/Arrays.swift.gyb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ extension ${Self} : _ArrayType {
513513
var p: UnsafeMutablePointer<Element>
514514
(self, p) = ${Self}._allocateUninitialized(count)
515515
for _ in 0..<count {
516-
p++.initialize(repeatedValue)
516+
p.initialize(repeatedValue)
517+
p += 1
517518
}
518519
}
519520

@@ -908,7 +909,9 @@ internal struct _InitializeMemoryFromCollection<
908909
var p = rawMemory
909910
var q = newValues.startIndex
910911
for _ in 0..<count {
911-
p++.initialize(newValues[q++])
912+
p.initialize(newValues[q])
913+
q = q.successor()
914+
p += 1
912915
}
913916
_expectEnd(q, newValues)
914917
}

stdlib/public/core/Collection.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ extension CollectionType {
322322
var i = self.startIndex
323323

324324
for _ in 0..<count {
325-
result.append(try transform(self[i++]))
325+
result.append(try transform(self[i]))
326+
i = i.successor()
326327
}
327328

328329
_expectEnd(i, self)
@@ -595,7 +596,8 @@ extension SequenceType
595596
} else {
596597
var p = ptr
597598
for x in self {
598-
p++.initialize(x)
599+
p.initialize(x)
600+
p += 1
599601
}
600602
return p
601603
}
@@ -726,8 +728,8 @@ internal func _writeBackMutableSlice<
726728
newElementIndex != newElementsEndIndex {
727729

728730
self_[selfElementIndex] = slice[newElementIndex]
729-
++selfElementIndex
730-
++newElementIndex
731+
selfElementIndex = selfElementIndex.successor()
732+
newElementIndex = newElementIndex.successor()
731733
}
732734

733735
_precondition(

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ internal func _copyCollectionToNativeArrayBuffer<
608608
var i = source.startIndex
609609
for _ in 0..<count {
610610
// FIXME(performance): use _initializeTo().
611-
(p++).initialize(source[i++])
611+
p.initialize(source[i])
612+
i = i.successor()
613+
p = p.successor()
612614
}
613615
_expectEnd(i, source)
614616
return result
@@ -665,7 +667,8 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
665667
"_UnsafePartiallyInitializedContiguousArrayBuffer has no more capacity")
666668
remainingCapacity -= 1
667669

668-
(p++).initialize(element)
670+
p.initialize(element)
671+
p += 1
669672
}
670673

671674
/// Finish initializing the buffer, adjusting its count to the final

stdlib/public/core/Existential.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal struct _CollectionOf<
4444
return AnyGenerator {
4545
() -> T? in
4646
if _fastPath(index != self.endIndex) {
47-
++index
47+
index = index.successor()
4848
return self._subscriptImpl(index)
4949
}
5050
return nil

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ public func == <Element : Hashable>(lhs: Set<Element>, rhs: Set<Element>) -> Boo
713713
}
714714

715715
let endIndex = lhsNative.endIndex
716-
for var i = lhsNative.startIndex; i != endIndex; ++i {
716+
for var i = lhsNative.startIndex; i != endIndex; i = i.successor() {
717717
let key = lhsNative.assertingGet(i)
718718
let bridgedKey: AnyObject = _bridgeToObjectiveCUnconditional(key)
719719
let optRhsValue: AnyObject? = rhsCocoa.maybeGet(bridgedKey)
@@ -1230,7 +1230,8 @@ public func == <Key : Equatable, Value : Equatable>(
12301230
}
12311231

12321232
let endIndex = lhsNative.endIndex
1233-
for var index = lhsNative.startIndex; index != endIndex; ++index {
1233+
for var index = lhsNative.startIndex; index != endIndex;
1234+
index = index.successor() {
12341235
let (key, value) = lhsNative.assertingGet(index)
12351236
let optRhsValue: AnyObject? =
12361237
rhsCocoa.maybeGet(_bridgeToObjectiveCUnconditional(key))

stdlib/public/core/Index.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ extension ForwardIndexType {
214214
_precondition(n >= 0,
215215
"Only BidirectionalIndexType can be advanced by a negative amount")
216216
var p = self
217-
for var i: Distance = 0; i != n; ++i {
218-
++p
217+
var i : Distance = 0
218+
while i != n {
219+
p = p.successor()
220+
i = i + 1
219221
}
220222
return p
221223
}
@@ -227,8 +229,11 @@ extension ForwardIndexType {
227229
_precondition(n >= 0,
228230
"Only BidirectionalIndexType can be advanced by a negative amount")
229231
var p = self
230-
for var i: Distance = 0; i != n && p != limit; ++i {
231-
++p
232+
var i : Distance = 0
233+
while i != n {
234+
if p == limit { break }
235+
p = p.successor()
236+
i = i + 1
232237
}
233238
return p
234239
}
@@ -248,8 +253,8 @@ extension ForwardIndexType {
248253
var p = self
249254
var count: Distance = 0
250255
while p != end {
251-
++count
252-
++p
256+
count += 1
257+
p = p.successor()
253258
}
254259
return count
255260
}

stdlib/public/core/Sequence.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ extension SequenceType {
613613
-> UnsafeMutablePointer<Generator.Element> {
614614
var p = UnsafeMutablePointer<Generator.Element>(ptr)
615615
for x in GeneratorSequence(self.generate()) {
616-
p++.initialize(x)
616+
p.initialize(x)
617+
p += 1
617618
}
618619
return p
619620
}

stdlib/public/core/Sort.swift.gyb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func _insertionSort<
5656

5757
// One element is trivially already-sorted, thus pre-increment
5858
// Continue until the sorted elements cover the whole sequence
59-
while (++sortedEnd != range.endIndex) {
59+
sortedEnd = sortedEnd.successor()
60+
while sortedEnd != range.endIndex {
6061
// get the first unsorted element
6162
let x: C.Generator.Element = elements[sortedEnd]
6263

@@ -80,6 +81,7 @@ func _insertionSort<
8081
// Plop x into position
8182
elements[i] = x
8283
}
84+
sortedEnd = sortedEnd.successor()
8385
}
8486
}
8587
}
@@ -130,8 +132,10 @@ func _partition<
130132

131133
Loop: while true {
132134
FindLo: repeat {
133-
while ++lo != hi {
135+
lo._successorInPlace()
136+
while lo != hi {
134137
if !${cmp("elements[lo]", "pivot", p)} { break FindLo }
138+
lo._successorInPlace()
135139
}
136140
break Loop
137141
} while false

stdlib/public/core/StringBuffer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public struct _StringBuffer {
106106
if isAscii {
107107
var p = UnsafeMutablePointer<UTF8.CodeUnit>(result.start)
108108
let sink: (UTF32.CodeUnit) -> Void = {
109-
(p++).memory = UTF8.CodeUnit($0)
109+
p.memory = UTF8.CodeUnit($0)
110+
p += 1
110111
}
111112
let hadError = transcode(
112113
encoding, UTF32.self, input.generate(), sink,
@@ -117,7 +118,8 @@ public struct _StringBuffer {
117118
else {
118119
var p = result._storage.baseAddress
119120
let sink: (UTF16.CodeUnit) -> Void = {
120-
(p++).memory = $0
121+
p.memory = $0
122+
p += 1
121123
}
122124
let hadError = transcode(
123125
encoding, UTF16.self, input.generate(), sink,

stdlib/public/core/StringCharacterView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ extension String.CharacterView : CollectionType {
143143

144144
var gcb0 = graphemeClusterBreakProperty.getPropertyRawValue(
145145
unicodeScalars[start].value)
146-
++start
146+
start = start.successor()
147147

148-
for ; start != end; ++start {
148+
for ; start != end; start = start.successor() {
149149
// FIXME(performance): consider removing this "fast path". A branch
150150
// that is hard to predict could be worse for performance than a few
151151
// loads from cache to fetch the property 'gcb1'.

stdlib/public/core/StringCore.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public struct _StringCore {
121121
var src = UnsafeMutablePointer<UTF8.CodeUnit>(srcStart)
122122
let srcEnd = src + count
123123
while (src != srcEnd) {
124-
dest++.memory = UTF16.CodeUnit(src++.memory)
124+
dest.memory = UTF16.CodeUnit(src.memory)
125+
dest += 1
126+
src += 1
125127
}
126128
}
127129
else {
@@ -130,7 +132,9 @@ public struct _StringCore {
130132
var src = UnsafeMutablePointer<UTF16.CodeUnit>(srcStart)
131133
let srcEnd = src + count
132134
while (src != srcEnd) {
133-
dest++.memory = UTF8.CodeUnit(src++.memory)
135+
dest.memory = UTF8.CodeUnit(src.memory)
136+
dest += 1
137+
src += 1
134138
}
135139
}
136140
}
@@ -618,13 +622,15 @@ extension _StringCore : RangeReplaceableCollectionType {
618622
if _fastPath(elementWidth == 1) {
619623
var dst = rangeStart
620624
for u in newElements {
621-
dst++.memory = UInt8(u & 0xFF)
625+
dst.memory = UInt8(u & 0xFF)
626+
dst += 1
622627
}
623628
}
624629
else {
625630
var dst = UnsafeMutablePointer<UTF16.CodeUnit>(rangeStart)
626631
for u in newElements {
627-
dst++.memory = u
632+
dst.memory = u
633+
dst += 1
628634
}
629635
}
630636

stdlib/public/core/StringLegacy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extension String {
158158
let rng = unicodeScalars
159159
var startIndex = rng.startIndex
160160
for _ in 0..<start {
161-
++startIndex
161+
startIndex = startIndex.successor()
162162
}
163163
return String(rng[startIndex..<rng.endIndex])
164164
}

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public struct UnsafeBufferPointerGenerator<Element>
2121
/// Advance to the next element and return it, or `nil` if no next
2222
/// element exists.
2323
public mutating func next() -> Element? {
24-
return position == end ? nil : (position++).memory
24+
if position == end { return nil }
25+
26+
let result = position.memory
27+
position += 1
28+
return result
2529
}
2630

2731
var position, end: UnsafePointer<Element>

0 commit comments

Comments
 (0)