Skip to content

Commit ea7564e

Browse files
refactor(perf): Makes static regexes global
This reduces the benchmark `graphql` time by 51%, and malloc calls by 77%
1 parent 3e727a6 commit ea7564e

3 files changed

Lines changed: 24 additions & 25 deletions

File tree

Sources/GraphQL/Error/SyntaxError.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ func splitLines(string: String) -> [String] {
5353
var location = 0
5454

5555
let nsstring = NSString(string: string)
56-
do {
57-
let regex = try NSRegularExpression(pattern: "\r\n|[\n\r]", options: [])
58-
for match in regex.matches(in: string, options: [], range: NSRange(0 ..< nsstring.length)) {
59-
let range = NSRange(location ..< match.range.location)
60-
lines.append(nsstring.substring(with: range))
61-
location = match.range.location + match.range.length
62-
}
63-
} catch {
64-
// Let lines and location remain unchanged
56+
for match in newLineRegex.matches(
57+
in: string,
58+
options: [],
59+
range: NSRange(0 ..< nsstring.length)
60+
) {
61+
let range = NSRange(location ..< match.range.location)
62+
lines.append(nsstring.substring(with: range))
63+
location = match.range.location + match.range.length
6564
}
6665

6766
if lines.isEmpty {

Sources/GraphQL/Language/Location.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ func getLocation(source: Source, position: Int) -> SourceLocation {
1818
var line = 1
1919
var column = position + 1
2020

21-
do {
22-
let regex = try NSRegularExpression(pattern: "\r\n|[\n\r]", options: [])
23-
let matches = regex.matches(
24-
in: source.body,
25-
options: [],
26-
range: NSRange(0 ..< source.body.utf16.count)
27-
)
28-
for match in matches where match.range.location < position {
29-
line += 1
30-
column = position + 1 - (match.range.location + match.range.length)
31-
}
32-
} catch {
33-
// Leave line and position unset if regex fails
21+
let matches = newLineRegex.matches(
22+
in: source.body,
23+
options: [],
24+
range: NSRange(0 ..< source.body.utf16.count)
25+
)
26+
for match in matches where match.range.location < position {
27+
line += 1
28+
column = position + 1 - (match.range.location + match.range.length)
3429
}
3530

3631
return SourceLocation(line: line, column: column)
3732
}
33+
34+
let newLineRegex = try! NSRegularExpression(pattern: "\r\n|[\n\r]", options: [])

Sources/GraphQL/Utilities/AssertValidName.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public enum InvalidNameError: Error, CustomStringConvertible {
1212
}
1313

1414
func assertValid(name: String) throws {
15-
let regex = try NSRegularExpression(pattern: "^[_a-zA-Z][_a-zA-Z0-9]*$", options: [])
16-
17-
let range = regex.rangeOfFirstMatch(
15+
let range = validNameRegex.rangeOfFirstMatch(
1816
in: name,
1917
options: [],
2018
range: NSRange(0 ..< name.utf16.count)
@@ -24,3 +22,8 @@ func assertValid(name: String) throws {
2422
throw InvalidNameError.invalidName(name)
2523
}
2624
}
25+
26+
private let validNameRegex = try! NSRegularExpression(
27+
pattern: "^[_a-zA-Z][_a-zA-Z0-9]*$",
28+
options: []
29+
)

0 commit comments

Comments
 (0)