Skip to content

Commit a982b62

Browse files
fix: Optimizes parsing of large non-UTF8 queries
While this doesn't impact UTF8 strings built up in Swift, Strings decoded from random data (like through Vapor, for example) may not be marked as UTF8, which makes the lexer UTF8 conversions very expensive. In evaluation of 450kb requests, performance improved from 27min to 5sec.
1 parent dda63e4 commit a982b62

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Sources/GraphQL/Language/Source.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
* but is mostly useful for clients who store GraphQL documents in
44
* source files; for example, if the GraphQL input is in a file Foo.graphql,
55
* it might be useful for name to be "Foo.graphql".
6+
*
7+
* Note that since Source parsing is heavily UTF8 dependent, the body
8+
* is converted into contiguous UTF8 bytes if necessary for optimal performance.
69
*/
710
public struct Source {
811
public let body: String
912
public let name: String
1013

1114
public init(body: String, name: String = "GraphQL") {
12-
self.body = body
15+
var utf8Body = body
16+
utf8Body.makeContiguousUTF8()
17+
18+
self.body = utf8Body
1319
self.name = name
1420
}
1521
}

0 commit comments

Comments
 (0)