From 4cacc28669333cccfe5ec78fed10ad48b46c506d Mon Sep 17 00:00:00 2001 From: Benedikt Terhechte Date: Mon, 16 Jan 2023 15:15:58 +0100 Subject: [PATCH] ~20% faster json decoding --- Package.swift | 6 +++++- Sources/SwiftGraphQL/AnyCodable.swift | 3 ++- Sources/SwiftGraphQL/HTTP+WebSockets.swift | 5 +++-- Sources/SwiftGraphQL/Result.swift | 6 ++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index def6c29e..3d769028 100644 --- a/Package.swift +++ b/Package.swift @@ -37,12 +37,16 @@ let package = Package( .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.4"), .package(url: "https://github.com/unstoppablefi/GzipSwift", .upToNextMajor(from: "5.0.0")), + .package(url: "https://github.com/michaeleisel/ZippyJSON", .upToNextMajor(from: "1.0.0")), ], targets: [ /* SwiftGraphQL */ .target( name: "SwiftGraphQL", - dependencies: [.product(name: "Gzip", package: "GzipSwift")], + dependencies: [ + .product(name: "Gzip", package: "GzipSwift"), + .product(name: "ZippyJSON", package: "ZippyJSON") + ], path: "Sources/SwiftGraphQL" ), .target( diff --git a/Sources/SwiftGraphQL/AnyCodable.swift b/Sources/SwiftGraphQL/AnyCodable.swift index 3be9164b..208eec95 100644 --- a/Sources/SwiftGraphQL/AnyCodable.swift +++ b/Sources/SwiftGraphQL/AnyCodable.swift @@ -1,4 +1,5 @@ import Foundation +import ZippyJSON // Based on: https://github.com/yonaskolb/Codability/blob/master/Sources/Codability/AnyCodable.swift @@ -123,7 +124,7 @@ extension AnyCodable: Equatable { /// Calling this allows you to compare AnyCodables that were initialised with a custom Encodable type. mutating func recode() throws { let encoded = try JSONEncoder().encode(self) - self = try JSONDecoder().decode(AnyCodable.self, from: encoded) + self = try ZippyJSONDecoder().decode(AnyCodable.self, from: encoded) } } diff --git a/Sources/SwiftGraphQL/HTTP+WebSockets.swift b/Sources/SwiftGraphQL/HTTP+WebSockets.swift index 32a21d27..d4102bf6 100644 --- a/Sources/SwiftGraphQL/HTTP+WebSockets.swift +++ b/Sources/SwiftGraphQL/HTTP+WebSockets.swift @@ -1,5 +1,6 @@ import Foundation import Network +import ZippyJSON import os.log @@ -47,7 +48,7 @@ public class GraphQLSocket { private var queue: [(GraphQLSocket) -> Void] = [] private var subscriptions: [String: (GraphQLSocketMessage) -> Void] = [:] - private var decoder = JSONDecoder() + private var decoder = ZippyJSONDecoder() private var encoder = JSONEncoder() // Every successful ping should be matched by a successful pong @@ -101,7 +102,7 @@ public class GraphQLSocket { // log: OSLog.subscription, // type: .debug, (String(data: data, encoding: .utf8) ?? "Invalid .utf8") // ) - guard var message = try? JSONDecoder().decode(Message.self, from: data) else { + guard var message = try? ZippyJSONDecoder().decode(Message.self, from: data) else { os_log("Invalid JSON Payload", log: OSLog.subscription, type: .debug) return false } diff --git a/Sources/SwiftGraphQL/Result.swift b/Sources/SwiftGraphQL/Result.swift index 3e27b5b3..4e097346 100644 --- a/Sources/SwiftGraphQL/Result.swift +++ b/Sources/SwiftGraphQL/Result.swift @@ -1,4 +1,6 @@ import Foundation +import ZippyJSON +//import zippyjson // MARK: - GraphQL Result @@ -11,12 +13,12 @@ public struct GraphQLResult { extension GraphQLResult: Equatable where Type: Equatable, TypeLock: Decodable {} extension GraphQLResult where TypeLock: Decodable { - init(_ response: Data, with selection: Selection) throws { + public init(_ response: Data, with selection: Selection) throws { // Decodes the data using provided selection. var errors: [GraphQLError]? = nil var extensions: [String: AnyCodable]? = nil do { - let decoder = JSONDecoder() + let decoder = ZippyJSONDecoder() let response = try decoder.decode(GraphQLResponse.self, from: response) errors = response.errors extensions = response.extensions