Skip to content

[stdlib][DNM] Replace several subscript gets with reads #19858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stdlib/public/core/DropWhile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ extension LazyDropWhileCollection: Collection {

@inlinable // lazy-performance
public subscript(position: Index) -> Element {
return _base[position.base]
_read {
yield _base[position.base]
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions stdlib/public/core/ExistentialCollection.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>

@inlinable
internal override subscript(position: _AnyIndexBox) -> Element {
return _base[_unbox(position)]
_read {
yield _base[_unbox(position)]
}
}

@inlinable
Expand Down Expand Up @@ -1022,7 +1024,9 @@ extension ${Self}: ${SelfProtocol} {
/// `position != endIndex`.
@inlinable
public subscript(position: Index) -> Element {
return _box[position._box]
_read {
yield _box[position._box]
}
}

@inlinable
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ extension LazyFilterCollection : LazyCollectionProtocol {
/// `position != endIndex`.
@inlinable // lazy-performance
public subscript(position: Index) -> Element {
return _base[position]
_read {
yield _base[position]
}
}

@inlinable // lazy-performance
Expand Down
7 changes: 5 additions & 2 deletions stdlib/public/core/Flatten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ extension FlattenCollection.Index : Hashable
}

extension FlattenCollection : Sequence {
public typealias Element = Base.Element.Element
public typealias Iterator = FlattenSequence<Base>.Iterator
public typealias SubSequence = Slice<FlattenCollection>

Expand Down Expand Up @@ -461,8 +462,10 @@ extension FlattenCollection : Collection {
/// - Precondition: `position` is a valid position in `self` and
/// `position != endIndex`.
@inlinable // lazy-performance
public subscript(position: Index) -> Base.Element.Element {
return _base[position._outer][position._inner!]
public subscript(position: Index) -> Element {
_read {
yield _base[position._outer][position._inner!]
}
}

@inlinable // lazy-performance
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/Indices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ extension DefaultIndices: Collection {
@inlinable
public subscript(i: Index) -> Elements.Index {
// FIXME: swift-3-indexing-model: range check.
return i
_read {
yield i
}
}

@inlinable
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/KeyValuePairs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ extension KeyValuePairs : RandomAccessCollection {
/// - Returns: The key-value pair at position `position`.
@inlinable // trivial-implementation
public subscript(position: Index) -> Element {
return _elements[position]
_read {
yield _elements[position]
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/LazyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ extension LazyCollection : Collection {
/// `position != endIndex`.
@inlinable
public subscript(position: Index) -> Element {
return _base[position]
_read {
yield _base[position]
}
}

/// A Boolean value indicating whether the collection is empty.
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/Repeat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ extension Repeated: RandomAccessCollection {
/// `endIndex` property.
@inlinable // trivial-implementation
public subscript(position: Int) -> Element {
_precondition(position >= 0 && position < count, "Index out of range")
return repeatedValue
_read {
_precondition(position >= 0 && position < count, "Index out of range")
yield repeatedValue
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/Reverse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ extension ReversedCollection: BidirectionalCollection {

@inlinable
public subscript(position: Index) -> Element {
return _base[_base.index(before: position.base)]
_read {
yield _base[_base.index(before: position.base)]
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
/// range `0..<count`.
@inlinable // unsafe-performance
public subscript(i: Int) -> Element {
get {
_read {
_debugPrecondition(i >= 0)
_debugPrecondition(i < endIndex)
return _position![i]
yield _position![i]
}
%if Mutable:
nonmutating _modify {
Expand Down