-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathFindStringNaive.swift
184 lines (169 loc) · 5.21 KB
/
FindStringNaive.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//===--- FindStringNaive.swift --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TestsUtils
// Mini benchmark implementing a naive String search algorithm that
// at the moment shows a lot of ARC traffic.
let t: [BenchmarkCategory] = [.String, .refcount]
var longStringFoFoFoFox: String?
var longArrayFoFoFoFox: [UInt8]?
public let benchmarks = [
BenchmarkInfo(
name: "FindString.Loop1.Substring",
runFunction: runBenchLoop1Substring,
tags: t,
setUpFunction: {
longStringFoFoFoFox = String(repeating: "fo", count: 5_000) + "fox <-- needle"
}),
BenchmarkInfo(
name: "FindString.Rec3.String",
runFunction: runBenchRecursive3String,
tags: t,
setUpFunction: {
longStringFoFoFoFox = String(repeating: "fo", count: 500) + "fox <-- needle"
}),
BenchmarkInfo(
name: "FindString.Rec3.Substring",
runFunction: runBenchRecursive3Substring,
tags: t,
setUpFunction: {
longStringFoFoFoFox = String(repeating: "fo", count: 500) + "fox <-- needle"
}),
BenchmarkInfo(
name: "FindString.Loop1.Array",
runFunction: runBenchLoop1Array,
tags: t,
setUpFunction: {
longArrayFoFoFoFox = []
longArrayFoFoFoFox!.reserveCapacity(1_100_000)
for _ in 0 ..< 500_000 {
longArrayFoFoFoFox!.append(contentsOf: "fo".utf8)
}
longArrayFoFoFoFox!.append(contentsOf: "fox <-- needle".utf8)
}),
BenchmarkInfo(
name: "FindString.Rec3.Array",
runFunction: runBenchRecursive3ArrayOfUTF8,
tags: t,
setUpFunction: {
longArrayFoFoFoFox = []
longArrayFoFoFoFox!.reserveCapacity(11_000)
for _ in 0 ..< 5_000 {
longArrayFoFoFoFox!.append(contentsOf: "fo".utf8)
}
longArrayFoFoFoFox!.append(contentsOf: "fox <-- needle".utf8)
}),
]
func findOne<S: StringProtocol>(
_ string: S,
needle: Character
) -> String.Index? {
var index = string.startIndex
while index < string.endIndex {
let nextIndex = string.index(after: index)
if string[index] == needle {
return index
}
index = nextIndex
}
return nil
}
func findThreeRecursive<S: StringProtocol>(
_ string: S,
needle1: Character,
needle2: Character?,
needle3: Character?
) -> String.Index? {
var index = string.startIndex
while index < string.endIndex {
let nextIndex = string.index(after: index)
if string[index] == needle1 {
// Check subsequent needles recursively (if applicable)
guard let needle2 = needle2 else { return index }
if findThreeRecursive(
string[nextIndex...].prefix(2), needle1: needle2, needle2: needle3, needle3: nil
) == nextIndex {
return index
}
}
index = nextIndex
}
return nil
}
func findOneOnUTF8Collection<Bytes: Collection>(
_ string: Bytes,
needle: UInt8
) -> Bytes.Index? where Bytes.Element == UInt8 {
var index = string.startIndex
while index < string.endIndex {
let nextIndex = string.index(after: index)
if string[index] == needle {
return index
}
index = nextIndex
}
return nil
}
func findThreeOnUTF8Collection<Bytes: Collection>(
_ string: Bytes,
needle1: UInt8,
needle2: UInt8?,
needle3: UInt8?
) -> Bytes.Index? where Bytes.Element == UInt8 {
var index = string.startIndex
while index < string.endIndex {
let nextIndex = string.index(after: index)
if string[index] == needle1 {
// Check subsequent needles recursively (if applicable)
guard let needle2 = needle2 else { return index }
if findThreeOnUTF8Collection(
string[nextIndex...].prefix(2), needle1: needle2, needle2: needle3, needle3: nil
) == nextIndex {
return index
}
}
index = nextIndex
}
return nil
}
@inline(never)
func runBenchLoop1Substring(iterations: Int) {
for _ in 0 ..< iterations {
precondition(findOne(longStringFoFoFoFox![...], needle: "x") != nil)
}
}
@inline(never)
func runBenchLoop1Array(iterations: Int) {
for _ in 0 ..< iterations {
precondition(findOneOnUTF8Collection(longArrayFoFoFoFox!, needle: UInt8(ascii: "x")) != nil)
}
}
@inline(never)
func runBenchRecursive3Substring(iterations: Int) {
for _ in 0 ..< iterations {
precondition(findThreeRecursive(longStringFoFoFoFox![...], needle1: "f", needle2: "o", needle3: "x") != nil)
}
}
@inline(never)
func runBenchRecursive3String(iterations: Int) {
for _ in 0 ..< iterations {
precondition(findThreeRecursive(longStringFoFoFoFox!, needle1: "f", needle2: "o", needle3: "x") != nil)
}
}
@inline(never)
func runBenchRecursive3ArrayOfUTF8(iterations: Int) {
for _ in 0 ..< iterations {
precondition(findThreeOnUTF8Collection(longArrayFoFoFoFox!,
needle1: UInt8(ascii: "f"),
needle2: UInt8(ascii: "o"),
needle3: UInt8(ascii: "x")) != nil)
}
}