Skip to content

Commit d3f7348

Browse files
mbrandonwbkase
authored andcommitted
Swift 4 + performance improvements (#8)
We ran into some serious performance improvements while using DoctorPretty for snapshot testing, so I wanted to see if there was anything I could do to fix it. I optimized a few small things (like using reduce(into:) with mutation instead of the pure version), but the real bottleneck is with the best and fits functions, which are recursive. I was able to rewrite fits as a plain loop, and that improved performance quite a bit, and we'd probably get huge improvements if we could rewrite best.
1 parent fbb4fda commit d3f7348

18 files changed

+98
-55
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1
1+
4.0

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
os:
22
- linux
33
- osx
4-
env:
4+
env:
55
language: generic
66
sudo: required
77
dist: trusty
8-
osx_image: xcode8.3
8+
osx_image: xcode9.2
99
install:
1010
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
1111
script:
1212
- swift build
13-
- SWIFTPM_TEST_DoctorPretty=YES swift test
14-
13+
- swift test

Package.resolved

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
// swift-tools-version:3.1
1+
// swift-tools-version:4.0
22

33
import PackageDescription
4-
import Foundation
5-
6-
// HACK from https://github.com/ReactiveCocoa/ReactiveSwift/blob/master/Package.swift
7-
var isSwiftPMTest: Bool {
8-
return ProcessInfo.processInfo.environment["SWIFTPM_TEST_DoctorPretty"] == "YES"
9-
}
104

115
let package = Package(
126
name: "DoctorPretty",
13-
targets: [],
147
dependencies: [
15-
.Package(url: "https://github.com/typelift/Algebra.git", majorVersion: 0, minor: 2),
16-
.Package(url: "https://github.com/typelift/Swiftx.git", majorVersion: 0, minor: 5)
17-
] + (isSwiftPMTest ?
18-
[.Package(url: "https://github.com/typelift/SwiftCheck.git", versions: Version(0,6,0)..<Version(1,0,0))] :
19-
[])
20-
8+
.package(url: "https://github.com/typelift/Algebra.git", .exact("0.2.0")),
9+
.package(url: "https://github.com/typelift/Swiftx.git", .exact("0.6.0")),
10+
.package(url: "https://github.com/typelift/SwiftCheck.git", .exact("0.9.1"))
11+
],
12+
targets: [
13+
.target(name: "DoctorPretty", dependencies: ["Algebra", "Swiftx"]),
14+
.testTarget(name: "DoctorPrettyTests", dependencies: ["DoctorPretty", "SwiftCheck"]),
15+
]
2116
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public indirect enum Doc {
3535
}
3636

3737
public static func text(_ str: String) -> Doc {
38-
return str == "" ? .empty : ._text(length: str.characters.count, str)
38+
return str == "" ? .empty : ._text(length: str.count, str)
3939
}
4040

4141
public static var line: Doc {
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance =
1414
/// Intersperses punctuation inside docs
1515
public func punctuate(with punctuation: Doc) -> [Doc] {
1616
if let d = first {
17-
return [d] + self.dropFirst().reduce([Doc]()) { t in
18-
let (acc, d2) = t
19-
return acc <> [punctuation, d2]
17+
return [d] + self.dropFirst().reduce(into: [Doc]()) { acc, d2 in
18+
acc.append(punctuation)
19+
acc.append(d2)
2020
}
2121
} else {
2222
return []
@@ -47,8 +47,8 @@ extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance =
4747
return (
4848
.nest(indent,
4949
left <&&> punctuated.sep()
50-
) <&&> right
51-
).grouped
50+
) <&&> right
51+
).grouped
5252
}
5353

5454
/// See @enclose
@@ -66,3 +66,4 @@ extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance =
6666
return enclose(left: Doc.lbrace, right: Doc.rbrace, separator: Doc.semi, indent: indent)
6767
}
6868
}
69+

0 commit comments

Comments
 (0)