Skip to content
Merged
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
10 changes: 8 additions & 2 deletions Sources/ConnectionPoolModule/TinyFastSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ struct TinyFastSequence<Element>: Sequence {
self.base = .none(reserveCapacity: 0)
case 1:
self.base = .one(collection.first!, reserveCapacity: 0)
case 2:
self.base = .two(
collection.first!,
collection[collection.index(after: collection.startIndex)],
reserveCapacity: 0
)
default:
if let collection = collection as? Array<Element> {
self.base = .n(collection)
Expand All @@ -46,7 +52,7 @@ struct TinyFastSequence<Element>: Sequence {
case 1:
self.base = .one(max2Sequence.first!, reserveCapacity: 0)
case 2:
self.base = .n(Array(max2Sequence))
self.base = .two(max2Sequence.first!, max2Sequence.second!, reserveCapacity: 0)
default:
fatalError()
}
Expand Down Expand Up @@ -169,7 +175,7 @@ struct TinyFastSequence<Element>: Sequence {

case .n(let array):
if self.index < array.endIndex {
defer { self.index += 1}
defer { self.index += 1 }
return array[self.index]
}
return nil
Expand Down
8 changes: 8 additions & 0 deletions Tests/ConnectionPoolModuleTests/TinyFastSequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ final class TinyFastSequenceTests: XCTestCase {
XCTAssertEqual(array.capacity, 8)

var twoElemSequence = TinyFastSequence<Int>([1, 2])
twoElemSequence.append(3)
twoElemSequence.reserveCapacity(8)
guard case .n(let array) = twoElemSequence.base else {
return XCTFail("Expected sequence to be backed by an array")
}
XCTAssertEqual(array.capacity, 8)

var threeElemSequence = TinyFastSequence<Int>([1, 2, 3])
threeElemSequence.reserveCapacity(8)
guard case .n(let array) = threeElemSequence.base else {
return XCTFail("Expected sequence to be backed by an array")
}
XCTAssertEqual(array.capacity, 8)
}

func testNewSequenceSlowPath() {
Expand Down
Loading