Skip to content

Commit

Permalink
128
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Dec 7, 2020
1 parent 65574bc commit 326d958
Show file tree
Hide file tree
Showing 26 changed files with 6,278 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
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("master"))
.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.
Expand Down
5 changes: 5 additions & 0 deletions 0128-parsing-performance-pt2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
5 changes: 5 additions & 0 deletions 0128-parsing-performance-pt2/README.md
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.
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])
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>
5 changes: 5 additions & 0 deletions 0128-parsing-performance-pt2/string-performance/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 0128-parsing-performance-pt2/string-performance/Package.swift
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"]),
]
)
3 changes: 3 additions & 0 deletions 0128-parsing-performance-pt2/string-performance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# string-performance

A description of this package.
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()
}
}

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)
}
)
}
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)
}
}
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)
}
}
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() {}
}
Loading

0 comments on commit 326d958

Please sign in to comment.