Skip to content

Commit 6805ecc

Browse files
authored
Merge pull request swiftlang#6311 from natecook1000/nc-fixes-06
2 parents 28258f7 + 4169635 commit 6805ecc

11 files changed

+162
-126
lines changed

stdlib/public/core/Arrays.swift.gyb

+54-12
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ if True:
248248
/// print(emptyDoubles[0])
249249
/// // Triggers runtime error: Index out of range
250250
///
251-
/// See the `Sequence`, `Collection`, and `RangeReplaceableCollection`
252-
/// protocols for more methods available to arrays.
253-
///
254251
/// Adding and Removing Elements
255252
/// ============================
256253
///
@@ -553,9 +550,8 @@ public struct ${Self}<Element>
553550
/// print(numbers[i])
554551
/// // Prints "50"
555552
///
556-
/// Advancing an index beyond a collection's ending index or offsetting it
557-
/// before a collection's starting index may trigger a runtime error. The
558-
/// value passed as `n` must not result in such an operation.
553+
/// The value passed as `n` must not offset `i` beyond the bounds of the
554+
/// collection.
559555
///
560556
/// - Parameters:
561557
/// - i: A valid index of the array.
@@ -564,8 +560,6 @@ public struct ${Self}<Element>
564560
/// this is the same value as the result of `n` calls to `index(after:)`.
565561
/// If `n` is negative, this is the same value as the result of `-n` calls
566562
/// to `index(before:)`.
567-
///
568-
/// - Complexity: O(1)
569563
public func index(_ i: Int, offsetBy n: Int) -> Int {
570564
// NOTE: this is a manual specialization of index movement for a Strideable
571565
// index that is required for Array performance. The optimizer is not
@@ -600,9 +594,9 @@ public struct ${Self}<Element>
600594
/// print(j)
601595
/// // Prints "nil"
602596
///
603-
/// Advancing an index beyond a collection's ending index or offsetting it
604-
/// before a collection's starting index may trigger a runtime error. The
605-
/// value passed as `n` must not result in such an operation.
597+
/// The value passed as `n` must not offset `i` beyond the bounds of the
598+
/// collection, unless the index passed as `limit` prevents offsetting
599+
/// beyond those bounds.
606600
///
607601
/// - Parameters:
608602
/// - i: A valid index of the array.
@@ -615,7 +609,6 @@ public struct ${Self}<Element>
615609
/// the method returns `nil`.
616610
///
617611
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
618-
/// - Complexity: O(1)
619612
public func index(
620613
_ i: Int, offsetBy n: Int, limitedBy limit: Int
621614
) -> Int? {
@@ -1210,6 +1203,55 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
12101203
/// greater than the requested capacity. Use the array's `capacity` property
12111204
/// to determine the size of the new storage.
12121205
///
1206+
/// Preserving an Array's Geometric Growth Strategy
1207+
/// ===============================================
1208+
///
1209+
/// If you implement a custom data structure backed by an array that grows
1210+
/// dynamically, naively calling the `reserveCapacity(_:)` method can lead
1211+
/// to worse than expected performance. Arrays need to follow a geometric
1212+
/// allocation pattern for appending elements to achieve amortized
1213+
/// constant-time performance. The `Array` type's `append(_:)` and
1214+
/// `append(contentsOf:)` methods take care of this detail for you, but
1215+
/// `reserveCapacity(_:)` allocates only as much space as you tell it to
1216+
/// (padded to a round value), and no more. This avoids over-allocation, but
1217+
/// can result in insertion not having amortized constant-time performance.
1218+
///
1219+
/// The following code declares `values`, an array of integers, and the
1220+
/// `addTenQuadratic()` function, which adds ten more values to the `values`
1221+
/// array on each call.
1222+
///
1223+
/// var values: [Int] = [0, 1, 2, 3]
1224+
///
1225+
/// // Don't use 'reserveCapacity(_:)' like this
1226+
/// func addTenQuadratic() {
1227+
/// let newCount = values.count + 10
1228+
/// values.reserveCapacity(newCount)
1229+
/// for n in values.count..<newCount {
1230+
/// values.append(n)
1231+
/// }
1232+
/// }
1233+
///
1234+
/// The call to `reserveCapacity(_:)` increases the `values` array's capacity
1235+
/// by exactly 10 elements on each pass through `addTenQuadratic()`, which
1236+
/// is linear growth. Instead of having constant time when averaged over
1237+
/// many calls, the function may decay to performance that is linear in
1238+
/// `values.count`. This is almost certainly not what you want.
1239+
///
1240+
/// In cases like this, the simplest fix is often to simply remove the call
1241+
/// to `reserveCapacity(_:)`, and let the `append(_:)` method grow the array
1242+
/// for you.
1243+
///
1244+
/// func addTen() {
1245+
/// let newCount = values.count + 10
1246+
/// for n in values.count..<newCount {
1247+
/// values.append(n)
1248+
/// }
1249+
/// }
1250+
///
1251+
/// If you need more control over the capacity of your array, implement your
1252+
/// own geometric growth strategy, passing the size you compute to
1253+
/// `reserveCapacity(_:)`.
1254+
///
12131255
/// - Parameter minimumCapacity: The requested number of elements to store.
12141256
///
12151257
/// - Complexity: O(*n*), where *n* is the number of elements in the array.

stdlib/public/core/BidirectionalCollection.swift

+6-8
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public protocol BidirectionalCollection
9292
// types with where clauses.
9393
// associatedtype SubSequence : BidirectionalCollection
9494

95-
/// A type that can represent the indices that are valid for subscripting the
95+
/// A type that represents the indices that are valid for subscripting the
9696
/// collection, in ascending order.
9797
associatedtype Indices : _BidirectionalIndexable, Collection
9898
= DefaultBidirectionalIndices<Self>
@@ -227,10 +227,10 @@ extension BidirectionalCollection where SubSequence == BidirectionalSlice<Self>
227227

228228
extension BidirectionalCollection where SubSequence == Self {
229229
/// Removes and returns the last element of the collection.
230-
///
231-
/// Use `popLast()` to remove the last element of a collection that might be empty.
232230
///
233-
/// The `removeLast()` method must be used only on a nonempty collection.
231+
/// You can use `popLast()` to remove the last element of a collection that
232+
/// might be empty. The `removeLast()` method must be used only on a
233+
/// nonempty collection.
234234
///
235235
/// - Returns: The last element of the collection if the collection has one
236236
/// or more elements; otherwise, `nil`.
@@ -246,10 +246,8 @@ extension BidirectionalCollection where SubSequence == Self {
246246

247247
/// Removes and returns the last element of the collection.
248248
///
249-
/// The collection must not be empty.
250-
///
251-
/// To remove the last element of a collection that might be empty,
252-
/// use the `popLast()` method instead.
249+
/// The collection must not be empty. To remove the last element of a
250+
/// collection that might be empty, use the `popLast()` method instead.
253251
///
254252
/// - Returns: The last element of the collection.
255253
///

stdlib/public/core/Collection.swift

+35-36
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,14 @@ public protocol _IndexableBase {
9090
subscript(position: Index) -> _Element { get }
9191

9292
// WORKAROUND: rdar://25214066
93-
/// A sequence that can represent a contiguous subrange of the collection's
93+
/// A sequence that represents a contiguous subrange of the collection's
9494
/// elements.
9595
associatedtype SubSequence
9696

9797
/// Accesses the subsequence bounded by the given range.
9898
///
9999
/// - Parameter bounds: A range of the collection's indices. The upper and
100-
/// lower bounds of the `bounds` range must be valid indices of the
101-
/// collection.
100+
/// lower bounds of the range must be valid indices of the collection.
102101
///
103102
/// - Complexity: O(1)
104103
subscript(bounds: Range<Index>) -> SubSequence { get }
@@ -148,7 +147,7 @@ public protocol _IndexableBase {
148147

149148
/// Returns the position immediately after the given index.
150149
///
151-
/// The successor of an index must be well-defined. For an index `i` into a
150+
/// The successor of an index must be well defined. For an index `i` into a
152151
/// collection `c`, calling `c.index(after: i)` returns the same index every
153152
/// time.
154153
///
@@ -174,7 +173,7 @@ public protocol _IndexableBase {
174173
@available(*, deprecated, message: "it will be removed in Swift 4.0. Please use 'Collection' instead")
175174
public typealias Indexable = _Indexable
176175
public protocol _Indexable : _IndexableBase {
177-
/// A type used to represent the number of steps between two indices, where
176+
/// A type that represents the number of steps between two indices, where
178177
/// one value is reachable from the other.
179178
///
180179
/// In Swift, *reachability* refers to the ability to produce one value from
@@ -191,8 +190,8 @@ public protocol _Indexable : _IndexableBase {
191190
/// print(s[i])
192191
/// // Prints "t"
193192
///
194-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
195-
/// before the `startIndex` of this collection.
193+
/// The value passed as `n` must not offset `i` beyond the bounds of the
194+
/// collection.
196195
///
197196
/// - Parameters:
198197
/// - i: A valid index of the collection.
@@ -231,9 +230,9 @@ public protocol _Indexable : _IndexableBase {
231230
/// print(j)
232231
/// // Prints "nil"
233232
///
234-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
235-
/// before the `startIndex` of this collection, unless the index passed as
236-
/// `limit` prevents offsetting beyond those bounds.
233+
/// The value passed as `n` must not offset `i` beyond the bounds of the
234+
/// collection, unless the index passed as `limit` prevents offsetting
235+
/// beyond those bounds.
237236
///
238237
/// - Parameters:
239238
/// - i: A valid index of the collection.
@@ -256,8 +255,8 @@ public protocol _Indexable : _IndexableBase {
256255

257256
/// Offsets the given index by the specified distance.
258257
///
259-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
260-
/// before the `startIndex` of this collection.
258+
/// The value passed as `n` must not offset `i` beyond the bounds of the
259+
/// collection.
261260
///
262261
/// - Parameters:
263262
/// - i: A valid index of the collection.
@@ -273,9 +272,9 @@ public protocol _Indexable : _IndexableBase {
273272
/// Offsets the given index by the specified distance, or so that it equals
274273
/// the given limiting index.
275274
///
276-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
277-
/// before the `startIndex` of this collection, unless the index passed as
278-
/// `limit` prevents offsetting beyond those bounds.
275+
/// The value passed as `n` must not offset `i` beyond the bounds of the
276+
/// collection, unless the index passed as `limit` prevents offsetting
277+
/// beyond those bounds.
279278
///
280279
/// - Parameters:
281280
/// - i: A valid index of the collection.
@@ -546,15 +545,15 @@ public struct IndexingIterator<
546545
/// count the number of contained elements, accessing its `count` property is
547546
/// an O(*n*) operation.
548547
public protocol Collection : _Indexable, Sequence {
549-
/// A type that can represent the number of steps between a pair of
548+
/// A type that represents the number of steps between a pair of
550549
/// indices.
551550
associatedtype IndexDistance : SignedInteger = Int
552551

553552
/// A type that provides the collection's iteration interface and
554553
/// encapsulates its iteration state.
555554
///
556555
/// By default, a collection conforms to the `Sequence` protocol by
557-
/// supplying a `IndexingIterator` as its associated `Iterator`
556+
/// supplying `IndexingIterator` as its associated `Iterator`
558557
/// type.
559558
associatedtype Iterator : IteratorProtocol = IndexingIterator<Self>
560559

@@ -632,7 +631,7 @@ public protocol Collection : _Indexable, Sequence {
632631
/// - Complexity: O(1)
633632
subscript(bounds: Range<Index>) -> SubSequence { get }
634633

635-
/// A type that can represent the indices that are valid for subscripting the
634+
/// A type that represents the indices that are valid for subscripting the
636635
/// collection, in ascending order.
637636
associatedtype Indices : _Indexable, Sequence = DefaultIndices<Self>
638637

@@ -649,10 +648,10 @@ public protocol Collection : _Indexable, Sequence {
649648
/// order.
650649
///
651650
/// A collection's `indices` property can hold a strong reference to the
652-
/// collection itself, causing the collection to be non-uniquely referenced.
651+
/// collection itself, causing the collection to be nonuniquely referenced.
653652
/// If you mutate the collection while iterating over its indices, a strong
654-
/// reference can cause an unexpected copy of the collection. To avoid the
655-
/// unexpected copy, use the `index(after:)` method starting with
653+
/// reference can result in an unexpected copy of the collection. To avoid
654+
/// the unexpected copy, use the `index(after:)` method starting with
656655
/// `startIndex` to produce indices instead.
657656
///
658657
/// var c = MyFancyCollection([10, 20, 30, 40, 50])
@@ -802,8 +801,8 @@ public protocol Collection : _Indexable, Sequence {
802801
/// print(s[i])
803802
/// // Prints "t"
804803
///
805-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
806-
/// before the `startIndex` of this collection.
804+
/// The value passed as `n` must not offset `i` beyond the bounds of the
805+
/// collection.
807806
///
808807
/// - Parameters:
809808
/// - i: A valid index of the collection.
@@ -842,9 +841,9 @@ public protocol Collection : _Indexable, Sequence {
842841
/// print(j)
843842
/// // Prints "nil"
844843
///
845-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
846-
/// before the `startIndex` of this collection, unless the index passed as
847-
/// `limit` prevents offsetting beyond those bounds.
844+
/// The value passed as `n` must not offset `i` beyond the bounds of the
845+
/// collection, unless the index passed as `limit` prevents offsetting
846+
/// beyond those bounds.
848847
///
849848
/// - Parameters:
850849
/// - i: A valid index of the collection.
@@ -941,8 +940,8 @@ extension _Indexable {
941940
/// print(s[i])
942941
/// // Prints "t"
943942
///
944-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
945-
/// before the `startIndex` of this collection.
943+
/// The value passed as `n` must not offset `i` beyond the bounds of the
944+
/// collection.
946945
///
947946
/// - Parameters:
948947
/// - i: A valid index of the collection.
@@ -983,9 +982,9 @@ extension _Indexable {
983982
/// print(j)
984983
/// // Prints "nil"
985984
///
986-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
987-
/// before the `startIndex` of this collection, unless the index passed as
988-
/// `limit` prevents offsetting beyond those bounds.
985+
/// The value passed as `n` must not offset `i` beyond the bounds of the
986+
/// collection, unless the index passed as `limit` prevents offsetting
987+
/// beyond those bounds.
989988
///
990989
/// - Parameters:
991990
/// - i: A valid index of the collection.
@@ -1010,8 +1009,8 @@ extension _Indexable {
10101009

10111010
/// Offsets the given index by the specified distance.
10121011
///
1013-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
1014-
/// before the `startIndex` of this collection.
1012+
/// The value passed as `n` must not offset `i` beyond the bounds of the
1013+
/// collection.
10151014
///
10161015
/// - Parameters:
10171016
/// - i: A valid index of the collection.
@@ -1029,9 +1028,9 @@ extension _Indexable {
10291028
/// Offsets the given index by the specified distance, or so that it equals
10301029
/// the given limiting index.
10311030
///
1032-
/// The value passed as `n` must not offset `i` beyond the `endIndex` or
1033-
/// before the `startIndex` of this collection, unless the index passed as
1034-
/// `limit` prevents offsetting beyond those bounds.
1031+
/// The value passed as `n` must not offset `i` beyond the bounds of the
1032+
/// collection, unless the index passed as `limit` prevents offsetting
1033+
/// beyond those bounds.
10351034
///
10361035
/// - Parameters:
10371036
/// - i: A valid index of the collection.

stdlib/public/core/CompilerProtocols.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public protocol _ExpressibleByBuiltinIntegerLiteral {
217217
/// To add `ExpressibleByIntegerLiteral` conformance to your custom type,
218218
/// implement the required initializer.
219219
public protocol ExpressibleByIntegerLiteral {
220-
/// A type that can represent an integer literal.
220+
/// A type that represents an integer literal.
221221
///
222222
/// The standard library integer and floating-point types are all valid types
223223
/// for `IntegerLiteralType`.
@@ -260,7 +260,7 @@ public protocol _ExpressibleByBuiltinFloatLiteral {
260260
/// To add `ExpressibleByFloatLiteral` conformance to your custom type,
261261
/// implement the required initializer.
262262
public protocol ExpressibleByFloatLiteral {
263-
/// A type that can represent a floating-point literal.
263+
/// A type that represents a floating-point literal.
264264
///
265265
/// Valid types for `FloatLiteralType` are `Float`, `Double`, and `Float80`
266266
/// where available.
@@ -295,7 +295,7 @@ public protocol _ExpressibleByBuiltinBooleanLiteral {
295295
/// implement the `init(booleanLiteral:)` initializer that creates an instance
296296
/// of your type with the given Boolean value.
297297
public protocol ExpressibleByBooleanLiteral {
298-
/// A type that can represent a Boolean literal, such as `Bool`.
298+
/// A type that represents a Boolean literal, such as `Bool`.
299299
associatedtype BooleanLiteralType : _ExpressibleByBuiltinBooleanLiteral
300300

301301
/// Creates an instance initialized to the given Boolean value.
@@ -335,7 +335,7 @@ public protocol _ExpressibleByBuiltinUnicodeScalarLiteral {
335335
/// To add `ExpressibleByUnicodeScalarLiteral` conformance to your custom type,
336336
/// implement the required initializer.
337337
public protocol ExpressibleByUnicodeScalarLiteral {
338-
/// A type that can represent a Unicode scalar literal.
338+
/// A type that represents a Unicode scalar literal.
339339
///
340340
/// Valid types for `UnicodeScalarLiteralType` are `UnicodeScalar`,
341341
/// `String`, and `StaticString`.
@@ -382,7 +382,7 @@ public protocol _ExpressibleByBuiltinExtendedGraphemeClusterLiteral
382382
public protocol ExpressibleByExtendedGraphemeClusterLiteral
383383
: ExpressibleByUnicodeScalarLiteral {
384384

385-
/// A type that can represent an extended grapheme cluster literal.
385+
/// A type that represents an extended grapheme cluster literal.
386386
///
387387
/// Valid types for `ExtendedGraphemeClusterLiteralType` are `Character`,
388388
/// `String`, and `StaticString`.
@@ -430,7 +430,7 @@ public protocol ExpressibleByStringLiteral
430430
// FIXME: when we have default function implementations in protocols, provide
431431
// an implementation of init(extendedGraphemeClusterLiteral:).
432432

433-
/// A type that can represent a string literal.
433+
/// A type that represents a string literal.
434434
///
435435
/// Valid types for `StringLiteralType` are `String` and `StaticString`.
436436
associatedtype StringLiteralType : _ExpressibleByBuiltinStringLiteral

0 commit comments

Comments
 (0)