Skip to content

Commit

Permalink
[stdlib][swift-3-indexing-model] speeding up the compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Moiseev committed Apr 8, 2016
1 parent 6c56af5 commit 1a875cb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 31 deletions.
7 changes: 0 additions & 7 deletions stdlib/public/core/LazyCollection.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,6 @@ extension ${TraversalCollection} where Self : LazyCollectionProtocol {

% end

extension LazyCollectionProtocol {
/// Identical to `self`.
public var lazy: Self { // Don't re-wrap already-lazy collections
return self
}
}

@available(*, unavailable, renamed: "LazyCollectionProtocol")
public typealias LazyCollectionType = LazyCollectionProtocol

Expand Down
10 changes: 7 additions & 3 deletions test/1_stdlib/Inputs/flatMap.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ ${Test}.test("flatMap/${Kind}/Lazy") {
elements: test.sequence.map(OpaqueValue.init))
let closureLifetimeTracker = LifetimeTracked(0)
var timesClosureWasCalled = 0
var result = s.lazy.flatMap {

let lazySeq = s.lazy
var result = lazySeq.flatMap {
(element: OpaqueValue<Int>) -> ${Minimal}<OpaqueValue<Int32>> in
_blackHole(closureLifetimeTracker)
timesClosureWasCalled += 1
let elements = test.transform(element.value)
return ${Minimal}<OpaqueValue<Int32>>(
elements: test.transform(element.value).map { OpaqueValue(Int32($0)) })
elements: elements.map { OpaqueValue($0) })
}
expectEqual(0, timesClosureWasCalled, "Unexpected eagerness")

Expand Down Expand Up @@ -47,8 +50,9 @@ ${Test}.test("flatMap/${Kind}/Lazy") {

// FIXME: we are not calling check${Kind} because of the same issue again.
// <rdar://problem/21989896>
let expected = test.expected.map(OpaqueValue.init)
expectEqualSequence(
test.expected.map(OpaqueValue.init), result,
expected, result,
stackTrace: SourceLocStack().with(test.loc)
) { $0.value == $1.value }
% end
Expand Down
32 changes: 18 additions & 14 deletions test/1_stdlib/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,25 @@ extension LazyMapCollection where Base : TestProtocol1, Element : TestProtocol1
// CHECK: testing...
print("testing...")

// Test mapping a collection
// CHECK-NEXT: [6, 9, 12, 15, 18, 21]
let a = Array((2..<8).lazy.map { $0 * 3 })
print(a)

// Test mapping a sequence
let s = a.makeIterator().lazy.map { $0 / 3 }
// CHECK-NEXT: <2, 3, 4, 5, 6, 7>
print("<", terminator: "")
var prefix = ""
for x in s {
print("\(prefix)\(x)", terminator: "")
prefix = ", "
do {
// Test mapping a collection
// CHECK-NEXT: [6, 9, 12, 15, 18, 21]
let lazyRange = (2..<8).lazy
let a = Array(lazyRange.map { $0 * 3 })
print(a)

// Test mapping a sequence
let lazySeq = a.makeIterator().lazy
let s = lazySeq.map { $0 / 3 }
// CHECK-NEXT: <2, 3, 4, 5, 6, 7>
print("<", terminator: "")
var prefix = ""
for x in s {
print("\(prefix)\(x)", terminator: "")
prefix = ", "
}
print(">")
}
print(">")

//===--- Avoid creating gratuitously self-destructive sequences -----------===//

Expand Down
7 changes: 5 additions & 2 deletions test/1_stdlib/Range.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ RangeTestSuite.test("map()") {
}

RangeTestSuite.test("reversed()") {
var result = (0..<10).lazy.reversed()
expectType(Array<Int>.self, &result)
let lazyRange = (0..<10).lazy
var result = lazyRange.reversed()
typealias Expected = LazyRandomAccessCollection<
ReversedRandomAccessCollection<CountableRange<Int>>>
expectType(Expected.self, &result)
expectEqualSequence(
[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
result)
Expand Down
10 changes: 8 additions & 2 deletions test/1_stdlib/StringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,15 @@ func checkHasPrefixHasSuffix(
}
let expectHasPrefix = lhsNFDGraphemeClusters.starts(
with: rhsNFDGraphemeClusters, isEquivalent: (==))

let lazyLhs = lhsNFDGraphemeClusters.lazy
let reversedLhs = lazyLhs.reversed()

let lazyRhs = rhsNFDGraphemeClusters.lazy
let reversedRhs = lazyRhs.reversed()

let expectHasSuffix =
lhsNFDGraphemeClusters.lazy.reversed().starts(
with: rhsNFDGraphemeClusters.lazy.reversed(), isEquivalent: (==))
reversedLhs.starts(with: reversedRhs, isEquivalent: (==))

expectEqual(expectHasPrefix, lhs.hasPrefix(rhs), stackTrace: stackTrace)
expectEqual(
Expand Down
5 changes: 3 additions & 2 deletions validation-test/stdlib/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,12 @@ for (step, evilBoundsCheck) in [ (1, true), (-1, false), (-1, true) ] {
}

for (op, rangeMax) in ["Grow":0, "Shrink":200] {
let lazyRange = (0..<200).lazy
let t3 = ArrayTestSuite.test("\(testPrefix)/replaceSubrange/\(op)Unique")
(_isDebugAssertConfiguration() ? t3.crashOutputMatches(message) : t3)
.code {
let evil = EvilCollection(step, boundsChecked: evilBoundsCheck)
var a = Array((0..<200).lazy.map { LifetimeTracked($0) })
var a = Array(lazyRange.map { LifetimeTracked($0) })
if expectedToFail {
expectCrashLater()
}
Expand All @@ -639,7 +640,7 @@ for (step, evilBoundsCheck) in [ (1, true), (-1, false), (-1, true) ] {
(_isDebugAssertConfiguration() ? t4.crashOutputMatches(message) : t4)
.code {
let evil = EvilCollection(step, boundsChecked: evilBoundsCheck)
var a = Array((0..<200).lazy.map { LifetimeTracked($0) })
var a = Array(lazyRange.map { LifetimeTracked($0) })
var b = a
if expectedToFail {
expectCrashLater()
Expand Down
4 changes: 3 additions & 1 deletion validation-test/stdlib/CollectionType.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,11 @@ CollectionTypeTests.test("index(of:)/ContinueSearch") {
let c = MinimalCollection(
elements: [1, 2, 1, 3, 1].map(MinimalEquatableValue.init))
let foundIndices = c._test_indicesOf(MinimalEquatableValue(1))
let actual = foundIndices.map { c.distance(from: c.startIndex, to: $0) }
expectEqualSequence(
[ 0, 2, 4 ],
foundIndices.map { c.distance(from: c.startIndex, to: $0) })
actual
)
}
}

Expand Down

0 comments on commit 1a875cb

Please sign in to comment.