Skip to content

Commit 6227932

Browse files
author
Dave Abrahams
authored
[stdlib] Custom Reverse[RandomAccess]Collection.Iterator
In local tests this has accounted for some major speedups by helping the optimizer eliminate ARC traffic
1 parent f6271a3 commit 6227932

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

stdlib/public/core/Reverse.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,35 @@ public struct ReversedCollection<
156156

157157
public typealias IndexDistance = Base.IndexDistance
158158

159-
/// A type that provides the sequence's iteration interface and
160-
/// encapsulates its iteration state.
161-
public typealias Iterator = IndexingIterator<ReversedCollection>
159+
@_fixed_layout
160+
public struct Iterator : IteratorProtocol, Sequence {
161+
@_inlineable
162+
@inline(__always)
163+
public /// @testable
164+
init(elements: Base, endPosition: Base.Index) {
165+
self._elements = elements
166+
self._position = endPosition
167+
}
168+
169+
@_inlineable
170+
@inline(__always)
171+
public mutating func next() -> Base.Iterator.Element? {
172+
guard _fastPath(_position != _elements.startIndex) else { return nil }
173+
_position = _elements.index(before: _position)
174+
return _elements[_position]
175+
}
176+
177+
@_versioned
178+
internal let _elements: Base
179+
@_versioned
180+
internal var _position: Base.Index
181+
}
182+
183+
@_inlineable
184+
@inline(__always)
185+
public func makeIterator() -> Iterator {
186+
return Iterator(elements: _base, endPosition: _base.endIndex)
187+
}
162188

163189
@_inlineable
164190
public var startIndex: Index {

0 commit comments

Comments
 (0)