-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65574bc
commit 326d958
Showing
26 changed files
with
6,278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [Point-Free](https://www.pointfree.co) | ||
|
||
> #### This directory contains code from Point-Free Episode: [Parsing Performance: Combinators](https://www.pointfree.co/episodes/ep128-parsing-performance-combinators) | ||
> | ||
> We're now comfortable with the performance characteristics of Swift strings and their abstraction levels, so let's apply this knowledge to the parser type. We will convert parsers of several complexities from substring to lower-level abstractions to get a handle on the differences and determine the abstractions we should work in. |
54 changes: 54 additions & 0 deletions
54
0128-parsing-performance-pt2/parser-performance.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
let bigString = String.init(repeating: "A", count: 1_000_000) | ||
|
||
var copy = bigString | ||
copy.removeFirst() | ||
|
||
var anotherCopy = bigString | ||
anotherCopy.removeFirst() | ||
|
||
let truncatedBigString = bigString.dropFirst() as Substring | ||
|
||
bigString.index(after: bigString.startIndex) == truncatedBigString.startIndex | ||
|
||
var copy1 = truncatedBigString.dropFirst(100) | ||
var copy2 = truncatedBigString.dropFirst(1_000) | ||
var copy3 = truncatedBigString.dropFirst(10_000) | ||
|
||
let acuteE1 = "é" | ||
let acuteE2 = "é" | ||
acuteE1 == acuteE2 | ||
acuteE1.count | ||
acuteE2.count | ||
|
||
acuteE1.unicodeScalars.count | ||
acuteE2.unicodeScalars.count | ||
|
||
Array(acuteE1.unicodeScalars) | ||
Array(acuteE2.unicodeScalars) | ||
|
||
acuteE1.unicodeScalars.elementsEqual(acuteE2.unicodeScalars) | ||
|
||
"🇺" | ||
"🇸" | ||
"🇺" + "🇸" | ||
"\u{1F1FA}\u{1F1F8}" | ||
|
||
|
||
|
||
"🇺🇸".dropFirst() | ||
|
||
String("🇺🇸".unicodeScalars.dropFirst()) | ||
|
||
Array(acuteE1.utf8) | ||
Array(acuteE2.utf8) | ||
|
||
Array("🇺🇸".unicodeScalars) | ||
Array("🇺🇸".utf8) | ||
Array("👨👨👧👧".unicodeScalars) | ||
Array("👨👨👧👧".utf8) | ||
"👨👨👧👧".utf8.count | ||
|
||
"🇺🇸".unicodeScalars.first == ("🇺" as Unicode.Scalar) | ||
|
||
"🇺🇸".utf8.starts(with: [240, 159, 135, 186]) |
4 changes: 4 additions & 0 deletions
4
0128-parsing-performance-pt2/parser-performance.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos' buildActiveScheme='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
7 changes: 7 additions & 0 deletions
7
...rmance-pt2/string-performance/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
...-pt2/string-performance/.swiftpm/xcode/xcshareddata/xcschemes/string-performance.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1230" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "string-performance" | ||
BuildableName = "string-performance" | ||
BlueprintName = "string-performance" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "NO" | ||
buildForArchiving = "NO" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "string-performanceTests" | ||
BuildableName = "string-performanceTests" | ||
BlueprintName = "string-performanceTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "string-performanceTests" | ||
BuildableName = "string-performanceTests" | ||
BlueprintName = "string-performanceTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Release" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "string-performance" | ||
BuildableName = "string-performance" | ||
BlueprintName = "string-performance" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
<CommandLineArguments> | ||
<CommandLineArgument | ||
argument = "--allow-debug-build" | ||
isEnabled = "YES"> | ||
</CommandLineArgument> | ||
</CommandLineArguments> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "string-performance" | ||
BuildableName = "string-performance" | ||
BlueprintName = "string-performance" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
24 changes: 24 additions & 0 deletions
24
0128-parsing-performance-pt2/string-performance/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "string-performance", | ||
platforms: [.macOS(.v10_15)], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
.package(name: "Benchmark", url: "https://github.com/google/swift-benchmark", .branch("main")) | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "string-performance", | ||
dependencies: [.product(name: "Benchmark", package: "Benchmark")]), | ||
.testTarget( | ||
name: "string-performanceTests", | ||
dependencies: ["string-performance"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# string-performance | ||
|
||
A description of this package. |
50 changes: 50 additions & 0 deletions
50
...ing-performance-pt2/string-performance/Sources/string-performance/Benchmark-Copying.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Benchmark | ||
|
||
let copyingSuite = BenchmarkSuite(name: "Copying") { suite in | ||
let string = String.init(repeating: "👨👨👧👧", count: 1_000_000) | ||
|
||
suite.benchmark("String") { | ||
var copy = string | ||
copy.removeFirst() | ||
var copy1 = copy | ||
copy1.removeFirst() | ||
var copy2 = copy1 | ||
copy2.removeFirst() | ||
var copy3 = copy2 | ||
copy3.removeFirst() | ||
} | ||
|
||
suite.benchmark("Substring") { | ||
var copy = string[...] | ||
copy.removeFirst() | ||
var copy1 = copy | ||
copy1.removeFirst() | ||
var copy2 = copy1 | ||
copy2.removeFirst() | ||
var copy3 = copy2 | ||
copy3.removeFirst() | ||
} | ||
|
||
suite.benchmark("UnicodeScalars") { | ||
var copy = string[...].unicodeScalars | ||
copy.removeFirst() | ||
var copy1 = copy | ||
copy1.removeFirst() | ||
var copy2 = copy1 | ||
copy2.removeFirst() | ||
var copy3 = copy2 | ||
copy3.removeFirst() | ||
} | ||
|
||
suite.benchmark("UTF8") { | ||
var copy = string[...].utf8 | ||
copy.removeFirst() | ||
var copy1 = copy | ||
copy1.removeFirst() | ||
var copy2 = copy1 | ||
copy2.removeFirst() | ||
var copy3 = copy2 | ||
copy3.removeFirst() | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
...-performance-pt2/string-performance/Sources/string-performance/Benchmark-IntParsing.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Benchmark | ||
import Foundation | ||
|
||
let intParserSuite = BenchmarkSuite(name: "Int Parsing") { suite in | ||
let string = "1234567890" | ||
|
||
suite.benchmark("Int.init") { | ||
precondition(Int(string) == 1234567890) | ||
} | ||
|
||
suite.benchmark("Substring") { | ||
var string = string[...] | ||
precondition(Parser.int.run(&string) == 1234567890) | ||
} | ||
|
||
suite.benchmark("UnicodeScalar") { | ||
var string = string[...].unicodeScalars | ||
precondition(Parser.int.run(&string) == 1234567890) | ||
} | ||
|
||
suite.benchmark("UTF8") { | ||
var string = string[...].utf8 | ||
precondition(Parser.int.run(&string) == 1234567890) | ||
} | ||
|
||
var scanner: Scanner! | ||
suite.register( | ||
benchmark: Benchmarking( | ||
name: "Scanner", | ||
setUp: { scanner = Scanner(string: string) } | ||
) { _ in | ||
precondition(scanner.scanInt() == 1234567890) | ||
} | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...arsing-performance-pt2/string-performance/Sources/string-performance/Benchmark-Race.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Benchmark | ||
|
||
let raceSuite = BenchmarkSuite(name: "Race") { suite in | ||
suite.benchmark("Substring") { | ||
var input = upcomingRaces[...] | ||
precondition(races.run(&input)?.count == 4) | ||
} | ||
|
||
suite.benchmark("UTF8") { | ||
var input = upcomingRaces[...].utf8 | ||
precondition(racesUtf8.run(&input)?.count == 4) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...g-performance-pt2/string-performance/Sources/string-performance/Benchmark-XcodeLogs.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Benchmark | ||
|
||
let xcodeLogsSuite = BenchmarkSuite(name: "Xcode Logs") { suite in | ||
suite.benchmark("Substring") { | ||
var input = xcodeLogs[...] | ||
precondition(testResults.run(&input)?.count == 283) | ||
} | ||
|
||
suite.benchmark("UTF8") { | ||
var input = xcodeLogs[...].utf8 | ||
precondition(testResultsUtf8.run(&input)?.count == 283) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...-parsing-performance-pt2/string-performance/Sources/string-performance/Benchmarking.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Benchmark | ||
|
||
struct Benchmarking: AnyBenchmark { | ||
var name: String | ||
private var run_: (_ state: inout BenchmarkState) throws -> Void | ||
private var setUp_: () -> Void | ||
|
||
init( | ||
name: String, | ||
setUp: @escaping () -> Void = {}, | ||
run: @escaping (_ state: inout BenchmarkState) throws -> Void | ||
) { | ||
self.name = name | ||
self.setUp_ = setUp | ||
self.run_ = run | ||
} | ||
|
||
func setUp() { | ||
self.setUp_() | ||
} | ||
|
||
func run(_ state: inout BenchmarkState) throws { | ||
try self.run_(&state) | ||
} | ||
|
||
// No-op | ||
var settings: [BenchmarkSetting] { [] } | ||
func tearDown() {} | ||
} |
Oops, something went wrong.