Skip to content

Commit 9783c1b

Browse files
authored
Inline all the things! 🎨 (#109)
* Make suffix functions inlinable * Inline all the things!
1 parent e64cff7 commit 9783c1b

14 files changed

+73
-64
lines changed

Sources/Algorithms/Chain.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct Chain2<Base1: Sequence, Base2: Sequence>
1919
/// The second sequence in this chain.
2020
public let base2: Base2
2121

22-
@usableFromInline
22+
@inlinable
2323
internal init(base1: Base1, base2: Base2) {
2424
self.base1 = base1
2525
self.base2 = base2
@@ -35,7 +35,7 @@ extension Chain2: Sequence {
3535
@usableFromInline
3636
internal var iterator2: Base2.Iterator
3737

38-
@usableFromInline
38+
@inlinable
3939
internal init(_ concatenation: Chain2) {
4040
iterator1 = concatenation.base1.makeIterator()
4141
iterator2 = concatenation.base2.makeIterator()
@@ -71,13 +71,13 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
7171
internal let position: Representation
7272

7373
/// Creates a new index into the first underlying collection.
74-
@usableFromInline
74+
@inlinable
7575
internal init(first i: Base1.Index) {
7676
position = .first(i)
7777
}
7878

7979
/// Creates a new index into the second underlying collection.
80-
@usableFromInline
80+
@inlinable
8181
internal init(second i: Base2.Index) {
8282
position = .second(i)
8383
}
@@ -99,7 +99,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
9999

100100
/// Converts an index of `Base1` to the corresponding `Index` by mapping
101101
/// `base1.endIndex` to `base2.startIndex`.
102-
@usableFromInline
102+
@inlinable
103103
internal func convertIndex(_ i: Base1.Index) -> Index {
104104
i == base1.endIndex ? Index(second: base2.startIndex) : Index(first: i)
105105
}
@@ -157,7 +157,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
157157
: offsetBackward(i, by: -n, limitedBy: limit)
158158
}
159159

160-
@usableFromInline
160+
@inlinable
161161
internal func offsetForward(
162162
_ i: Index, by n: Int, limitedBy limit: Index
163163
) -> Index? {
@@ -197,7 +197,7 @@ extension Chain2: Collection where Base1: Collection, Base2: Collection {
197197
}
198198
}
199199

200-
@usableFromInline
200+
@inlinable
201201
internal func offsetBackward(
202202
_ i: Index, by n: Int, limitedBy limit: Index
203203
) -> Index? {

Sources/Algorithms/Chunked.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct LazyChunked<Base: Collection, Subject> {
2727
@usableFromInline
2828
internal var firstUpperBound: Base.Index
2929

30-
@usableFromInline
30+
@inlinable
3131
internal init(
3232
base: Base,
3333
projection: @escaping (Base.Element) -> Subject,
@@ -51,7 +51,7 @@ extension LazyChunked: LazyCollectionProtocol {
5151
@usableFromInline
5252
internal var baseRange: Range<Base.Index>
5353

54-
@usableFromInline
54+
@inlinable
5555
internal init(_ baseRange: Range<Base.Index>) {
5656
self.baseRange = baseRange
5757
}
@@ -72,7 +72,7 @@ extension LazyChunked: LazyCollectionProtocol {
7272

7373
/// Returns the index in the base collection of the end of the chunk starting
7474
/// at the given index.
75-
@usableFromInline
75+
@inlinable
7676
internal func endOfChunk(startingAt start: Base.Index) -> Base.Index {
7777
let subject = projection(base[start])
7878
return base[base.index(after: start)...]
@@ -112,7 +112,7 @@ extension LazyChunked: BidirectionalCollection
112112
{
113113
/// Returns the index in the base collection of the start of the chunk ending
114114
/// at the given index.
115-
@usableFromInline
115+
@inlinable
116116
internal func startOfChunk(endingAt end: Base.Index) -> Base.Index {
117117
let indexBeforeEnd = base.index(before: end)
118118

@@ -174,7 +174,7 @@ extension Collection {
174174
/// predicate.
175175
///
176176
/// - Complexity: O(*n*), where *n* is the length of this collection.
177-
@usableFromInline
177+
@inlinable
178178
internal func chunked<Subject>(
179179
on projection: (Element) throws -> Subject,
180180
by belongInSameGroup: (Subject, Subject) throws -> Bool
@@ -273,7 +273,7 @@ extension ChunkedByCount: Collection {
273273
@usableFromInline
274274
internal let baseRange: Range<Base.Index>
275275

276-
@usableFromInline
276+
@inlinable
277277
internal init(_baseRange: Range<Base.Index>) {
278278
self.baseRange = _baseRange
279279
}
@@ -290,6 +290,7 @@ extension ChunkedByCount: Collection {
290290
}
291291

292292
/// - Complexity: O(1)
293+
@inlinable
293294
public subscript(i: Index) -> Element {
294295
precondition(i < endIndex, "Index out of range")
295296
return base[i.baseRange]
@@ -392,7 +393,7 @@ extension ChunkedByCount {
392393
return index
393394
}
394395

395-
@usableFromInline
396+
@inlinable
396397
internal func offsetForward(
397398
_ i: Index, offsetBy distance: Int, limit: Index? = nil
398399
) -> Index? {
@@ -406,8 +407,8 @@ extension ChunkedByCount {
406407
}
407408

408409
// Convenience to compute offset backward base distance.
409-
@inline(__always)
410-
private func computeOffsetBackwardBaseDistance(
410+
@inlinable
411+
internal func computeOffsetBackwardBaseDistance(
411412
_ i: Index, _ distance: Int
412413
) -> Int {
413414
if i == endIndex {
@@ -423,7 +424,7 @@ extension ChunkedByCount {
423424
return distance * chunkCount
424425
}
425426

426-
@usableFromInline
427+
@inlinable
427428
internal func offsetBackward(
428429
_ i: Index, offsetBy distance: Int, limit: Index? = nil
429430
) -> Index? {
@@ -438,8 +439,8 @@ extension ChunkedByCount {
438439
}
439440

440441
// Helper to compute index(offsetBy:) index.
441-
@inline(__always)
442-
private func makeOffsetIndex(
442+
@inlinable
443+
internal func makeOffsetIndex(
443444
from i: Index, baseBound: Base.Index, distance: Int, baseDistance: Int,
444445
limit: Index?, by limitFn: (Base.Index, Base.Index) -> Bool
445446
) -> Index? {

Sources/Algorithms/Combinations.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct Combinations<Base: Collection> {
2727
/// - Parameters:
2828
/// - base: The collection to iterate over for combinations.
2929
/// - k: The expected size of each combination.
30-
@usableFromInline
30+
@inlinable
3131
internal init(_ base: Base, k: Int) {
3232
self.init(base, kRange: k...k)
3333
}
@@ -37,7 +37,7 @@ public struct Combinations<Base: Collection> {
3737
/// - Parameters:
3838
/// - base: The collection to iterate over for combinations.
3939
/// - kRange: The range of accepted sizes of combinations.
40-
@usableFromInline
40+
@inlinable
4141
internal init<R: RangeExpression>(
4242
_ base: Base, kRange: R
4343
) where R.Bound == Int {
@@ -89,14 +89,15 @@ extension Combinations: Sequence {
8989
internal var kRange: Range<Int>
9090

9191
/// Whether or not iteration is finished (`kRange` is empty)
92-
@usableFromInline
92+
@inlinable
9393
internal var isFinished: Bool {
9494
return kRange.isEmpty
9595
}
9696

9797
@usableFromInline
9898
internal var indexes: [Base.Index]
9999

100+
@inlinable
100101
internal init(_ combinations: Combinations) {
101102
self.base = combinations.base
102103
self.kRange = combinations.kRange ?? 0..<0
@@ -121,7 +122,7 @@ extension Combinations: Sequence {
121122
/// [2, 3, 4] *
122123
/// // Can't advance without needing to go past `base.endIndex`,
123124
/// // so the iteration is finished.
124-
@usableFromInline
125+
@inlinable
125126
internal mutating func advance() {
126127
/// Advances `kRange` by incrementing its `lowerBound` until the range is
127128
/// empty, when iteration is finished.

Sources/Algorithms/Cycle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct Cycle<Base: Collection> {
1414
/// The collection to repeat.
1515
public let base: Base
1616

17-
@usableFromInline
17+
@inlinable
1818
internal init(base: Base) {
1919
self.base = base
2020
}
@@ -29,7 +29,7 @@ extension Cycle: Sequence {
2929
@usableFromInline
3030
var current: Base.Index
3131

32-
@usableFromInline
32+
@inlinable
3333
internal init(base: Base) {
3434
self.base = base
3535
self.current = base.startIndex

Sources/Algorithms/Indexed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct Indexed<Base: Collection> {
1818
/// The base collection.
1919
public let base: Base
2020

21-
@usableFromInline
21+
@inlinable
2222
internal init(base: Base) {
2323
self.base = base
2424
}

Sources/Algorithms/Intersperse.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct Intersperse<Base: Sequence> {
1818
@usableFromInline
1919
internal let separator: Base.Element
2020

21-
@usableFromInline
21+
@inlinable
2222
internal init(base: Base, separator: Base.Element) {
2323
self.base = base
2424
self.separator = separator
@@ -37,7 +37,7 @@ extension Intersperse: Sequence {
3737
@usableFromInline
3838
internal var state = State.start
3939

40-
@usableFromInline
40+
@inlinable
4141
internal init(iterator: Base.Iterator, separator: Base.Element) {
4242
self.iterator = iterator
4343
self.separator = separator
@@ -89,6 +89,11 @@ extension Intersperse: Collection where Base: Collection {
8989
@usableFromInline
9090
internal let representation: Representation
9191

92+
@inlinable
93+
init(representation: Representation) {
94+
self.representation = representation
95+
}
96+
9297
@inlinable
9398
public static func < (lhs: Index, rhs: Index) -> Bool {
9499
switch (lhs.representation, rhs.representation) {
@@ -101,12 +106,12 @@ extension Intersperse: Collection where Base: Collection {
101106
}
102107
}
103108

104-
@usableFromInline
109+
@inlinable
105110
static func element(_ index: Base.Index) -> Self {
106111
Self(representation: .element(index))
107112
}
108113

109-
@usableFromInline
114+
@inlinable
110115
static func separator(next: Base.Index) -> Self {
111116
Self(representation: .separator(next: next))
112117
}

Sources/Algorithms/LazySplit.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct LazySplitSequence<Base: Sequence> {
3434
@usableFromInline
3535
internal let omittingEmptySubsequences: Bool
3636

37-
@usableFromInline
37+
@inlinable
3838
internal init(
3939
base: Base,
4040
isSeparator: @escaping (Base.Element) -> Bool,
@@ -72,7 +72,7 @@ extension LazySplitSequence {
7272
@usableFromInline
7373
internal var sequenceLength = 0
7474

75-
@usableFromInline
75+
@inlinable
7676
internal init(
7777
base: Base.Iterator,
7878
whereSeparator: @escaping (Base.Element) -> Bool,
@@ -360,7 +360,7 @@ public struct LazySplitCollection<Base: Collection> {
360360
@usableFromInline
361361
internal var _startIndex: Index
362362

363-
@usableFromInline
363+
@inlinable
364364
internal init(
365365
base: Base,
366366
isSeparator: @escaping (Base.Element) -> Bool,
@@ -415,7 +415,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
415415
@usableFromInline
416416
internal let splitCount: Int
417417

418-
@usableFromInline
418+
@inlinable
419419
internal init(
420420
baseRange: Range<Base.Index>,
421421
sequenceLength: Int,
@@ -441,7 +441,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
441441

442442
/// Returns the index of the subsequence starting at or after the given base
443443
/// collection index.
444-
@usableFromInline
444+
@inlinable
445445
internal func indexForSubsequence(
446446
atOrAfter lowerBound: Base.Index,
447447
sequenceLength: Int,

Sources/Algorithms/Partition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension MutableCollection {
2121
/// - Complexity: O(*n* log *n*), where *n* is the number of elements.
2222
/// - Precondition:
2323
/// `n == distance(from: range.lowerBound, to: range.upperBound)`
24-
@usableFromInline
24+
@inlinable
2525
internal mutating func stablePartition(
2626
count n: Int,
2727
subrange: Range<Index>,

Sources/Algorithms/Permutations.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct Permutations<Base: Collection> {
2828
/// - base: The collection to iterate over for permutations
2929
/// - k: The expected size of each permutation, or `nil` (default) to
3030
/// iterate over all permutations of the same size as the base collection.
31-
@usableFromInline
31+
@inlinable
3232
internal init(_ base: Base, k: Int? = nil) {
3333
let kRange: ClosedRange<Int>?
3434
if let countToChoose = k {
@@ -45,7 +45,7 @@ public struct Permutations<Base: Collection> {
4545
/// - base: The collection to iterate over for permutations.
4646
/// - kRange: The range of accepted sizes of permutations, or `nil` to
4747
/// iterate over all permutations of the same size as the base collection.
48-
@usableFromInline
48+
@inlinable
4949
internal init<R: RangeExpression>(
5050
_ base: Base, kRange: R?
5151
) where R.Bound == Int {
@@ -84,15 +84,15 @@ extension Permutations: Sequence {
8484
internal var kRange: Range<Int>
8585

8686
/// Whether or not iteration is finished (`kRange` is empty)
87-
@usableFromInline
87+
@inlinable
8888
internal var isFinished: Bool {
8989
return kRange.isEmpty
9090
}
9191

9292
@usableFromInline
9393
internal var indexes: [Base.Index]
9494

95-
@usableFromInline
95+
@inlinable
9696
internal init(_ permutations: Permutations) {
9797
self.base = permutations.base
9898
self.baseCount = permutations.baseCount
@@ -111,7 +111,7 @@ extension Permutations: Sequence {
111111
/// is in ascending order.
112112
///
113113
/// - Complexity: O(*n*), where *n* is the length of the collection.
114-
@usableFromInline
114+
@inlinable
115115
internal mutating func nextState() -> Bool {
116116
let countToChoose = self.kRange.lowerBound
117117
let edge = countToChoose - 1

0 commit comments

Comments
 (0)